#$Id: menubar.py,v 1.3 2004/04/22 04:31:18 mandava Exp $ # This example illustrates the menu widget. # importing pygtk module and initializing it. import gtk class menubar: # call back function when the window is closed or when quit button is # pressed. def close(self,widget,event,data = None): gtk.main_quit() return gtk.FALSE # call back function when the menu items are selected def menuitem_response(self,widget,string): print "%s" %string def __init__(self): # Create a new window self.window = gtk.Window(gtk.WINDOW_TOPLEVEL) # sets a handler for delete event that immediately exits GTK. self.window.connect("delete_event",self.close) # set the title and size of the window. self.window.set_title("menubar") self.window.set_size_request(200,30) # create a horizontal box. box2 = gtk.HBox(gtk.FALSE,0) # add the Hbox to the window and show it self.window.add(box2) box2.show() # Create a file menu file_menu = gtk.Menu() # create menu items to be packed into the menu new_item = gtk.MenuItem("new") open_item = gtk.MenuItem("open") quit_item = gtk.MenuItem("Quit") # add the menu items to the menu file_menu.append(new_item) file_menu.append(open_item) file_menu.append(quit_item) # Attach the callback functions to the activate signal new_item.connect("activate",self.menuitem_response,"new") open_item.connect("activate",self. menuitem_response, "open") # We can attach the Quit menu item to our exit function quit_item.connect ("activate",self.close , "quit") # We show menu items new_item.show() open_item.show() quit_item.show() # create an edit menu and menu items edit_menu = gtk.Menu() undo_item = gtk.MenuItem("undo") copy_item = gtk.MenuItem("copy") cut_item = gtk.MenuItem("cut") # add the menu items to the edit menu edit_menu.append(undo_item) edit_menu.append(copy_item) edit_menu.append(cut_item) # attach the callback function to the activate signals undo_item.connect("activate",self.menuitem_response,"undo") copy_item.connect("activate",self.menuitem_response,"copy") cut_item.connect("activate",self.menuitem_response,"cut") # show the menu items undo_item.show() copy_item.show() cut_item.show() # create a help menu,about menu item and add it to the help menu help_menu = gtk.Menu() about_item = gtk.MenuItem("about") help_menu.append(about_item) # attach the callback function to activate signals and show the # menu items about_item.connect("activate",self.menuitem_response,"about") about_item.show() # we create a menu item for file entry , to which we add our menu file_item = gtk.MenuItem("file") file_item.show() # we associate the menu with file item file_item.set_submenu(file_menu) # create a menu item for edit entry, to which we add our menu and # associate the menu with edit item edit_item = gtk.MenuItem("edit") edit_item.show() edit_item.set_submenu(edit_menu) # create a help item for help entry, to which we add our menu and # associate the menu with the help item help_item = gtk.MenuItem("help") help_item.show() help_item.set_submenu(help_menu) # create a menubar, pack it into the horizontal box and show it. menu_bar = gtk.MenuBar() box2.pack_start(menu_bar,gtk.FALSE,gtk.FALSE,2) menu_bar.show() # add the menus to the menu bar. menu_bar.append(file_item) menu_bar.append(edit_item) menu_bar.append(help_item) self.window.show() def main(self): gtk.main() if __name__ == "__main__": widget = menubar() widget.main()