Python Tkinter Radiobutton – How to use

In this Python tutorial, we will learn about Python Tkinter RadioButton. Also, we will cover these topics:

  • Tkinter RadioButton Python
  • Tkinter RadioButton get value
  • Tkinter RadioButton command
  • Tkinter RadioButton variable
  • Tkinter RadioBbutton horizontal
  • Tkinter RadioButton grid
  • Tkinter RadioButton default
  • Tkinter RadioButton group
  • Tkinter RadioButton example

Python Tkinter RadioButton

Let us discuss first what is a Python Tkinter RadioButton.

  • In Python GUI programming, we can use Radio buttons to make a choice.
  • It supports a single selection from multiple radio buttons.
  • While filling any form, you must have encountered the gender selection option. Wherein you are allowed to select only one option. And that’s is called a radio button.

Example:

Here you can see an example of a Python radio button.

python tkinter radiobutton
python tkinter radiobutton

Tkinter RadioButton get value

  • In this section, we will learn how to get the value stored in the radio button in Python
  • In the below example students have a choice to select a stream (science, commerce, arts).
  • the value of radiobutton comes in an integer or string form.
  • In this case value is integer

Code:

from tkinter import *
from tkinter import messagebox

ws = Tk()
ws.title('PythonGuides')
ws.geometry('200x200')

def viewSelected():
    choice  = var.get()
    if choice == 1:
       output = "Science"

    elif choice == 2:
       output =  "Commerce"
    
    elif choice == 3:
       output =  "Arts"
    else:
        output = "Invalid selection"

    return messagebox.showinfo('PythonGuides', f'You Selected {output}.')
    
var = IntVar()
Radiobutton(ws, text="Science", variable=var, value=1, command=viewSelected).pack()
Radiobutton(ws, text="Commerce", variable=var, value=2, command=viewSelected).pack()
Radiobutton(ws, text="Arts", variable=var, value=3, command=viewSelected).pack()

ws.mainloop()

Output:

In this output, user selected ‘commerce option’. So the prompt tells him that he has selected ‘Commerce’.

python tkinter radiobutton get
python tkinter radiobutton

Also, you may like, BMI Calculator Using Python Tkinter.

READ:  How to use Python While with Assignment[4 Examples]

Python Tkinter RadioButton command

  • Python Tkinter RadioButton commands we can use to trigger a function or method
  • In other words, what will happen, when the radiobutton is selected
  • Like in the previous section when radiobutton has clicked a prompt appeared with the selected option.

Code:

from tkinter import *

ws =Tk()
ws.title("PythonGuides")
ws.geometry('200x200')

def sayHello():
    return Label(ws, text="Hello user").pack()

var= IntVar()
Radiobutton(ws, text='Python', variable=var, value=1, command=sayHello).pack()
Radiobutton(ws, text='Guide', variable=var, value=2, command=sayHello).pack()
ws.mainloop()

Output:

In this output, function is created that will print ‘hello world’. When user clicks on any of the radiobutton than that function is triggered & output is displayed. command keyword is used to pass the function in the radiobutton.

python tkinter radiobutton command
Python Tkinter RadioButton command

Tkinter RadioButton variable

  • Radiobutton variable controls the type of value.
  • If the variable is StringVar() that means the value will be a string type.
  • If the variable is IntVar() then the value will be an integer type.
  • value in the variable is used to target the radiobutton.

Read: Python Tkinter Text Box Widget

Tkinter RadioButton horizontal

  • Python Radio buttons can be aligned in horizontal or vertical form
  • In this section, we will learn how to set radio button in a horizontal form

Code:

from tkinter import *

ws = Tk()
ws.title("PythonGuides")
ws.geometry('200x200')

var = IntVar()
    

cold_bev = Radiobutton(ws, text="Cold Beverage", variable=var, value=1).grid(row=0, column=1)
hot_bev = Radiobutton(ws, text="Hot Beverage", variable=var, value=2).grid(row=0, column=2)


ws.mainloop()

Output:

In this output, radiobuttons are aligned horizontally. We have used grid to do so. Here row = 0 and column = 1 for ‘cold Beverage’ and column = 2 for ‘Hot Beverage’.

python tkinter radiobutton horizontal
Tkinter RadioButton horizontal

Python Tkinter RadioButton grid

  • Grid is a positioning manager tool
  • It positions the widget in a row and column format.
  • the position starts from top-left corner
  • that means, row=0 and column=0

Code:

from tkinter import *

ws = Tk()
ws.title('PythonGuides')
ws.geometry('200x200')

var = IntVar()
frame = LabelFrame(ws, text='Choose wisely')
frame.pack(pady=10)

Radiobutton(frame, text='Army', variable=var, value=1).grid(row=0, column=1)
Radiobutton(frame, text="Airforce", variable=var, value=2).grid(row=0, column=2)
Radiobutton(frame, text="Navy", variable=var, value=3).grid(row=1, columnspan=3)

ws.mainloop()

Output:

READ:  How to Convert Python Dictionary to Pandas DataFrame

In this output, we can see that radiobuttons are positioned using grid.

python tkinter radiobutton grid
Tkinter RadioButton grid

Tkinter RadioButton default in Python

We can also set a default option selected in the Tikinter Radio button in Python.

  • Default helps in providing some value when the user does not provide any value.
  • A default radio button is selected or accepted when a user either clicks on the reset button or does not provide any input.

Code:

from tkinter import *

ws = Tk()
ws.title("PythonGuides")
ws.geometry('200x250')
ws.configure(bg='#dddddd')

def reset():
    return var.set(5)

var = IntVar()
frame = LabelFrame(ws, text='Select Beverage', padx=50, bg= '#dddddd')
frame.pack()
Radiobutton(frame, text="Tea", variable=var, value=1 ).pack(anchor=W)
Radiobutton(frame, text="Coffee", variable=var, value=2).pack(anchor=W)
Radiobutton(frame, text="Cola", variable=var, value=3).pack(anchor=W)
Radiobutton(frame, text="Wine", variable=var, value=4).pack(anchor=W)
none_Rb = Radiobutton(frame, text="None", variable=var, value=5).pack(anchor=W)


Button(ws, text='reset', command=reset, padx=20, pady=5).pack(pady=10)

ws.mainloop()

Output:

In this output, user can choose any one option. No matter what ever option is selected but if he clicks on reset then the radiobutton will set to default i.e none.

python tkinter radiobutton default
Tkinter RadioButton default

Tkinter RadioButton group

  • Creating a cluster of the radio button is called group
  • The Radiobutton of the same group has the same variable name.
  • In a form, there are multiple radio buttons but they all may not save the same purpose.
  • so to categorize them they are grouped.
  • like gender will have male, the female, stream will have, science commerce, etc.

Code:

from tkinter import *

ws = Tk()
ws.title("PythonGuies")
ws.geometry('200x200')

frame1 = LabelFrame(ws, text='Gender')
frame1.grid(row=1, column=1, padx=10)

frame2 = LabelFrame(ws, text='Discipline')
frame2.grid(row=1, column=2)

group_1 = IntVar()
group_2 = IntVar()
Radiobutton(frame1, text='Female', variable=group_1, value=1).pack()
Radiobutton(frame1, text='Male', variable=group_1, value=2).pack()
Radiobutton(frame2, text='Army', variable=group_2, value=1).pack()
Radiobutton(frame2, text='Navy', variable=group_2, value=2).pack()

ws.mainloop()

Output:

In this output, 2 groups are created for radiobutton. Radiobutton inside the ‘Gender‘ won’t affect radiobuttons in ‘Discipline‘. Each group is independent.

Tkinter RadioButton group
Tkinter RadioButton group

Tkinter RadioButton example

  • In this example, we will write code for an entire application that will include radiobutton.
  • The application is a form wherein the user will provide information
  • Submit button will prompt with the title, name, and a message.
  • These titles (Mr. or Ms.) will be defined as per the choice made in radio buttons.
READ:  Training Neural Network in TensorFlow

Code:

from tkinter import *
from tkinter import messagebox

ws =Tk()
ws.title('PythonGuides')
ws.geometry('250x300')

def selection():
    choice = var.get()
    if choice == 1:
        m = 'Ms.'
    elif choice == 2:
        m = 'Mr.'
    elif choice == 3:
        pass
    return m

def submit():
    name = name_Tf.get()
    m = selection()
    return messagebox.showinfo('PythonGuides', f'{m} {name}, submitted form.')
    


frame1 = Label(ws)
frame1.pack()
frame2 = LabelFrame(frame1, text='Gender', padx=30, pady=10)

var =IntVar()

Label(frame1, text='Full Name').grid(row=0, column=0, padx=5, pady=5)
Label(frame1, text='Email').grid(row=1, column=0, padx=5, pady=5)
Label(frame1, text='Password').grid(row=2, column=0, padx=5, pady=5)
Radiobutton(frame2, text='Female', variable=var, value=1,command=selection).pack()
Radiobutton(frame2, text='Male', variable=var, value=2,command=selection).pack(anchor=W)
Radiobutton(frame2, text='Others', variable=var, value=3,command=selection).pack()
name_Tf = Entry(frame1)
name_Tf.grid(row=0, column=2)
Entry(frame1).grid(row=1, column=2)
Entry(frame1, show="*").grid(row=2, column=2)
frame2.grid(row=3, columnspan=3,padx=30)
Button(frame1, text="Submit", command=submit, padx=50, pady=5).grid(row=4, columnspan=4, pady=5)

ws.mainloop()

Output:

This is the output of the code, Here Label, entry , radiobutton, frame, labelframe and button widgets are used.

python tkinter radiobutton example
Tkinter RadioButton example

You may like the following Python tutorials:

In this Python tutorial, we learned how to use Python Tkinter radiobutton in GUI programming. Now while designing a screen we can use the radio buttons in Python.

  • Tkinter RadioButton Python
  • Tkinter RadioButton get value
  • Tkinter RadioButton command
  • Tkinter RadioButton variable
  • Tkinter RadioBbutton horizontal
  • Tkinter RadioButton grid
  • Tkinter RadioButton default
  • Tkinter RadioButton group
  • Tkinter RadioButton example