In this tutorial, we will learn about Python Tkinter RadioButton. Tkinter RadioButton is used to show multiple options to the user from these options user can select only one option. We can associate a different method with each RadioButton. Furthermore, we can show multiple lines or images with RadioButtons.
It is important to note, to add proper functionality in RadioButtons each RadioButton must be associated with a single variable. Each RadioButton must symbolize with a single value for that particular variable.
Syntax of Python Tkinter RadioButton:
w = Radiobutton (master, options)
- master: This represents the parent window.
- options: Here is the list of most commonly used options for this widget.
Example of Python Tkinter RadioButton:
# importing tkinter lib
from tkinter import *
l= ["C", "Java", "Python"]
selection = "You have chosen : " + l[radio.get()]
message.config(text = selection)
mainWindow = Tk()
radio = IntVar()
lb = Label(text = "Please choose one programming language:", pady=15 )
c = Radiobutton(mainWindow, text="C", variable=radio, value=0,
c.pack( anchor = W )
java = Radiobutton(mainWindow, text="Java", variable=radio, value=1,
java.pack( anchor = W)
python = Radiobutton(mainWindow, text="Python", variable=radio, value=2,
python.pack( anchor = W)
message = Label(mainWindow)
Various possible options in Python Tkinter RadioButton:
|This option is used to set background color of widget under focus.
|This option is used to set foreground color of widget under focus.
|This option helps us to set position of text inside the widget. By default value of anchor is CENTER.
|This option helps us to set normal background color of widget.
|This option helps us to display an image. You just have to set this option equal to an image object.
|This option is used to set the width of border.
|This option helps us to mention a function to every time when RadioButton will change its state.
|This option helps us to set the style of cursor like an arrow, dot etc
|This option is used to set font type in widget.
|This option helps us to set normal foreground colour of widget.
|This option helps us to set height of widget.
|This option is used to set highlight color shown to the widget under focus.
|This option is used to set color of the focus highlight when the widget is not having the focus.
|This option helps us to show image with radio button instead of text.
|This helps us to automatically organize the text in multiple lines.
|This option helps us to set space left and right between text and RadioButton.
|This option helps us to set space above and below.
|This helps us to set the style of the border by which is default Flat.
|This option used to set color of text when radio button will selected state.
|This option used to display image when radio button will selected state.
|This option is use to set state or RadioButton. By default it is NORMAL. You can also set it DISABLE.
|This option helps us to set string source for label.
|This option is use to mention text source(variable.)
|This option helps us to underline specified letter of the text.
|This option is used to set value of each RadioButton.
|This option is used to set variable in RadioButton.
|This helps us to set the width of the widget.
|This option is used to wrap text into number of lines.
Python Tkinter RadioButton Methods:
After learning about various available options in Python Tkinter RadioButton. Its time to check out some available methods for Python Tkinter RadioButton widget.
|This method called to deselect the RadioButton.
|This method is used to flash RadioButton between normal and active state.
|This method helps us to invoke the associated method with state of a RadioButton is changed.
|This method is used to select the RadioButton.