python
  1. python-radiobutton

Python Radiobutton

Radiobutton is a graphical user interface element that allows users to select only one item from a list of options. In Python, we can implement Radiobutton using the tkinter module.

Syntax

Radiobutton(root, text, variable, value)
  • root: The root or parent window.
  • text: The text to be displayed next to the Radiobutton.
  • variable: A variable to hold the selected value.
  • value: The value of the Radiobutton.

Example

Let's create a simple program to demonstrate the usage of Radiobutton in Python:

from tkinter import *

root = Tk()
root.geometry("300x150")

frame = Frame(root)
frame.pack(pady=10)

label = Label(frame, text="Choose a language:")
label.pack(side=LEFT)

selected_language = StringVar()
language1 = Radiobutton(frame, text="Python", variable=selected_language, value="Python")
language1.pack(side=LEFT)
language2 = Radiobutton(frame, text="Java", variable=selected_language, value="Java")
language2.pack(side=LEFT)
language3 = Radiobutton(frame, text="JavaScript", variable=selected_language, value="JavaScript")
language3.pack(side=LEFT)

def chosen_language():
    label2 = Label(root, text="You chose " + selected_language.get(), font=("Arial", 14))
    label2.pack(pady=10)

button = Button(root, text="Choose", command=chosen_language)
button.pack()

root.mainloop()

Explanation

In the above program, we first import the tkinter module and create a parent window called root. We set its geometry to 300x150.

Then, we create a frame inside root for our Radiobutton options and label.

We create a Label widget to display the prompt "Choose a language:". We also create a StringVar to store the value of the selected Radiobutton.

We create three Radiobuttons, one for each language option, and assign their values as "Python", "Java", and "JavaScript". We also assign the StringVar we created earlier as the variable for the Radiobutton group.

We create a function chosen_language() which outputs a label displaying which language was selected.

Finally, we create a Button widget and assign it to the function chosen_language(). We pack the Button inside the parent window and execute the root.mainloop() method to start the GUI event loop.

Output

When we run the above program, we get a window that displays the Radiobutton choices and a "Choose" button. Choosing a language and clicking on the "Choose" button displays a label showing the selected language.

Python Radiobutton Output

Use

Radiobuttons are often used in GUI applications to allow users to select one option from a list of choices.

Important Points

  • Radiobuttons can only allow users to select one option from a list of choices.
  • The variable for a Radiobutton group should be a tkinter StringVar, IntVar, DoubleVar, or BooleanVar object.
  • The value of each Radiobutton should be unique within the Radiobutton group.

Summary

Radiobuttons in Python with tkinter allow users to select one option from a list of choices. We can use the tkinter module to create Radiobuttons, with a parent window, a label, a variable to hold the selected value, and a value for each Radiobutton.

Published on: