wxPython Button Demo

Please support our Python advertiser: Programming Forums
May 29th, 2006
Views: 10,157
Thread Rating: 1 votes, 5.0000 average.
AddThis Social Bookmark Button
In this snippet we are playing around with wxPython's buttons, showing you how to bind the mouse click event, enable and disable, show and hide the buttons. Each button also has a tool-tip (hint) associated with itself.
Last edited : Jun 22nd, 2006.
python Syntax
  1. # create a wx frame with 6 wx buttons and optional tooltips
  2. # hide/disable and show/enable the buttons as they are clicked
  3. # also show right-click and double-click events
  4. # tested with Python24 and wxPython26 by vegaseat 29may2006
  5.  
  6. import wx
  7.  
  8. class MyFrame(wx.Frame):
  9. """make a frame, inherits wx.Frame"""
  10. def __init__(self):
  11. # create a frame, no parent, default to wxID_ANY
  12. wx.Frame.__init__(self, None, wx.ID_ANY, 'wxButton',
  13. pos=(300, 150), size=(320, 250))
  14. self.SetBackgroundColour("green")
  15.  
  16. self.button1 = wx.Button(self, id=-1, label='Button1',
  17. pos=(8, 8), size=(175, 28))
  18. self.button1.Bind(wx.EVT_BUTTON, self.button1Click)
  19. # optional tooltip
  20. self.button1.SetToolTip(wx.ToolTip("click to hide"))
  21.  
  22. self.button2 = wx.Button(self, id=-1, label='Button2',
  23. pos=(8, 38), size=(175, 28))
  24. self.button2.Bind(wx.EVT_BUTTON, self.button2Click)
  25. # optional tooltip
  26. self.button2.SetToolTip(wx.ToolTip("click to hide"))
  27.  
  28. self.button3 = wx.Button(self, id=-1, label='Button3',
  29. pos=(8, 68), size=(175, 28))
  30. self.button3.Bind(wx.EVT_BUTTON, self.button3Click)
  31. # optional tooltip
  32. self.button3.SetToolTip(wx.ToolTip("click to disable"))
  33.  
  34. self.button4 = wx.Button(self, id=-1, label='Button4',
  35. pos=(8, 98), size=(175, 28))
  36. self.button4.Bind(wx.EVT_BUTTON, self.button4Click)
  37. # optional tooltip
  38. self.button4.SetToolTip(wx.ToolTip("click to disable"))
  39.  
  40. self.button5 = wx.Button(self, id=-1, label='Button5',
  41. pos=(8, 128), size=(175, 28))
  42. self.button5.Bind(wx.EVT_RIGHT_DOWN, self.button5Click)
  43. # optional tooltip
  44. self.button5.SetToolTip(wx.ToolTip("right click"))
  45.  
  46. self.button6 = wx.Button(self, id=-1, label='Button6',
  47. pos=(8, 158), size=(175, 28))
  48. self.button6.Bind(wx.EVT_LEFT_DCLICK, self.button6Click)
  49. # optional tooltip
  50. self.button6.SetToolTip(wx.ToolTip("left double click"))
  51.  
  52. # show the frame
  53. self.Show(True)
  54.  
  55. def button1Click(self,event):
  56. self.button1.Hide()
  57. self.SetTitle("Button1 clicked")
  58. self.button2.Show()
  59.  
  60. def button2Click(self,event):
  61. self.button2.Hide()
  62. self.SetTitle("Button2 clicked")
  63. self.button1.Show()
  64.  
  65. def button3Click(self,event):
  66. self.button3.Disable()
  67. self.SetTitle("Button3 clicked")
  68. self.button4.Enable()
  69.  
  70. def button4Click(self,event):
  71. self.button4.Disable()
  72. self.SetTitle("Button4 clicked")
  73. self.button3.Enable()
  74.  
  75. def button5Click(self,event):
  76. self.SetTitle("Button5 right-clicked")
  77.  
  78. def button6Click(self,event):
  79. self.SetTitle("Button6 double-clicked")
  80.  
  81.  
  82. application = wx.PySimpleApp()
  83. # call class MyFrame
  84. window = MyFrame()
  85. # start the event loop
  86. application.MainLoop()
Racoon200 | Junior Poster in Training | Nov 16th, 2007
how do I run a wxPython .py file?  

Only community members can submit or comment on code snippets. You must register or log in to contribute.

Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 6:06 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC