code_junkie

Receiving keystrokes in an opened wx.ComboCtrl

Nov 13th, 2011
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. import wx
  2. import wx.combo
  3.  
  4. class CustomPopup(wx.combo.ComboPopup):
  5.  
  6. def Create(self, parent):
  7. # Create the popup with a bunch of radiobuttons
  8. self.panel = wx.Panel(parent)
  9. sizer = wx.GridSizer(cols=2)
  10. for x in range(10):
  11. r = wx.RadioButton(self.panel, label="Element "+str(x))
  12. r.Bind(wx.EVT_RADIOBUTTON, self.on_selection)
  13. sizer.Add(r)
  14. self.panel.SetSizer(sizer)
  15.  
  16. # Handle keyevents
  17. self.panel.Bind(wx.EVT_KEY_UP, self.on_key)
  18.  
  19. def GetControl(self):
  20. return self.panel
  21.  
  22. def GetAdjustedSize(self, minWidth, prefHeight, maxHeight):
  23. return wx.Size(200, 150)
  24.  
  25. def on_key(self, evt):
  26. if evt.GetEventObject() is self.panel:
  27. # Trying to redirect the key event to the combo.. But this always returns false :(
  28. print self.GetCombo().GetTextCtrl().GetEventHandler().ProcessEvent(evt)
  29. evt.Skip()
  30.  
  31. def on_selection(self, evt):
  32. self.Dismiss()
  33. wx.MessageBox("Selection made")
  34.  
  35.  
  36. class CustomFrame(wx.Frame):
  37.  
  38. def __init__(self):
  39. # Toolbar-shaped frame with a ComboCtrl
  40. wx.Frame.__init__(self, None, -1, "Test", size=(800,50))
  41. combo = wx.combo.ComboCtrl(self)
  42. popup = CustomPopup()
  43. combo.SetPopupControl(popup)
  44.  
  45. sizer = wx.BoxSizer(wx.VERTICAL)
  46. sizer.Add(combo, 0)
  47. self.SetSizer(sizer)
  48. self.Layout()
  49.  
  50. if __name__ == '__main__':
  51. app = wx.PySimpleApp()
  52. CustomFrame().Show()
  53. app.MainLoop()
Add Comment
Please, Sign In to add comment