Radio button in Tkinter | python

Python Methods and Functions

Syntax :

button = Radiobutton (master, text = ”Name on Button”, variable = “shared variable”, value = “values ​​of each button ”, Options = values, ...)

shared variable = A Tkinter variable shared among all Radio buttons
value = each radiobutton should have different value otherwise more than 1 radiobutton will get selected.

Code # 1:

Radio buttons, but not buttons, in a form buttons . To display the button field, you need to set the value to 0.

# Import Tkinter module

from tkinter import *

# from tkinter.ttk import *

 
# Create the main Tkinter window

master = Tk ()

master.geometry ( "175x175" )

 
# Tkinter string variable
# the ability to store any string value

= StringVar (master, "1" )

 
# Dictionary for creating multiple buttons

values ​​ = { "RadioButton 1" : "1" ,

"RadioButton 2" : "2" ,

"RadioButton 3" : "3" ,

"RadioButton 4" : " 4 " ,

  " RadioButton 5 " : " 5 " }

 
# Loop used to create multiple radio buttons
# instead of creating each button separately

for (text, value) in values.items ():

Radiobutton (master, text = text, variable = v, 

value = value, indicator = 0 ,

  background = "light blue" ). pack (fill = X, ipady = 5 )

  
# Infinite loop can be interrupted
# keyboard or mouse interruption
# or any predefined function (destroy ())
mainloop ()

Output:

The background of these button blocks is light blue. Button blocks with white background and sunken ones are selected.

Code no. 2: Changing button blocks to standard radio buttons. To do this, remove the indicator by option.

# Import Tkinter module

from tkinter import * from tkinter.ttk import *

 
# Create the main Tkinter window

master = Tk ()

master.geometry ( "175x175" )

 
# Tkinter string variable
# the ability to store any string variable meaning

v = StringVar (master, "1" )

 
# Dictionary for creating multiple buttons

values ​​ = { " RadioButton 1 " : "1" ,

"RadioButton 2" : "2" ,

"RadioButton 3" : "3" ,

  "RadioButton 4" : "4" ,

"RadioButton 5" : "5" }

 
# Loop used to create multiple radio buttons
# instead of creating each button separately

for (text, value) in values.items ():

Radiobutton (master, text = text, variable = v,

value = value) .pack (side = TOP, ipady = 5 )

 
# Endless loop can be interrupted
# keyboard or mouse interruption
# or any predefined function (destroy ())
mainloop ()

Output:

These tkinter.ttk are created using tkinter.ttk , so the background option is not available, but we can use the style class for styling.

Code # 3: Styling the radio button with style class .

# Import Tkinter module

from tkinter import * from tkinter.ttk import *

 
# Create the main Tkinter window

master = Tk ()

master.geometry ( '175x175' )

 
# Tkinter string variable
# the ability to store any string value

v   = StringVar (master, "1" )

  
# Style class to add style to the Radiobutton
# it can be used to style any ttk widget

style = Style (master)

style.configure ( "TRadiobutton" , background = " light green "

  foreground = "red" , font = ( " arial " , 10 , " bold " ))

 
# Dictionary for creating multiple buttons

values ​​ = { "RadioButton 1" : "1" ,

  " RadioButton 2 " : " 2 " ,

  "RadioButton 3" : "3" ,

"RadioButton 4" : "4" ,

"RadioButton 5" : "5" }

 
# Loop used to create multiple radio buttons
# instead of to create each button separately

for (text, value) in values.items ():

Radiobutton (master, text = text, variable = v, 

value = value) .pack (side = TOP, ipady = 5 )

 
# Endless loop can be interrupted
# keyboard or mouse interruption
# or any predefined function (destroy ())
mainloop ()

Output:

You may notice that the font style is changing, as well as changing the background and foreground colors. Here TRadiobutton is used in the style class, it automatically applies styling to all available radio buttons.





Tutorials