Python Radio Button - How do i check Radio Button
Published December 16, 2021This widget allows you to present many options for the user to choose from. Unlike checkboxes, the user can only make one choice with this widget. To implement this functionality, each set of radio buttons must be connected with a comparable variable, and a single value must represent each button.
Let's have a look at how you can make this widget using Tkinter.
The Syntax
The syntax for creating Tkinter radio buttons is very easy. The root represents the parent window, and the choices are the various properties required to generate the widget.
w = Radiobutton (root, option) |
The Tkinter Radio Button Options
-
Bg-Specifies the color of the indicator and the label.
-
Cursor- defines how the cursor behaves when it hovers over the radiobutton widget.
-
Typeface-Specifies the message's font.
-
Activebackground- specifies the color of the mouse when hovered over the radiobutton
-
Activeforeground- specifies the foreground color when the cursor is over the widget.
-
Anchor- The radiobutton is positioned on the widget using an anchor. The default setting is CENTER.
-
Bitmap- used to display the monochrome image on the radio button.
-
Command- The command is called every time the user clicks on the radio button.
-
Fg- specifies the foreground.
-
Height- Specifies the height of the radio buttons in terms of the number of lines rather than pixels. The default value is 1.
-
Image- displays image instead of text on the radiobuttons.
-
Padx- indicates the horizontal padding.
-
Pady- defines the vertical padding.
-
Text- specifies texts displayed on the radiobuttons.
-
Takefocus- If the value is true, the widget will allow the input focus.
-
Textvariable-associates the Tkinter variable with the text displayed label that constitutes the StringVar
-
Width- defines the width of the specified character units and not pixels.
-
Wraplength- wraps the text in the number of lines specified.
-
Value-specifies the value of the radio button
Examples of Tkinter Radio Buttons
The following example allows the user s to select their favorite programming language
from tkinter import * def sel(): selection = "Your favourite Programming language is " + str(var.get()) label.config(text = selection) root = Tk() var = IntVar() R1 = Radiobutton(root, text="Python", variable=var, value=1,command=sel) R1.pack( anchor = W ) R2 = Radiobutton(root, text="Java", variable=var, value=2, command=sel) R2.pack( anchor = W ) R3 = Radiobutton(root, text="C++", variable=var, value=3,command=sel) R3.pack( anchor = W) label = Label(root) label.pack() root.mainloop()
|
![]() |
Article Contributed By :
|
|
|
|
527 Views |