In this Python GUI article iam going to talk about creating RadioBox in wxPython. now let’s talk about wxPython RadioBox.
What is RadioBox in wxPython?
In wxPython RadioBox is a type of widget that allows users to select a single option from a list of mutually exclusive choices. It is typically presented as a group of radio buttons, where only one button can be selected at a time.
RadioBox class supports the following styles:
- wx.RA_SPECIFY_ROWS: The major dimension parameter refers to the maximum number of rows.
- wx.RA_SPECIFY_COLS: The major dimension parameter refers to the maximum number of columns.
Python GUI RadioBox in wxPython
Now let’s create our example, so this is the complete code about Python GUI RadioBox 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 |
import wx class MyFrame(wx.Frame): def __init__(self, parent, title): # Initialize frame super(MyFrame, self).__init__(parent, title=title, size=(600, 400)) # Set window icon icon = wx.Icon("codeloop.png", wx.BITMAP_TYPE_PNG) self.SetIcon(icon) # Create panel self.panel = MyPanel(self) class MyPanel(wx.Panel): def __init__(self, parent): # Initialize the panel super(MyPanel, self).__init__(parent) # Define list of items for the RadioBox lblist = ["Cat", "Dog", "Tiger"] # Create RadioBox with the list of items self.rbox = wx.RadioBox(self, label="Radio Box", pos=(50, 50), choices=lblist, style=wx.RA_SPECIFY_ROWS) # Create a static text label self.label = wx.StaticText(self, label="", pos=(50, 150)) # Bind radio box event to the onRadioBox method self.Bind(wx.EVT_RADIOBOX, self.onRadioBox) def onRadioBox(self, event): # Update label with the selected item from the radio box rboxSelection = self.rbox.GetStringSelection() self.label.SetLabelText("You Have Selected " + rboxSelection) class MyApp(wx.App): def OnInit(self): # Initialize application self.frame = MyFrame(parent=None, title="Codeloop.org - wxPython RadioBox") # Show frame self.frame.Show() return True # Start application event loop app = MyApp() app.MainLoop() |
This is class is MyFrame class that inherits from wx.Frame and it is a top level window that contains our panel.
1 2 3 4 5 6 7 8 |
class MyFrame(wx.Frame): def __init__(self, parent, title): super(MyFrame, self).__init__(parent, title =title, size = (600,400)) self.panel = MyPanel(self) |
After that we have created MyPanel class that extends from wx.Panel and it is the place that we create our widgets and layouts in this class, you can see that we have created our RadioBox in this class .
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
class MyPanel(wx.Panel): def __init__(self, parent): super(MyPanel, self).__init__(parent) lblist = ["Cat", "Dog", "Tiger"] self.rbox = wx.RadioBox(self, label = "Radio Box", pos = (50,50), choices = lblist, style = wx.RA_SPECIFY_ROWS) #style = wx.RA_SPECIFY_COLS self.label = wx.StaticText(self, label = "", pos = (50,150)) self.Bind(wx.EVT_RADIOBOX, self.onRadioBox) |
This is the RadioBox creation code in wxPython. you need to give a label, pos, the choices that we have created is a list of them in python and the last one is the style.
1 |
self.rbox = wx.RadioBox(self, label = "Radio Box", pos = (50,50), choices = lblist, style = wx.RA_SPECIFY_ROWS) |
And this is the label that we have created.
1 |
self.label = wx.StaticText(self, label = "", pos = (50,150)) |
We have also an event method for binding that method with our RadioBox.
1 2 3 4 |
def onRadioBox(self, event): rboxSelection = self.rbox.GetStringSelection() self.label.SetLabelText("You Have Selected " + rboxSelection) |
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="Radio Box") self.frame.Show() return True app = MyApp() app.MainLoop() |
Run the code this will be the result
FAQs:
wxPython is a Python wrapper for the wxWidgets C++ library, Using wxPython you can create cross-platform desktop applications with native look and feel.
How to install Python wx?
1 |
pip install wxPython |
How to install wxPython with Anaconda/Miniconda?
If you’re using Anaconda or Miniconda, you can install wxPython using conda package manager. Run the following command in your terminal or Anaconda prompt:
1 |
conda install -c anaconda wxpython |
Subscribe and Get Free Video Courses & Articles in your Email