Radiobutton Tkinter | Python 3
In this tutorial, we are going to see how to use Radiobutton widget in Tkinter. The Radiobutton is a standard Tkinter widget used to implement MCQ button, which is a way to offer many possible selections to the user and allows the user to choose only one.
Radio buttons can contain text or images, and you can associate a Python function or method with each button. When the button is clicked, Tkinter automatically calls that function or method.
To implement this feature, each group of radio buttons must be associated with the same variable, and each button must symbolize a single value. You can use the Tab key to switch from one radion button to another.
[st_adsense]
Syntax:
Here is the syntax to create this widget:
btn = Radiobutton ( master, option = value, ... )
Parameters:
- master : This represents the parent window.
- options : Options can be used as comma separated key-value pairs. See the following example:
[st_adsense]
Example:
from tkinter import * def sel(): selected = "You have selected : " + v.get() label.config(text = selected) gui = Tk() v = StringVar() v.set("Python") # initialize r1 = Radiobutton(gui, text="Python", variable=v, value="Python", command=sel) r1.pack(anchor = W) r2 = Radiobutton(gui, text="Java", variable=v, value="Java", command=sel) r2.pack(anchor = W) r3 = Radiobutton(gui, text="PHP", variable=v, value="PHP", command=sel) r3.pack(anchor = W) label = Label(gui) label.pack() gui.mainloop()
Output:
[st_adsense]