Python Tkinter Radiobutton

Created with Sketch.

Python Tkinter Radiobutton

The Radiobutton widget is used to implement one-of-many selection in the python application. It shows multiple choices to the user out of which, the user can select only one out of them. We can associate different methods with each of the radiobutton.

We can display the multiple line text or images on the radiobuttons. To keep track the user’s selection the radiobutton, it is associated with a single variable. Each button displays a single value for that particular variable.

The syntax to use the Radiobutton is given below.

Syntax

  1. w = Radiobutton(top, options)

 

SNOptionDescription
1activebackgroundThe background color of the widget when it has the focus.
2activeforegroundThe font color of the widget text when it has the focus.
3anchorIt represents the exact position of the text within the widget if the widget contains more space than the requirement of the text. The default value is CENTER.
4bgThe background color of the widget.
5bitmapIt is used to display the graphics on the widget. It can be set to any graphical or image object.
6borderwidthIt represents the size of the border.
7commandThis option is set to the procedure which must be called every-time when the state of the radiobutton is changed.
8cursorThe mouse pointer is changed to the specified cursor type. It can be set to the arrow, dot, etc.
9fontIt represents the font type of the widget text.
10fgThe normal foreground color of the widget text.
11heightThe vertical dimension of the widget. It is specified as the number of lines (not pixel).
12highlightcolorIt represents the color of the focus highlight when the widget has the focus.
13highlightbackgroundThe color of the focus highlight when the widget is not having the focus.
14imageIt can be set to an image object if we want to display an image on the radiobutton instead the text.
15justifyIt represents the justification of the multi-line text. It can be set to CENTER(default), LEFT, or RIGHT.
16padxThe horizontal padding of the widget.
17padyThe vertical padding of the widget.
18reliefThe type of the border. The default value is FLAT.
19selectcolorThe color of the radio button when it is selected.
20selectimageThe image to be displayed on the radiobutton when it is selected.
21stateIt represents the state of the radio button. The default state of the Radiobutton is NORMAL. However, we can set this to DISABLED to make the radiobutton unresponsive.
22textThe text to be displayed on the radiobutton.
23textvariableIt is of String type that represents the text displayed by the widget.
24underlineThe default value of this option is -1, however, we can set this option to the number of character which is to be underlined.
25valueThe value of each radiobutton is assigned to the control variable when it is turned on by the user.
26variableIt is the control variable which is used to keep track of the user’s choices. It is shared among all the radiobuttons.
27widthThe horizontal dimension of the widget. It is represented as the number of characters.
28wraplengthWe can wrap the text to the number of lines by setting this option to the desired number so that each line contains only that number of characters.

Methods

The radiobutton widget provides the following methods.

SNMethodDescription
1deselect()It is used to turn of the radiobutton.
2flash()It is used to flash the radiobutton between its active and normal colors few times.
3invoke()It is used to call any procedure associated when the state of a Radiobutton is changed.
4select()It is used to select the radiobutton.

Example

  1. from tkinter import *
  2. def selection():
  3.    selection = “You selected the option “ + str(radio.get())
  4.    label.config(text = selection)
  5. top = Tk()
  6. top.geometry(“300×150”)
  7. radio = IntVar()
  8. lbl = Label(text = “Favourite programming language:”)
  9. lbl.pack()
  10. R1 = Radiobutton(top, text=“C”, variable=radio, value=1,
  11.                   command=selection)
  12. R1.pack( anchor = W )
  13. R2 = Radiobutton(top, text=“C++”, variable=radio, value=2,
  14.                   command=selection)
  15. R2.pack( anchor = W )
  16. R3 = Radiobutton(top, text=“Java”, variable=radio, value=3,
  17.                   command=selection)
  18. R3.pack( anchor = W)
  19. label = Label(top)
  20. label.pack()
  21. top.mainloop()

Output:

Python Tkinter Radiobutton

 

Leave a Reply

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