In this Python GUI article i want to show Creating CheckBox in wxPython. so checkbox is a labelled box which by default is either on (checkmark is visible) or off (no checkmark).
This class supports the following styles:
- wx.CHK_2STATE: Create a 2-state checkbox. This is the default.
- wx.CHK_3STATE: Create a 3-state checkbox. Not implemented in GTK1.
- wx.CHK_ALLOW_3RD_STATE_FOR_USER: By default a user can’t set a 3-state checkbox to the third state. It can only be done from code. Using this flags allows the user to set the checkbox to the third state by clicking.
- wx.ALIGN_RIGHT: Makes the text appear on the left of the checkbox.
Python GUI Creating CheckBox in wxPython
Let’s create our example, so this is the complete code for Python GUI Creating CheckBox 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 |
import wx class MyFrame(wx.Frame): def __init__(self, parent, title): super(MyFrame, self).__init__(parent, title=title) # Set frame icon self.SetIcon(wx.Icon('codeloop.png', wx.BITMAP_TYPE_PNG)) self.panel = MyPanel(self) class MyPanel(wx.Panel): def __init__(self, parent): super(MyPanel, self).__init__(parent) # Create vertical box sizer vbox = wx.BoxSizer(wx.VERTICAL) # Create check boxes self.cb1 = wx.CheckBox(self, label="Cat") vbox.Add(self.cb1) self.cb2 = wx.CheckBox(self, label="Dog") vbox.Add(self.cb2) self.cb3 = wx.CheckBox(self, label="Tiger") vbox.Add(self.cb3) # Create static text label self.label = wx.StaticText(self, label="") vbox.Add(self.label) # Set sizer for the panel self.SetSizer(vbox) # Bind checkbox event to onChecked method self.Bind(wx.EVT_CHECKBOX, self.onChecked) # Define event handler for checkbox event def onChecked(self, e): cb = e.GetEventObject() self.label.SetLabelText('You Have Selected ' + cb.GetLabel()) class MyApp(wx.App): def OnInit(self): self.frame = MyFrame(parent=None, title="Codeloop.org - Check Boxes") 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 create the object of MyPanel in here.
1 2 3 4 5 6 7 8 |
class MyFrame(wx.Frame): def __init__(self, parent, title): super(MyFrame, self).__init__(parent, title =title) self.panel = MyPanel(self) |
This is MyPanel class that inherits from wx.Panel and we are going to create our layouts with CheckBox in this MyPanel class. because MyPanel class is a container for all widgets 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 |
class MyPanel(wx.Panel): def __init__(self, parent): super(MyPanel, self).__init__(parent) vbox = wx.BoxSizer(wx.VERTICAL) self.cb1= wx.CheckBox(self, label = "Cat") vbox.Add(self.cb1) self.cb2 = wx.CheckBox(self, label = "Dog") vbox.Add(self.cb2) self.cb3 = wx.CheckBox(self, label = "Tiger" ) vbox.Add(self.cb3) self.label = wx.StaticText(self, label = "") vbox.Add(self.label) self.SetSizer(vbox) self.Bind(wx.EVT_CHECKBOX, self.onChecked) |
So this is the code for creating of three CheckBoxes and also we are going to bind an event handler in these CheckBoxes.
1 2 3 4 5 6 7 8 9 10 11 12 |
self.cb1= wx.CheckBox(self, label = "Cat") vbox.Add(self.cb1) self.cb2 = wx.CheckBox(self, label = "Dog") vbox.Add(self.cb2) self.cb3 = wx.CheckBox(self, label = "Tiger" ) vbox.Add(self.cb3) self.label = wx.StaticText(self, label = "") vbox.Add(self.label) |
We also needs a StaticText for this
1 2 |
self.label = wx.StaticText(self, label = "") vbox.Add(self.label) |
This is event binding
1 |
self.Bind(wx.EVT_CHECKBOX, self.onChecked) |
And this is the method that we want to bind with our CheckBoxes.
1 2 3 |
def onChecked(self, e): cb = e.GetEventObject() self.label.SetLabelText('You Have Selected ' + cb.GetLabel()) |
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 |
class MyApp(wx.App): def OnInit(self): self.frame = MyFrame(parent=None, title="Codeloop.org - Check Boxes") self.frame.Show() return True |
Run the code and this will be the result
Subscribe and Get Free Video Courses & Articles in your Email