Introduction

Avoiding EVT_MENU because it is ugly and not very Pythonic

What Objects are Involved

EvtHandler.Connect()

Process Overview

Just a simple example of using Connect() for attaching handlers to events. This example uses the wxEVT_COMMAND_MENU_SELECTED (10010) event.

Code Sample

Note: This example is obsolete. The preferred style is now to use frame.Bind(), which did not exist when this page was written.

   1 import wx
   2 ID_About = 101
   3 ID_Exit = 102
   4 
   5 class BasicApp(wx.App):
   6   def OnInit(self):
   7     frame = BasicFrame(None, -1, "Hello from wxPython")
   8     frame.Show(True)
   9 
  10     frame.Connect(ID_About, -1, # see line 34
  11                   wx.wxEVT_COMMAND_MENU_SELECTED, frame.OnAbout)
  12     frame.Connect(ID_Exit, -1,  # see line 35
  13                   wx.wxEVT_COMMAND_MENU_SELECTED, frame.OnExit)
  14     
  15     self.SetTopWindow(frame)
  16     return True;
  17 
  18 class BasicFrame(wx.Frame):
  19   def __init__(self, parent, ID, title):
  20     wx.Frame.__init__(self, parent, ID, title,
  21                      wx.DefaultPosition, wx.Size(200, 150))
  22     self.CreateStatusBar()
  23     self.SetStatusText("This is the statusbar")
  24     menu = wx.Menu()
  25     menu.Append(ID_About, "&About",
  26                 "More information about this program")
  27     menu.AppendSeparator()
  28     menu.Append(ID_Exit, "&Exit", "Terminate program")
  29     menuBar = wx.MenuBar()
  30     menuBar.Append(menu, "&File")
  31     self.SetMenuBar(menuBar)
  32 
  33    # see line 11 - EVT_MENU(self, ID_About, self.OnAbout)
  34    # see line 12 - EVT_MENU(self, ID_Exit, self.OnExit)
  35     
  36   def OnAbout(self, event):
  37     dlg = wx.MessageDialog(self, "No EVT_MENU!",
  38                           "About No EVT_MENU", wx.OK | wx.ICON_INFORMATION)
  39     dlg.ShowModal()
  40     dlg.Destroy()
  41 
  42   def OnExit(self, event):
  43     self.Close(True)
  44     
  45   
  46 app = BasicApp(0)
  47 app.MainLoop()

Comments

Send comments to Wayne Witzel

Avoiding EVT_MENU (last edited 2009-11-13 16:32:46 by LLPROXY)

NOTE: To edit pages in this wiki you must be a member of the TrustedEditorsGroup.