Advertisement
Guest User

Tutorial 2 - wxPython and event names (Infinity77)

a guest
Jul 24th, 2011
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.60 KB | None | 0 0
  1. import wx
  2. import wx.calendar
  3. import wx.grid
  4.  
  5. import inspect
  6.  
  7. ########################################################################
  8.  
  9. class MyPanel(wx.Panel):
  10.    
  11.     def __init__(self, parent):
  12.  
  13.         wx.Panel.__init__(self, parent)
  14.        
  15.         self.button = wx.Button(self, -1, "Button")
  16.         self.checkbox = wx.CheckBox(self, -1, "Checkbox")
  17.         self.calendar_ctrl = wx.calendar.CalendarCtrl(self, -1)
  18.         self.grid = wx.grid.Grid(self, -1, size=(1, 1))
  19.         self.choice = wx.Choice(self, -1, choices=["One", "Two", "Three"])
  20.         self.listbox = wx.ListBox(self, -1, choices=["One", "Two", "Three"])
  21.  
  22.         self.SetProperties()
  23.         self.DoLayout()
  24.  
  25.         self.BindEvents()
  26.  
  27.  
  28.     def BindEvents(self):
  29.        
  30.         self.Bind(wx.EVT_BUTTON, self.OnEvent, self.button)
  31.         self.Bind(wx.EVT_CHECKBOX, self.OnEvent, self.checkbox)
  32.         self.Bind(wx.calendar.EVT_CALENDAR_WEEKDAY_CLICKED, self.OnEvent, self.calendar_ctrl)
  33.         self.Bind(wx.calendar.EVT_CALENDAR_YEAR, self.OnEvent, self.calendar_ctrl)
  34.         self.Bind(wx.calendar.EVT_CALENDAR, self.OnEvent, self.calendar_ctrl)
  35.         self.Bind(wx.calendar.EVT_CALENDAR_MONTH, self.OnEvent, self.calendar_ctrl)
  36.         self.Bind(wx.calendar.EVT_CALENDAR_DAY, self.OnEvent, self.calendar_ctrl)
  37.         self.Bind(wx.grid.EVT_GRID_CMD_CELL_CHANGE, self.OnEvent, self.grid)
  38.         self.Bind(wx.grid.EVT_GRID_CMD_CELL_RIGHT_DCLICK, self.OnEvent, self.grid)
  39.         self.Bind(wx.grid.EVT_GRID_CMD_CELL_LEFT_CLICK, self.OnEvent, self.grid)
  40.         self.Bind(wx.grid.EVT_GRID_CMD_COL_SIZE, self.OnEvent, self.grid)
  41.         self.Bind(wx.grid.EVT_GRID_CMD_ROW_SIZE, self.OnEvent, self.grid)
  42.         self.Bind(wx.grid.EVT_GRID_CMD_SELECT_CELL, self.OnEvent, self.grid)
  43.         self.Bind(wx.grid.EVT_GRID_CMD_CELL_LEFT_DCLICK, self.OnEvent, self.grid)
  44.         self.Bind(wx.grid.EVT_GRID_CMD_CELL_RIGHT_CLICK, self.OnEvent, self.grid)
  45.         self.Bind(wx.EVT_CHOICE, self.OnEvent, self.choice)
  46.         self.Bind(wx.EVT_LISTBOX_DCLICK, self.OnEvent, self.listbox)
  47.         self.Bind(wx.EVT_LISTBOX, self.OnEvent, self.listbox)
  48.         self.Bind(wx.EVT_LEFT_DCLICK, self.OnEvent)
  49.         self.Bind(wx.EVT_RIGHT_DOWN, self.OnEvent)
  50.  
  51.  
  52.     def SetProperties(self):
  53.  
  54.         self.grid.CreateGrid(10, 3)
  55.         self.choice.SetSelection(0)
  56.         self.listbox.SetSelection(0)
  57.  
  58.  
  59.     def DoLayout(self):
  60.  
  61.         flag = wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL
  62.         grid_sizer = wx.GridSizer(3, 3, 0, 0)
  63.         grid_sizer.Add(self.button, 0, flag, 0)
  64.         grid_sizer.Add(self.checkbox, 0, flag, 0)
  65.         grid_sizer.Add(self.calendar_ctrl, 0, flag, 0)
  66.         grid_sizer.Add(self.grid, 1, wx.EXPAND|flag, 0)
  67.         grid_sizer.Add(self.choice, 0, flag, 0)
  68.         grid_sizer.Add(self.listbox, 0, flag, 0)
  69.         self.SetSizer(grid_sizer)
  70.         grid_sizer.Fit(self)
  71.  
  72.     #----------------------------------------------------------------------
  73.     def OnEvent(self, event):
  74.         """
  75.        Print out what event was fired
  76.        """
  77.  
  78.         widget = event.GetEventObject()
  79.         eventId = event.GetEventType()
  80.  
  81.         self.HuntEvent(widget, eventId)
  82.  
  83.  
  84.     def HuntEvent(self, widget, eventId):
  85.        
  86.         moduleStr = widget.__module__
  87.        
  88.         if "__main__" in moduleStr:
  89.             classes = inspect.getmro(self.__class__)
  90.             moduleStr = str(classes[1])
  91.        
  92.         if "._core" in moduleStr or "._windows" in moduleStr or \
  93.            "._controls" in moduleStr:
  94.             module = __import__("wx")
  95.         else:
  96.             local = moduleStr.replace("wx.", "")
  97.             module = __import__(moduleStr, fromlist=local)
  98.        
  99.         evtNames = [x for x in dir(module) if x.startswith("EVT_")]
  100.  
  101.         printStr = "You got event %-30s id = %5d, module = %s"
  102.        
  103.         for name in evtNames:
  104.  
  105.             evt = getattr(module, name)
  106.  
  107.             if not isinstance(evt, wx.PyEventBinder):
  108.                 continue
  109.  
  110.             if evt.typeId == eventId:
  111.                 print printStr%(name+":", eventId, moduleStr)
  112.                 break
  113.  
  114.  
  115. class MyForm(wx.Frame):
  116.  
  117.     #----------------------------------------------------------------------
  118.     def __init__(self):
  119.         wx.Frame.__init__(self, None, size=(600, 500), title="Tutorial 2")
  120.  
  121.         # Add a panel so it looks the correct on all platforms
  122.         panel = MyPanel(self)
  123.  
  124.  
  125.  
  126. #----------------------------------------------------------------------
  127. # Run the program
  128. if __name__ == "__main__":
  129.     app = wx.App(False)
  130.     frame = MyForm().Show()
  131.     app.MainLoop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement