A Look at wxPython - Events
(Page 5 of 6 )
What good is a menu when it doesn't do anything besides look pretty? Let's add some real functionality to our menu. As you probably have guessed, wxPython makes this very simple. Let's combine our knowledge of dialogs with our knowledge of menus. We just have to glue it together. Remember those unique numbers we assigned to each menu item? We'll use them below:
from wxPython.wx import *
application = wxPySimpleApp()
window = wxFrame ( None, wxID_ANY, 'Title Here.' )
window.CreateStatusBar()
# You'll see the signifigance of "name" in a second
name = 'John Doe'
# Create a menu
# Nothing new here
menu = wxMenu()
menu.Append ( 100, 'Text Entry', 'Open a text entry dialog.' )
menu.Append ( 101, 'Message', 'Open a message dialog.' )
bar = wxMenuBar()
bar.Append ( menu, 'Options' )
window.SetMenuBar ( bar )
# Now we'll make the menu options do something
# First, we'll define two functions:
def text ( event ):
global name
textDialog = wxTextEntryDialog ( window, 'What is your name?', 'Name', name )
textDialog.ShowModal()
name = textDialog.GetValue()
textDialog.Destroy()
def message ( event ):
messageDialog = wxMessageDialog ( window, 'Your name is: ' + name, 'Name', wxICON_INFORMATION | wxOK )
messageDialog.ShowModal()
messageDialog.Destroy()
# We will now attach our functions to our menu
EVT_MENU ( window, 100, text )
EVT_MENU ( window, 101, message )
window.Show ( True )
application.MainLoop()
Next: Some Widgets >>
More Python Articles
More By Peyton McCullough