Python

Python Tkinter Radiobutton

Python Tkinter Radiobutton
Python Tkinter Radiobutton

Python Tkinter Radiobutton

In Python GUI development, the Tkinter Radiobutton widget is used when an application needs to let the user choose one option from a group of choices. Unlike a checkbox, where multiple items can be selected, Radiobuttons are generally connected to the same control variable so that selecting one option automatically represents the current choice.

Each Radiobutton can be assigned a unique value, while a shared variable keeps track of which option the user has selected. Radiobuttons can contain text, multiple lines of text, or images, which makes them useful for creating interactive and user-friendly Tkinter applications.

You can also connect a function to a Radiobutton using the command option. This allows the application to perform a specific action whenever the user selects an option.

Python Tkinter Radiobutton
Python Tkinter Radiobutton

Complete Advance AI Topics: Click Here
SQL Tutorial:
Click Here

Syntax

The basic syntax for creating a Radiobutton widget in Tkinter is:

w = Radiobutton(top, options)

Here, top represents the parent or main window in which the Radiobutton will be placed. The options are configuration properties used to control its appearance and behavior.

Tkinter Radiobutton Options

Tkinter provides several options for customizing a Radiobutton. These options can control its color, size, text, state, alignment, selection behavior, and other properties.

SNOptionDescription
1activebackgroundSets the background color when the widget is active or focused.
2activeforegroundSets the text color when the widget is active.
3anchorControls text positioning inside the widget. The default value is CENTER.
4bgDefines the background color of the Radiobutton.
5bitmapDisplays a bitmap image instead of regular text.
6borderwidthSpecifies the width of the widget border.
7commandExecutes a function when the Radiobutton selection changes.
8cursorChanges the mouse pointer when it is positioned over the widget.
9fontSpecifies the font style and size used by the widget.
10fgSets the foreground or text color.
11heightDefines the widget height in terms of text lines.
12highlightcolorSpecifies the highlight color when the widget receives focus.
13highlightbackgroundDefines the highlight color when the widget does not have focus.
14imageDisplays an image instead of text.
15justifyControls the alignment of multiple lines of text using LEFT, CENTER, or RIGHT.
16padxAdds horizontal space inside the widget.
17padyAdds vertical space inside the widget.
18reliefControls the border appearance, such as FLAT, RAISED, or SUNKEN.
19selectcolorDefines the color shown when the Radiobutton is selected.
20selectimageDisplays an alternate image when the Radiobutton is selected.
21stateControls the widget state, such as NORMAL or DISABLED.
22textSpecifies the text displayed beside the Radiobutton.
23textvariableUses a string variable to update the displayed text dynamically.
24underlineSpecifies the character position that should be underlined.
25valueAssigns a unique value to the Radiobutton.
26variableSpecifies the control variable used to track the selected option.
27widthSets the horizontal size of the widget in characters.
28wraplengthWraps long text into multiple lines after the specified length.

Tkinter Radiobutton Methods

The Radiobutton widget also provides methods that can be used to control its selection state and trigger its associated command.

SNMethodDescription
1deselect()Removes the selection from the Radiobutton.
2flash()Flashes the Radiobutton between its active and normal states.
3invoke()Executes the function associated with the command option.
4select()Selects the Radiobutton.

Example: Implementing Radiobutton in Tkinter

The following example creates three Radiobuttons for selecting a favorite programming language. An IntVar is shared by all three buttons, and each button receives a different value. When the user makes a selection, the selection() function displays the selected value.

from tkinter import *

def selection():
    selected_option = "You selected the option " + str(radio.get())
    label.config(text=selected_option)

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

radio = IntVar()

lbl = Label(text="Favourite programming language:")
lbl.pack()

R1 = Radiobutton(
    top,
    text="Python",
    variable=radio,
    value=1,
    command=selection
)
R1.pack(anchor=W)

R2 = Radiobutton(
    top,
    text="Java",
    variable=radio,
    value=2,
    command=selection
)
R2.pack(anchor=W)

R3 = Radiobutton(
    top,
    text="C++",
    variable=radio,
    value=3,
    command=selection
)
R3.pack(anchor=W)

label = Label(top)
label.pack()

top.mainloop()

Output

After running the program, a Tkinter window appears containing three choices: Python, Java, and C++. Because all three Radiobuttons use the same radio variable, the user can select only one option at a time.

When an option is selected, the command calls the selection() function. The selected value is retrieved using radio.get() and displayed in the label below the Radiobuttons.

YT:- DecodeIT

Frequently Asked Questions (FAQs)

1. What is a Radiobutton in Tkinter?
A Tkinter Radiobutton is a GUI widget that allows the user to select one option from a group of multiple choices.

2. How do you create a Radiobutton in Python Tkinter?
You can create a Radiobutton using the following syntax:

Radiobutton(parent, options)

3. How does Tkinter Radiobutton allow only one selection?
Multiple Radiobuttons are connected to the same control variable, such as IntVar() or StringVar(). Each button has a different value, so selecting one option updates the shared variable.

4. How can I get the selected Radiobutton value?
Use the get() method of the associated variable:

radio.get()

5. What is the use of the variable option in Radiobutton?
The variable option stores and tracks the current selection of the Radiobutton group.

6. What is the purpose of the value option?
The value option assigns a unique value to each Radiobutton. When a button is selected, its value is stored in the associated control variable.

7. How can I execute a function when a Radiobutton is selected?
Use the command option:

Radiobutton(root, text="Python", command=selection)

8. Can Tkinter Radiobutton display images?
Yes. The image option can be used to display an image instead of regular text. The selectimage option can display a different image when the Radiobutton is selected.

9. What are the main Radiobutton methods in Tkinter?
The main methods are select(), deselect(), flash(), and invoke().

10. What is the difference between a Tkinter Radiobutton and Checkbutton?
Radiobuttons are generally used when the user should select one option from a group, while Checkbuttons are used when multiple independent choices can be selected.

Conclusion

The Python Tkinter Radiobutton is an important widget for applications where users need to select one item from a predefined group of choices. By using the variable and value options together, developers can easily manage the selected option.

Tkinter Radiobuttons also provide numerous configuration options and useful methods for controlling their appearance and behavior. By combining Radiobuttons with functions, variables, labels, and other Tkinter widgets, you can build more interactive and intuitive Python GUI applications.

Keywords: Python Tkinter Radiobutton, tkinter radiobutton command, radio button tkinter get value, tkinter radiobutton default select, tkinter radiobutton documentation, radio button Python example, Python tkinter checkbox, tkinter radiobutton group, Python tkinter checkbutton, Python tkinter button, python tkinter radiobutton example, python tkinter radiobutton command, python tkinter radiobutton get value, python tkinter radiobutton set value, python tkinter radiobutton select, python tkinter radiobutton grid, python tkinter radiobutton default value

Source Code Available

Interested in This Project?

Get the complete source code for this project at a very affordable price — perfect for your portfolio, college submission, or learning. Message us on WhatsApp and we'll get back to you instantly!

Full source code included Step-by-step setup guide Instant delivery on WhatsApp Instant reply on WhatsApp
Chat on WhatsApp

We usually reply within a few minutes

Leave a Reply

Your email address will not be published. Required fields are marked *

Chat with us