# MenuDemoApplicationGTKdeprecated.py (c) Kari Laitinen # 2006-08-24 File created. # 2006-08-25 Last modification. # This is a somewhat 'raw' program. I stopped developing this # after I found out that gtk.ItemFactory is a deprecated class. import pygtk pygtk.require('2.0') import gtk class MenuDemoApplication : def get_main_menu(self, window): accel_group = gtk.AccelGroup() # This function initializes the item factory. # Param 1: The type of menu - can be MenuBar, Menu, # or OptionMenu. # Param 2: The path of the menu. # Param 3: A reference to an AccelGroup. The item factory sets up # the accelerator table while generating menus. item_factory = gtk.ItemFactory(gtk.MenuBar, "
", accel_group) # This method generates the menu items. Pass to the item factory # the list of menu items item_factory.create_items(self.menu_items) # Attach the new accelerator group to the window. window.add_accel_group(accel_group) # need to keep a reference to item_factory to prevent its destruction self.item_factory = item_factory # Finally, return the actual menu bar created by the item factory. return item_factory.get_widget("
") def __init__( self ) : self.application_window = gtk.Window( gtk.WINDOW_TOPLEVEL ) self.application_window.set_title( "Traditional Windows Application" ) self.application_window.connect("destroy", lambda w: gtk.main_quit() ) self.application_window.set_size_request( 600, 500 ) self.menu_items = ( ( "/_File", None, None, 0, "" ), ( "/File/_New", "N", self.file_new_selection_made, 0, None ), ( "/File/_Open", "O", None, 0, None ), ( "/File/_Save", "S", None, 0, None ), ( "/File/Save _As", None, None, 0, None ), ( "/File/sep1", None, None, 0, "" ), ( "/File/Quit", "Q", gtk.main_quit, 0, None ), ( "/_Options", None, None, 0, "" ), ( "/Options/Test", None, None, 0, None ), ( "/_Help", None, None, 0, "" ), ( "/_Help/About", None, None, 0, None ), ) main_vbox = gtk.VBox(False, 1) main_vbox.set_border_width(1) self.application_window.add( main_vbox ) main_vbox.show() menubar = self.get_main_menu( self.application_window ) main_vbox.pack_start(menubar, False, True, 0) menubar.show() self.drawing_area = gtk.DrawingArea() self.drawing_area.set_size_request( 600, 450 ) self.drawing_area.connect( "expose-event", self.drawing_area_exposed ) main_vbox.pack_start( self.drawing_area, False, True, 0 ) window_width, window_height = self.application_window.get_size() self.ball_position_x = window_width / 2 - 40 ; self.ball_position_y = window_height / 2 - 40 ; self.drawing_area.show() self.application_window.show() self.last_selected_menu_item = "NO SELECTIONS MADE" def file_new_selection_made( self, widget, data=None ) : self.last_selected_menu_item = "File -> New" self.drawing_area.queue_draw() def up_button_clicked( self, widget, data=None ) : self.ball_position_y = self.ball_position_y - 3 self.drawing_area.queue_draw() def down_button_clicked( self, widget, data=None ) : self.ball_position_y = self.ball_position_y + 3 self.drawing_area.queue_draw() def right_button_clicked( self, widget, data=None ) : self.ball_position_x = self.ball_position_x + 3 self.drawing_area.queue_draw() def color_selection_menu_used( self, menu_selection ) : self.current_ball_color = menu_selection.get_text() self.drawing_area.queue_draw() def drawing_area_exposed( self, area, event ) : graphics_context = self.drawing_area.get_style().fg_gc[gtk.STATE_NORMAL] this_drawable = self.drawing_area.window text_to_show = self.drawing_area.create_pango_layout( "" ) text_to_show.set_text( self.last_selected_menu_item ) this_drawable.draw_layout( graphics_context, 20, 20, text_to_show ) return True def run( self ) : gtk.main() if __name__ == "__main__": this_application = MenuDemoApplication() this_application.run()