Python Tkinter Radiobutton

Python Tkinter Radiobutton

Python Tkinter Radiobutton

Interested in above project ,Click Below
WhatsApp
Telegram
LinkedIn

Python Tkinter Radiobutton

In Python GUI programming with Tkinter, the Radiobutton widget is used to implement a one-of-many selection mechanism. This means users can choose only one option from a given set of multiple choices. Each Radiobutton is associated with a variable that keeps track of the selected option.

Radiobuttons can display text, multiple lines of text, or even images, making them highly versatile. Additionally, you can associate different methods with each Radiobutton to trigger specific actions upon selection.

Complete Python Course with Advance topics:-Click here

Syntax

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

w = Radiobutton(top, options)

Here, top represents the parent window, and options are various configuration options to customize the widget’s behavior and appearance.

Tkinter Radiobutton Options

The following table lists the available options for the Radiobutton widget:

SNOptionDescription
1activebackgroundBackground color when the widget is focused.
2activeforegroundText color when the widget is focused.
3anchorDefines text alignment within the widget. Default is CENTER.
4bgBackground color of the widget.
5bitmapDisplays a graphical image instead of text.
6borderwidthSets the border size.
7commandCalls a function when the state of the Radiobutton changes.
8cursorChanges the mouse pointer when hovering over the button.
9fontDefines the font type and size.
10fgForeground color of the text.
11heightSpecifies the height in terms of lines (not pixels).
12highlightcolorColor of the highlight when the widget has focus.
13highlightbackgroundHighlight color when the widget is not focused.
14imageDisplays an image instead of text.
15justifyAligns multi-line text (LEFT, CENTER, RIGHT).
16padxHorizontal padding.
17padyVertical padding.
18reliefType of border (e.g., FLAT, RAISED, SUNKEN).
19selectcolorColor of the button when selected.
20selectimageDisplays a different image when selected.
21stateSets the button state (NORMAL, DISABLED).
22textDisplays text on the Radiobutton.
23textvariableString variable used to update the displayed text dynamically.
24underlineSpecifies the character position to be underlined.
25valueAssigns a unique value to each Radiobutton.
26variableA control variable to track the selected option.
27widthSets the horizontal size in terms of characters.
28wraplengthWraps text into multiple lines if it exceeds the set limit.

Tkinter Radiobutton Methods

The following methods are available for the Radiobutton widget:

See also  Python sys Module : A Comprehensive Guide

SNMethodDescription
1deselect()Deselects the Radiobutton.
2flash()Flashes the Radiobutton between active and normal states.
3invoke()Calls the function associated with the command option.
4select()Selects the Radiobutton.

Example: Implementing Radiobutton in Tkinter

Below is a Python Tkinter example demonstrating how to use Radiobuttons to let users select their favorite programming language:

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:

When you run the above code, a GUI window appears with three radio buttons labeled Python, Java, and C++. When the user selects one, the corresponding text is displayed below the buttons.

Python Tkinter Radiobutton

Download New Real Time Projects :-Click here
Complete Advance AI topics:- CLICK HERE

Conclusion

The Tkinter Radiobutton is an essential widget for applications that require a user to select one option from multiple choices. With the ability to customize its appearance and behavior using different options and methods, it is a powerful tool in GUI development.

By implementing Radiobuttons in your projects, you can enhance user interaction and create more intuitive interfaces. Experiment with different options and integrate them into your Tkinter applications for an enriched user experience.

For more Python tutorials, stay tuned to UpdateGadh!


tkinter radiobutton command
radio button tkinter – get value
tkinter radiobutton default select
tkinter radiobutton documentation
checkbox radio button in python
radio button python example
python tkinter checkbox
tkinter radiobutton group
python tkinter checkbutton
python tkinter button
python tkinter radiobutton
python tkinter radiobutton example
python tkinter radiobutton group
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

    🎓 Need Complete Final Year Project?

    Get Source Code + Report + PPT + Viva Questions (Instant Access)

    🛒 Visit UpdateGadh Store →
    💬 Chat Now