Tkinter radio button widget adds a multiple-choice button and permits the user to pick only one option from a list. Each set of radiobuttons must be associated with the same variable, and each button must represent a single value. To transition from one radionbutton to another, press the Tab key.

What Is a Radiobutton?

A radio button also referred to as an option button, is a Tkinter graphical user interface element that allows users to choose (exactly) one option from a set of options. Text or images can be used in radio buttons. Only a single font can be displayed by the button.

The name “radio buttons” comes from the actual buttons on old radios that were used to choose wave bands or preset radio programs. When such a button is pressed, other buttons pop out, leaving only the pressed button pushed in.

Example 1:

We’ll go over how to make a typical Tkinter Radio Button here. The syntax is nearly comparable to that of Check-buttons. Their control variables are the only thing that differs. The IntVar() function can be used to create a control variable. StringVar() is a variable that can be used to return a string value.

All Radio Buttons must have access to a similar control variable. This is the case because only a single radio button should be chosen at a time (Because of the fact that the control variable can only hold one value simultaneously). It would be no different from a Check Button if each radio button had its own control variable. They must, however, have different values to be distinguished.

Make sure they have separate values even if they have the same control variable. When you get to the next example, you’ll get to know what this is about.

from tkinter import *


r = Tk()


r.geometry(“300×200”)


frame = Frame(r)


frame.pack()


my_var = StringVar()


r_btn1 = Radiobutton(frame, text = “Option Number 1”, variable = my_var,


                    value = 1)


r_btn1.pack(padx = 10, pady = 10)


r_btn2 = Radiobutton(frame, text = “Option Number 2”, variable = my_var,


                     value = 2)


r_btn2.pack(padx = 10, pady = 10)


r.mainloop()

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Tkinter-Radiobutton-1.png" data-lazy- height="283" src="data:image/svg xml,” width=”560″>

Here you can see that two radio buttons are created.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Tkinter-Radiobutton-2.png" data-lazy- height="238" src="data:image/svg xml,” width=”307″>

Example 2:

We’ll look at getting the values of radio buttons next. A Radio Button returns the value that was assigned to it at the time of its creation. The values of Radio Buttons can be retrieved in two ways. The first is prompted by the user.

When you push the submit button, a function is executed that uses the get() method on the shared control variable by the Radio Buttons. When we created the Radio Button, we assigned a value to the option value, returned by the get method.

from tkinter import *

def retrieve():


    print(my_var.get())


r = Tk()


r.geometry(“300×200”)


frame = Frame(r)


frame.pack()


my_var = IntVar()


r_btn1 = Radiobutton(frame, text = “Apple”, variable = my_var,


                   value = 1)


r_btn1.pack(padx = 10, pady = 10)


r_btn2 = Radiobutton(frame, text = “Banana”, variable = my_var,


                     value = 2)


r_btn2.pack(padx = 10, pady = 10)


Btn= Button(frame, text = “Submit”, command = retrieve)


Btn.pack()


r.mainloop()

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Tkinter-Radiobutton-3.png" data-lazy- height="379" src="data:image/svg xml,” width=”559″>

Below is the output screen where you can see the two radiobuttons and one submit button.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Tkinter-Radiobutton-4.png" data-lazy- height="239" src="data:image/svg xml,” width=”305″>

Example 3:

This section will teach us how to access the value saved in the radio button in Python. In the example below, students choose an option from the three displayed options (Red, Green, Blue). The value of a radiobutton can be in the form of an integer or a string.

from tkinter import *

from tkinter import messagebox


ws1 = Tk()


ws1.title(‘Tkinter Radiobutton Example’)


ws1.geometry(‘310×310’)

def viewSelected():


    options  = my_var1.get()


    if options == 1:


      res = “Red”


    elif options == 2:


       res =  “Green”


    elif options == 3:


       res =  “Blue”


    else:


        res = “Invalid option”


    return messagebox.showinfo(‘Tkinter Radiobutton Example’, f‘You Selected {res}.’)


my_var1 = IntVar()


Radiobutton(ws1, text=“Red”, variable=my_var1, value=1, command=viewSelected).pack()


Radiobutton(ws1, text=“Green”, variable=my_var1, value=2, command=viewSelected).pack()


Radiobutton(ws1, text=“Blue”, variable=my_var1, value=3, command=viewSelected).pack()


ws1.mainloop()

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Tkinter-Radiobutton-5.png" data-lazy- height="435" src="data:image/svg xml,” width=”630″>

For example, the user chose the ‘Green’ option in this output. As a result, the prompt informs him that he has chosen ‘Green.’

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Tkinter-Radiobutton-6.png" data-lazy- height="345" src="data:image/svg xml,” width=”316″>

Here you can see the prompt displaying message of what option the user has selected.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Tkinter-Radiobutton-7.png" data-lazy- height="352" src="data:image/svg xml,” width=”323″>

