In this Python GUI article i want to show you Creating RadioButton in wxPython. first let’s talk about wxPython RadioButton.
What is wxPython RadioButton?
In wxPython RadioButton item is a button which usually denotes one of several mutually exclusive options. It has a text label next to a (usually) round button.so you can create a group of mutually-exclusive radio buttons. by specifying RB_GROUP for the first in the group.
wxPython RadioButton class supports the following styles:
- wx.RB_GROUP: Marks the beginning of a new group of radio buttons.
- wx.RB_SINGLE: In some circumstances, radio buttons that are not consecutive siblings trigger a hang bug in Windows (only). If this happens, add this style to mark the button as not belonging to a group, and implement the mutually-exclusive group behaviour yourself.
Python GUI RadioButton in wxPython
Now let’s create our example, This is the complete code for Python GUI Creating RadioButton 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 |
import wx # Define main frame of the application class MyFrame(wx.Frame): def __init__(self, parent, title): # Initialize the 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 a panel and add it to the frame self.panel = MyPanel(self) # Define panel that contains the radio buttons class MyPanel(wx.Panel): def __init__(self, parent): # Initialize the panel super(MyPanel, self).__init__(parent) # Create radio buttons for different options self.rb1 = wx.RadioButton(self, label="Cat", pos=(10, 10), style=wx.RB_GROUP) self.rb2 = wx.RadioButton(self, label="Dog", pos=(10, 40)) self.rb3 = wx.RadioButton(self, label="Tiger", pos=(10, 70)) # Create static text label to display selection self.label = wx.StaticText(self, label="", pos=(10, 100)) # Bind the radio button event to the onRadioButtons method self.Bind(wx.EVT_RADIOBUTTON, self.onRadioButtons) # Event handler for radio button selection def onRadioButtons(self, e): # Get the selected radio button and update the label rb = e.GetEventObject() self.label.SetLabelText("You Have Selected " + rb.GetLabel()) # Define the application class MyApp(wx.App): def OnInit(self): # Create and show the main frame self.frame = MyFrame(parent=None, title="Codeloop.org - RadioButton") self.frame.Show() return True # Start the application 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 |
class MyFrame(wx.Frame): def __init__(self, parent, title): super(MyFrame, self).__init__(parent, title =title, size = (600,400)) |
After that we have MyPanel class that inherits 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 three RadioButton in this class.
1 2 3 4 5 6 7 8 9 10 11 12 |
class MyPanel(wx.Panel): def __init__(self, parent): super(MyPanel, self).__init__(parent) self.rb1 = wx.RadioButton(self, label = "Cat", pos = (10,10), style = wx.RB_GROUP) self.rb2 = wx.RadioButton(self, label="Dog", pos=(10, 40)) self.rb2 = wx.RadioButton(self, label="Tiger", pos=(10, 70)) self.label = wx.StaticText(self, label = "", pos = (10,100)) self.Bind(wx.EVT_RADIOBUTTON, self.onRadioButtons) |
So in our MyPanel class these codes are for creating RadioButton in wxPython.
1 2 3 |
self.rb1 = wx.RadioButton(self, label = "Cat", pos = (10,10), style = wx.RB_GROUP) self.rb2 = wx.RadioButton(self, label="Dog", pos=(10, 40)) self.rb2 = wx.RadioButton(self, label="Tiger", pos=(10, 70)) |
This is our StaticText that we will use this when we do binding for our RadioButton.
1 |
self.label = wx.StaticText(self, label = "", pos = (10,100)) |
And this is the binding process. you can see that we have bonded the RadioButton with our onRadioButton method.
1 |
self.Bind(wx.EVT_RADIOBUTTON, self.onRadioButtons) |
This is the event method for our RadioButton
1 2 3 |
def onRadioButtons(self, e): rb = e.GetEventObject() self.label.SetLabelText("You Have Selected " + rb.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 6 7 8 9 10 |
class MyApp(wx.App): def OnInit(self): self.frame = MyFrame(parent=None, title="Codeloop.org - RadioButton") self.frame.Show() return True app = MyApp() app.MainLoop() |
Run the code this will be the result
FAQs:
How to use radio button in Python?
Using radio buttons in Python typically involves using a GUI library such as Tkinter, PyQt, or wxPython. This is a general overview of how to use radio buttons in Python using wxPython:
- Import the wxPython library: Begin by importing the wxPython library.
- Create a frame: Define a frame where you want to place your radio buttons.
- Create radio buttons: Instantiate wx.RadioButton objects with appropriate labels and options.
- Create event handlers: Define event handlers to respond to radio button selection changes.
- Bind events: Bind the event handlers to the radio buttons.
- Display the frame: Show the frame to display the radio buttons and interact with them.
How to Install wxPython in Windows?
You can install wxPython on Windows using pip, Anaconda or by downloading pre-built binaries from the wxPython website. To install via pip, open the command prompt and run this command.
1 |
pip install wxPython |
How to Install wxPython in Windows?
Installation on Linux depends on different distribution. You can use your package manager to install wxPython if it’s available in the repository. For example, on Ubuntu, you can use this command.
1 |
sudo apt-get install python-wxgtk4.0 |
Also you can install from source or using pip.
How to Install wxPython in Mac?
You can install wxPython on macOS using pip or by downloading pre-built binaries. To install via pip, open the terminal and run this command.
1 |
pip install wxPython |
Subscribe and Get Free Video Courses & Articles in your Email