Easily Master Radiobuttons in Python Tkinter GUI 🟢

Posted by



Introduction:

In this tutorial, we will learn how to create a simple GUI application using Python’s tkinter library. Specifically, we will focus on creating radio buttons, which allow users to select one option from a group of options. Radio buttons are a common GUI element that can be used to present users with a set of choices and allow them to make a selection.

Prerequisites:
Before getting started with this tutorial, you should have basic knowledge of Python programming language and some familiarity with tkinter library. If you are new to tkinter, I recommend going through some introductory tutorials to get familiar with the basics of creating GUI applications using tkinter.

Step 1: Import the necessary modules
To get started, you will need to import the tkinter module and the tkinter’s messagebox module. You can do this by adding the following lines of code in your Python script:

import tkinter as tk
from tkinter import messagebox

Step 2: Create the main window
Next, create the main window for your application. You can do this by creating an instance of the tk.Tk class and assigning it to a variable, like so:

root = tk.Tk()
root.title(“Radio Buttons Tutorial”)

Step 3: Create the radio buttons
Now, let’s create some radio buttons for the user to choose from. You can create radio buttons by creating instances of the tk.Radiobutton class and passing the necessary parameters. In this example, we will create three radio buttons:

radio_var = tk.StringVar()
radio_var.set(“Option 1″)

radio_button1 = tk.Radiobutton(root, text=”Option 1″, variable=radio_var, value=”Option 1″)
radio_button2 = tk.Radiobutton(root, text=”Option 2″, variable=radio_var, value=”Option 2″)
radio_button3 = tk.Radiobutton(root, text=”Option 3″, variable=radio_var, value=”Option 3”)

radio_button1.pack()
radio_button2.pack()
radio_button3.pack()

Step 4: Create a function to display the selected option
Now, let’s create a function that will display a message box with the selected option when the user clicks a button. You can do this by creating a function like the one below:

def display_selected_option():
selected_option = radio_var.get()
messagebox.showinfo(“Selected Option”, f”You have selected: {selected_option}”)

Step 5: Create a button to trigger the function
Now, let’s create a button that will trigger the display_selected_option function when clicked. You can create a button by creating an instance of the tk.Button class and passing the necessary parameters:

btn = tk.Button(root, text=”Show Selected Option”, command=display_selected_option)
btn.pack()

Step 6: Run the main loop
Finally, run the main loop of the application by calling the mainloop() method on the root window:

root.mainloop()

Conclusion:
In this tutorial, we learned how to create radio buttons in a tkinter GUI application using Python. We created a simple application that allows users to select one option from a group of options using radio buttons. We also created a button that displays the selected option in a message box when clicked. I hope this tutorial was helpful in getting you started with tkinter GUI programming in Python.

0 0 votes
Article Rating

Leave a Reply

34 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@BroCodez
16 days ago

# radio button = similar to checkbox, but you can only select one from a group

from tkinter import *

food = ["pizza","hamburger","hotdog"]

def order():

if(x.get()==0):

print("You ordered pizza!")

elif(x.get()==1):

print("You ordered a hamburger!")

elif(x.get()==2):

print("You ordered a hotdog!")

else:

print("huh?")

window = Tk()

pizzaImage = PhotoImage(file='pizza.png')

hamburgerImage = PhotoImage(file='hamburger.png')

hotdogImage = PhotoImage(file='hotdog.png')

foodImages = [pizzaImage,hamburgerImage,hotdogImage]

x = IntVar()

for index in range(len(food)):

radiobutton = Radiobutton(window,

text=food[index], #adds text to radio buttons

variable=x, #groups radiobuttons together if they share the same variable

value=index, #assigns each radiobutton a different value

padx = 25, #adds padding on x-axis

font=("Impact",50),

image = foodImages[index], #adds image to radiobutton

compound = 'left', #adds image & text (left-side)

#indicatoron=0, #eliminate circle indicators

#width = 375, #sets width of radio buttons

command=order #set command of radiobutton to function

)

radiobutton.pack(anchor=W)

window.mainloop()

@hydarhydar2338
16 days ago

@hydarhydar2338
16 days ago

@SandeepBudha-h6y
16 days ago

Love you bro❤😂

@aquasepwaterengineering4242
16 days ago

very nice way of teaching. Thanks BRO

@rubenc4696
16 days ago

thanks

@hamzazad5258
16 days ago

every time i add images its gets all messed up!

@haydenmcelmon6504
16 days ago

Hey guys I know this is a comment on a 3-year old post but just want some help. I wrote all the code he used, then I wanted to change the background of the radio button to be blue, so I wrote "bg="blue"" after the width and before the command in the radio button variable. For some reason when I run the code only the hamburger and hot dog changes background to blue and the pizza's background stays as white. I asked ChatGPT 4.0 what could be the issue and they don't have a concrete answer.

Are there any coding connoisseurs that might be able to identify my issue?

@linussandwich8915
16 days ago

rahhhhh best code teacher

@artur0y95
16 days ago

I used f-string for the order function. It's even simplier and all in one line of code.
def order():

print(f"You ordered {food[x.get()]}!")

@kapibara2440
16 days ago

Great stuff from the Bro❤❤❤

@thePavuk
16 days ago

for me, even width 75 is very large. like 4-5 times more then in example. My screen is 2560×1440.

@truquichan
16 days ago

Great!

@arpanshah355
16 days ago

A simpler way to write his order function is:
def order():

order = ["You bought the pizza!", "You bought the hamburger!", "You bought the hotdog!"]

print(order[x.get()])

@androtech1285
16 days ago

Great work

@Blazer50085
16 days ago

Thanks this tutorial was really helpful

@gustavoaponte1814
16 days ago

meow meow meow~!

@bbong76
16 days ago

Hi, just a quick question!
When I follow the tutorial, I notice that the default selection is the first one.
Is there anyway to deselect that?
Thanks

@activechaos128
16 days ago

why can I not find a png image that works?

@piotrkopcewicz5227
16 days ago

Great !!

34
0
Would love your thoughts, please comment.x
()
x