Example 4:

We may utilize Python Tkinter RadioButton commands to call a function or method. What will occur if the radiobutton is pressed, to put it another way. When a radiobutton is clicked, a prompt containing the selected option appears, just as it did in the preceding section.

from tkinter import *


ws1 =Tk()


ws1.title(“Tkinter Radiobutton Example”)


ws1.geometry(‘310×310’)

def sayHello():


    return Label(ws1, text=“Welcome”).pack()


my_var1= IntVar()


Radiobutton(ws1, text=‘Tkinter’, variable=my_var1, value=1, command=sayHello).pack()


Radiobutton(ws1, text=‘Radiobutton’, variable=my_var1, value=2, command=sayHello).pack()


ws1.mainloop()

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Tkinter-Radiobutton-8.png" data-lazy- height="212" src="data:image/svg xml,” width=”630″>

This output has a function that prints ‘Welcome.’ The function is triggered, and the output is displayed when the user picks one of the radiobuttons. The function in the radiobutton is passed via the command keyword.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Tkinter-Radiobutton-9.png" data-lazy- height="345" src="data:image/svg xml,” width=”317″>

When one of the radiobuttons is chosen by the relevant user, the text ‘Welcome’ is shown.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Tkinter-Radiobutton-10.png" data-lazy- height="348" src="data:image/svg xml,” width=”315″>

Example 5:

We can likewise establish a default option for the Tikinter Radio button in Python. When the user does not supply any value, default assists in providing some value. When a relevant user either hits the reset button or does not deliver any input, a default radio button is selected.

from tkinter import *


ws1 = Tk()


ws1.title(“Tkinter Radiobutton Example”)


ws1.geometry(‘310×310’)


ws1.configure(bg=‘#CAA787’)

def reset():


    return my_var1.set(5)


my_var1 = IntVar()


frame1 = LabelFrame(ws1, text=‘Pick your favorite fruit’, padx=70, bg= ‘#F7A963’)


frame1.pack()


Radiobutton(frame1, text=“Apple”, variable=my_var1, value=1 ).pack(anchor=W)


Radiobutton(frame1, text=“Banana”, variable=my_var1, value=2).pack(anchor=W)


Radiobutton(frame1, text=“Coconut”, variable=my_var1, value=3).pack(anchor=W)


Radiobutton(frame1, text=“Mango”, variable=my_var1, value=4).pack(anchor=W)


none_Rb = Radiobutton(frame1, text=“None”, variable=my_var1, value=5).pack(anchor=W)


Button(ws1, text=‘Reset’, command=reset, padx=15, pady=8).pack(pady=8)


ws1.mainloop()

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Tkinter-Radiobutton-11.png" data-lazy- height="345" src="data:image/svg xml,” width=”623″>

The user is allowed to choose any of the options in this output. Regardless of the option is selected, if he clicks on reset, the radiobutton will default to none.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Tkinter-Radiobutton-12.png" data-lazy- height="344" src="data:image/svg xml,” width=”316″>

Example 6:

The term “group” refers to the process of arranging radio buttons in a cluster. The variable name for the same group’s Radiobutton is the same. There may be several radio buttons on a form, but they may not all save the same information. As a result, they’ve been put in different categories to make it easier to categorize them.

from tkinter import *


ws1 = Tk()


ws1.title(“Tkinter Radiobutton Example”)


ws1.geometry(‘310×310’)


frame_one = LabelFrame(ws1, text=‘Want to Place Order?’)


frame_one.grid(row=1, column=1, padx=10)


frame_two = LabelFrame(ws1, text=‘Payment Method’)


frame_two.grid(row=1, column=2)


group_one = IntVar()


group_two = IntVar()


Radiobutton(frame_one, text=‘Yes’, variable=group_one, value=1).pack()


Radiobutton(frame_one, text=‘No’, variable=group_one, value=2).pack()


Radiobutton(frame_two, text=‘Credit/Debit Card’, variable=group_two, value=1).pack()


Radiobutton(frame_two, text=‘Cash on Delivery’, variable=group_two, value=2).pack()


ws1.mainloop()

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Tkinter-Radiobutton-13.png" data-lazy- height="313" src="data:image/svg xml,” width=”611″>

For radiobutton, two groups are formed in this output.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Tkinter-Radiobutton-14.png" data-lazy- height="348" src="data:image/svg xml,” width=”320″>

Conclusion:

We learned about the Radiobutton widget in this tutorial, which provides numerous alternatives from which one can be chosen. This is the most common method when establishing a user form, such as a registration form. We have also mentioned some example programs to elaborate the concept better.

About the author

<img alt="" data-lazy-src="https://secure.gravatar.com/avatar/d014e3711df41253029f4d4199698df8?s=112&r=g" data-lazy- height="112" src="data:image/svg xml,” width=”112″>

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content