FORUM HOME | WHUK BLOG   
WEB HOSTING UK AFFORDABLE WEBSITE HOSTING SERVICES IN UNITED KINGDOM
PHP LINUX SHARED HOSTING WINDOWS ASP.NET HOSTING PACKAGES
ECOMMERCE HOSTING ASP MSSQL MS ACCESS ODBC FRONTPAGE HOSTING
CPANEL WHM FANTASTICO RESELLER DEDICATED SERVER WEB HOSTING
CHEAP PLESK CPANEL HTML MYSQL BEST UK VPS HOSTING COMPANY
CHEAP RELIABLE UK HOSTING PROVIDER SINCE 2001
MANAGED WEB HOSTING SERVICE
AFFORDABLE WEBSITE HOSTING SERVICES IN UNITED KINGDOM

Web Hosting UK Forums | Linux Windows Dedicated Server and cPanel VPS Hosting Forum » Web Hosting and Domains » FAQ's / Tutorials.

Reply
 
LinkBack Thread Tools Display Modes

  #1 (permalink)  
Old 15-12-11, 12:50 PM
Cust. Interaction Assoc.
 
Join Date: Aug 2011
Posts: 149
Send a message via Skype™ to bastones
Default How to add a menu bar to GTK+ PyGTK PHP-GTK Applications

Here is my code:

Code:
def __init__(self):
		menuBar = self.addMenus() # returns MenuBar object
		self.window.add(self.mainHBox)
		# add to mainHBox
		self.mainHBox.pack_start(menuBar)

def addMenus(self):
		# This will hold the menu items
		fileMenuList = gtk.Menu()
		helpMenuList = gtk.Menu()
		fileMenu = [gtk.MenuItem("File"), gtk.MenuItem("Quit")]
		helpMenu = [gtk.MenuItem("Help"), gtk.MenuItem("About")]
		fileMenu[0].set_submenu(fileMenuList)
		fileMenuList.append(fileMenu[1])
		helpMenu[0].set_submenu(helpMenuList)
		helpMenuList.append(helpMenu[1])
		menuBar = gtk.MenuBar()
		menuBar.append(fileMenu[0])
		menuBar.append(helpMenu[0])
		return menuBar # return MenuBar object
So, let's give context to the addMenus() method. And the above code is indeed in Python but the GTK+ API remains the same for whatever language you use with GTK+, whether C, PHP(GTK), etc.

Code:
fileMenuList = gtk.Menu()
helpMenuList = gtk.Menu()
This is for the menu list (i.e. what appears in the File and Help menu options.

Code:
fileMenu = [gtk.MenuItem("File"), gtk.MenuItem("Quit")]
helpMenu = [gtk.MenuItem("Help"), gtk.MenuItem("About")]
We create the menu items for each individual menu item. Specifying what is a menu option and a specific menu item within a menu option (i.e. File is the menu option and Quit is the menu item) will be done later.

Code:
fileMenu[0].set_submenu(fileMenuList)
fileMenuList.append(fileMenu[1])
helpMenu[0].set_submenu(helpMenuList)
helpMenuList.append(helpMenu[1])
If you didn't notice above, we are using lists (which are like arrays), so we are specifying the sub menu for the "File" file menu item as the fileMenuList (the GtkMenu class), and likewise with the Help menu option.

Then each Menu class is being appended the relevant MenuItem.

Code:
menuBar = gtk.MenuBar()
menuBar.append(fileMenu[0])
menuBar.append(helpMenu[0])
return menuBar # return MenuBar object
Then the MenuBar is created, and the main menu options are appended to the MenuBar. Why do we not append the other 2 items? It is because the menu items for the menu options (menu options being File and Help) have been appended to the Menus, and the Menus have been set as the sub menu for the Menu Options, so the only thing to do is append the menu options to the MenuBar, and everything will then be displayed.

Then, we pack the MenuBar to the mainHBox, which is then added to the main GtkWindow in some other code not shown:

Code:
def __init__(self):
		menuBar = self.addMenus() # returns MenuBar object
		self.window.add(self.mainHBox)
		# add to mainHBox
		self.mainHBox.pack_start(menuBar)
Hope this helps.
__________________
Regards,
Ben Stones

Customer Interaction Associate

Need to contact us?

Support - 0808 262 0606 or
support.webhosting.uk.com

Customer Relations - 0808 262 0855 or
support.webhosting.uk.com

Sales - 0800 862 0890 or sales@webhosting.uk.com


Contact me
ben@webhosting.uk.com
Skype ID: whuk_ben
Reply With Quote

Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump


All times are GMT. The time now is 07:49 PM.

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Copyright 2001-2010 Web Hosting UK. All rights reserved.
Web Hosting UK Forum





Site Map

Shared Cloud
Shared Cloud From £1

Affiliate Program
Earn up to £300 Per Sale

Dedicated Servers
Dedicated Server Hosting

Cloud Hosting
Cloud Server Hosting

Load Balanced Server
Load Balancing Server

VPS Hosting
Linux VPS Hosting

Windows VPS
Windows 2003 VPS

Zimbra Hosting
Zimbra Email Hosting

cPanel Hosting
Shared Linux Hosting

Windows Hosting
Shared Windows Hosting

Coldfusion Hosting
Windows Coldfusion Hosting

cPanel Reseller Hosting
Reseller Hosting

Windows Reseller
Windows Reseller Hosting

Email Web Hosting
Email Hosting

Semi-Dedicated Server
Semi-Dedicated Hosting

Remote Backup Plans
Offsite Backup Service


cpanel hosting
Knowledgebase Articles

Pre-Sales Question
Web Hosting FAQ's

Dedicated Hosting
Dedicated Server FAQ's

Virtual Private Servers
VPS Hosting

PHP MySQL Hosting
cPanel Hosting

Windows Hosting
ASP MSSQL Hosting

Domain Name
Domain registration FAQ's

CMS Hosting
CMS Hosting FAQ's

Payment Gateways
Payment FAQ's


Support Tutorials

cPanel Tutorials
cPanel Flash Tutorials

Wordpress Tutorials
Wordpress Flash Tutorials

Plesk Tutorials
Plesk Flash Tutorials

PhpMyadmin Tutorials
PhpMyadmin Flash Tutorials

Drupal Tutorials
Drupal Flash Tutorials

Mambo Tutorials
Mambo Flash Tutorials

Joomla Tutorials
Joomla Flash Tutorials

More Hosting Tutorials