Python Tkinter RadioButton


In this tutorial, we will learn about Python Tkinter RadioButtonTkinter 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"]

def choice():  
    selection = "You have chosen : " + l[radio.get()]  
    message.config(text = selection)  
    
mainWindow = Tk()  

mainWindow.geometry("320x150")

radio = IntVar()  

lb = Label(text = "Please choose one programming language:", pady=15 )  
lb.pack()  

c = Radiobutton(mainWindow, text="C", variable=radio, value=0,  
                  command=choice)  
c.pack( anchor = W )  
  
java = Radiobutton(mainWindow, text="Java", variable=radio, value=1,  
                  command=choice)  
java.pack( anchor = W) 

python = Radiobutton(mainWindow, text="Python", variable=radio, value=2,  
                  command=choice)  
python.pack( anchor = W) 
  
message = Label(mainWindow)  
message.pack()   

mainWindow.mainloop() 

Various possible options in Python Tkinter RadioButton:

Option Description
activebackground This option is used to set background color of widget under focus.
activeforeground This option is used to set foreground color of widget under focus.
anchor This option helps us to set position of text inside the widget. By default value of anchor is CENTER.
bg This option helps us to set normal background color of widget.
bitmap This option helps us to display an image. You just have to set this option equal to an image object.
borderwidth This option is used to set the width of border.
command This option helps us to mention a function to every time when RadioButton will change its state.
cursor This option helps us to set the style of cursor like an arrow, dot etc
font This option is used to set font type in widget.
fg This option helps us to set normal foreground colour of widget.
height This option helps us to set height of widget.
highlightcolor This option is used to set highlight color shown to the widget under focus.
highlightbackground This option is used to set color of the focus highlight when the widget is not having the focus.
image This option helps us to show image with radio button instead of text.
justify This helps us to automatically organize the text in multiple lines.
padx This option helps us to set space left and right between text and RadioButton.
pady This option helps us to set space above and below.
relief This helps us to set the style of the border by which is default Flat.
selectcolor This option used to set color of text when radio button will selected state.
selectimage This option used to display image when radio button will selected state.
state This option is use to set state or RadioButton. By default it is NORMAL. You can also set it DISABLE.
text This option helps us to set string source for label.
textvariable This option is use to mention text source(variable.)
underline This option helps us to underline specified letter of the text.
value This option is used to set value of each RadioButton.
variable This option is used to set variable in RadioButton.
width This helps us to set the width of the widget.
wraplength 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.

METHOD Description
deselect() This method called to deselect the RadioButton.
flash() This method is used to flash RadioButton between normal and active state.
invoke() This method helps us to invoke the associated method with state of a RadioButton is changed.
select() This method is used to select the RadioButton.


Spread the love:

Please share this page on social media with your friends..

Facebook share Twitter Linkedin share