In this Python GUI article i want to show you creating of Toggle Button in wxPython. so Toggle Button is a button that stays pressed when clicked by the user.it is similar to wx.CheckBox in functionality, but looks like wx.Button. since wxWidgets version 2.9.0 this control emits an update UI event.
Also you can check More GUI Development Tutorials in the below link.
1: PyQt5 GUI Development Tutorials
2: TKinter GUI Development Tutorials
3: Pyside2 GUI Development Tutorials
4: Kivy GUI Development Tutorials
So this is the complete code for Python GUI Toggle Button in wxPython
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
import wx class MyFrame(wx.Frame): def __init__(self, parent, title): super(MyFrame, self).__init__(parent, title =title, size = (600,500)) self.panel = MyPanel(self) class MyPanel(wx.Panel): def __init__(self, parent): super(MyPanel, self).__init__(parent) vbox = wx.BoxSizer(wx.VERTICAL) self.label = wx.StaticText(self, label = "Hello") vbox.Add(self.label, 0,wx.EXPAND) self.toggleBtn = wx.ToggleButton(self, label = "Click To On") vbox.Add(self.toggleBtn, 0) self.toggleBtn.Bind(wx.EVT_TOGGLEBUTTON, self.onToggleClick) self.SetSizer(vbox) def onToggleClick(self, event): state = event.GetEventObject().GetValue() if state == True: self.label.SetLabelText("Off") event.GetEventObject().SetLabel("Click To Off") else: self.label.SetLabelText("On") event.GetEventObject().SetLabel("Click To On") class MyApp(wx.App): def OnInit(self): self.frame = MyFrame(parent=None, title="Toggle Button") self.frame.Show() return True app = MyApp() app.MainLoop() |
This is our MyFrame class that inherits from wx.Frame. and it is our top level window. and also we have created the object of MyPanel in here.
1 2 3 |
class MyFrame(wx.Frame): def __init__(self, parent, title): super(MyFrame, self).__init__(parent, title =title, size = (600,500)) |
So this is MyPanel class that inherits from wx.Panel and in this class we create our label, Toggle Button and layout.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
class MyPanel(wx.Panel): def __init__(self, parent): super(MyPanel, self).__init__(parent) vbox = wx.BoxSizer(wx.VERTICAL) self.label = wx.StaticText(self, label = "Hello") vbox.Add(self.label, 0,wx.EXPAND) self.toggleBtn = wx.ToggleButton(self, label = "Click To On") vbox.Add(self.toggleBtn, 0) self.toggleBtn.Bind(wx.EVT_TOGGLEBUTTON, self.onToggleClick) self.SetSizer(vbox) |
This is our layout in wxPython.
1 |
vbox = wx.BoxSizer(wx.VERTICAL) |
And this is our label because we need a label in this article.
1 2 |
self.label = wx.StaticText(self, label = "Hello") vbox.Add(self.label, 0,wx.EXPAND) |
This is the Toggle Button that we have created
1 2 |
self.toggleBtn = wx.ToggleButton(self, label = "Click To On") vbox.Add(self.toggleBtn, 0) |
This is the event binding, because we want to add event handling for Toggle Button.
1 |
self.toggleBtn.Bind(wx.EVT_TOGGLEBUTTON, self.onToggleClick) |
And this is the method that we have bind at the top for our Toggle Button
1 2 3 4 5 6 7 8 9 10 11 12 13 |
def onToggleClick(self, event): state = event.GetEventObject().GetValue() if state == True: self.label.SetLabelText("Off") event.GetEventObject().SetLabel("Click To Off") else: self.label.SetLabelText("On") event.GetEventObject().SetLabel("Click To On") |
So the last class is MyApp class that inherits from wx.App. the OnInit() method is where you will most often create frame subclass objects.
and start our main loop.That’s it. Once the application’s main event loop processing takes over, control passes to wxPython. Unlike procedural programs, a wxPython GUI program primarily responds to the events taking place around it, mostly determined by a human user clicking with a mouse and typing at the keyboard. When all the frames in an application have been closed, the app.MainLoop() method will return and the program will exit.
1 2 3 4 5 6 7 8 9 10 |
class MyApp(wx.App): def OnInit(self): self.frame = MyFrame(parent=None, title="Toggle Button") self.frame.Show() return True app = MyApp() app.MainLoop() |
So run the code and this will be the result
Also you can watch the complete video for this article
Subscribe and Get Free Video Courses & Articles in your Email