# MenuDemoWithPopupMenuGTK.py (c) Kari Laitinen
# http://www.naturalprogramming.com
# 2006-08-30 File created.
# 2006-10-17 Last modification.
# This program is another enhanced version of MenuDemoApplicationGTK.py
# When a mouse button is pressed in the drawing area, a popup menu
# is displayed. The popup menu is the same as the Settings menu that
# belongs to the menubar.
# The difference between this program and
# MenuDemoApplicationGTK.py is that a popup definition is
# added to the XML-based user_interface_description and
# a popup menu "object" is created in the __init__() method.
# In addition, the program is made to react to mouse pressings,
# and the mouse_button_pressed() method activates the popup
# menu after a mouse button has been pressed.
import gtk
import pango
class MenuDemoWithPopupMenu :
user_interface_description = """
"""
def __init__( self ) :
self.application_window = gtk.Window( gtk.WINDOW_TOPLEVEL )
self.application_window.set_title( "GUI Application with Menus" )
self.application_window.connect("destroy", self.exit_this_application )
self.application_window.set_size_request( 600, 500 )
main_vertical_box = gtk.VBox( False, 1 )
main_vertical_box.set_border_width( 1 )
self.application_window.add( main_vertical_box )
main_vertical_box.show()
# Create a ui_manager instance
ui_manager = gtk.UIManager()
# Add the accelerator group to the toplevel window
accelerator_group = ui_manager.get_accel_group()
self.application_window.add_accel_group( accelerator_group )
# Create an ActionGroup
self.action_group = gtk.ActionGroup("GUIApplicationExample")
# When an action is specified ( "File", None, "_File" )
# the string "File" is a link to the same string in the
# XML description and the string "_File" specifies
# what is the menu name on the screen. The underscore in
# the menu name specifies the Alt key combination that
# acts as a shortcut for the selection. A menu named
# "Fi_le" could thus be selected by typing Alt-L.
# Note that the actions in the File menu can also be
# selected with the help of the Ctrl key.
# Create basic actions
self.action_group.add_actions(
[ ( "File", None, "_File" ),
( "Open", None, "_Open", "O",
"Open a file", self.process_menu_selection ),
( "Save", None, "_Save", "S",
"Save a file", self.process_menu_selection ),
( "Quit", None, "_Quit", "Q",
"Quit the Program", self.exit_this_application),
( "Edit", None, "_Edit" ),
( "Copy", None, "_Copy", None,
None, self.process_menu_selection ),
( "Paste", None, "_Paste", None,
None, self.process_menu_selection ),
( "Settings", None, "_Settings" ),
( "Text size", None, "_Text size" ),
( "Help", None, "_Help" ),
( "About", None, "_About", None,
None, self.help_about_selection_made ) ] )
self.action_group.add_toggle_actions(
[ ( "Cyan background", None, "_Cyan background",
None, None, self.background_color_changed ) ] )
# Create RadioActions to control the used text size
# The integers 0, 1, 2 are values associated with the actions
# The last integer 1 means the original selected text size.
self.action_group.add_radio_actions(
[ ( "Small", None, "_Small", None, None, 0 ),
( "Medium", None, "_Medium", None, None, 1 ),
( "Large", None, "_Large", None, None, 2 ) ],
1, self.text_size_changed )
# Add the action_group to the ui_manager
ui_manager.insert_action_group( self.action_group, 0 )
# Add a UI description
ui_manager.add_ui_from_string( self.user_interface_description )
# Create a MenuBar
menu_bar = ui_manager.get_widget("/MenuBar")
main_vertical_box.pack_start( menu_bar, False, True, 0 )
menu_bar.show()
# Create a Popup menu
self.popup_menu = ui_manager.get_widget( "/Popup" )
self.drawing_area = gtk.DrawingArea()
self.drawing_area.set_size_request( 600, 450 )
self.drawing_area.connect( "expose-event", self.drawing_area_exposed )
self.drawing_area.connect( "button_press_event",
self.mouse_button_pressed )
self.drawing_area.set_events(gtk.gdk.EXPOSURE_MASK
| gtk.gdk.BUTTON_PRESS_MASK )
main_vertical_box.pack_start( self.drawing_area, False, True, 0 )
self.drawing_area.show()
self.application_window.show()
self.original_background_color = \
self.drawing_area.get_style().bg[gtk.STATE_NORMAL]
self.last_selected_menu_item = "NO SELECTIONS MADE"
def mouse_button_pressed( self, widget, event ) :
self.popup_menu.popup( None, None, None, 0, 0 )
self.drawing_area.queue_draw()
return True
def process_menu_selection( self, action, data=None ) :
# This method processes the actions File->Open,
# File->Save, Edit->Copy, and Edit->Paste
self.last_selected_menu_item = action.get_name()
self.drawing_area.queue_draw()
def help_about_selection_made( self, action, data=None ) :
self.last_selected_menu_item = "About"
self.about_dialog = gtk.MessageDialog( None,
gtk.DIALOG_MODAL,
gtk.MESSAGE_INFO,
gtk.BUTTONS_OK, "This is a simple GUI application" )
self.about_dialog.set_title( "About this application" )
self.about_dialog.connect( "response", self.about_dialog_button_pressed )
self.about_dialog.show()
self.drawing_area.queue_draw()
def about_dialog_button_pressed( self, dialog, data=None ) :
self.about_dialog.hide()
self.about_dialog.destroy()
def background_color_changed( self, action, data=None ) :
if self.action_group.get_action( "Cyan background" ).get_active() :
new_background_color = \
self.drawing_area.window.get_colormap().alloc_color( "cyan" )
else :
new_background_color = \
self.drawing_area.window.get_colormap().alloc_color(
self.original_background_color )
# Set window background color
drawing_area_style = self.drawing_area.get_style().copy()
drawing_area_style.bg[gtk.STATE_NORMAL] = new_background_color
self.drawing_area.set_style( drawing_area_style )
self.last_selected_menu_item = "Cyan background"
def text_size_changed( self, action, data=None ) :
# This method processes the actions caused by the
# radio button menu items. The int value returned by
# get_current_value() is used as an index to take
# one string of the tuple. The int values are specified
# when the radio actions are defined.
self.last_selected_menu_item = \
( "Small", "Medium", "Large" ) [ action.get_current_value() ]
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
if self.action_group.get_action( "Small" ).get_active() :
font_description_string = "serif 10"
elif self.action_group.get_action( "Medium" ).get_active() :
font_description_string = "serif 14"
elif self.action_group.get_action( "Large" ).get_active() :
font_description_string = "serif 18"
text_to_show = self.drawing_area.create_pango_layout( "" )
text_to_show.set_font_description(
pango.FontDescription( font_description_string ) )
text_to_show.set_text( "Last selected menu item \"" + \
self.last_selected_menu_item + "\"" )
this_drawable.draw_layout( graphics_context, 100, 200, text_to_show )
return True
def exit_this_application( self, widget, data=None ) :
gtk.main_quit()
def run( self ) :
gtk.main()
if __name__ == "__main__":
this_application = MenuDemoWithPopupMenu()
this_application.run()