Tkinter Radiobutton

Tkinter Radiobutton is the standard widget that allows the user to choose only one of many predefined options. Radiobutton can display text or image.

You can also attach any function or method to the radiobutton via the ‘command’ option. Means, when the user will click on the radiobutton Tkinter will automatically call that function.

Tkinter Radiobutton is different from checkbutton where the user is allowed to select multiple options.

Here is the syntax of radiobutton.

Syntax :

radio = Radiobutton(window,**options)

Now, lets look at various options that we can use with radiobutton.

Options :

activebackgroundSet the background color of the radiobutton when it’s active.
activeforegroundSet the text color of the radiobutton when it’s active.
anchorSet the location of the text of the radiobutton. default is CENTER.
backgroundSet the background color of the radiobutton.
borderwidthSet the border width of the radiobutton.
commandSet the function or method to be called when the user clicks on the radiobutton.
compoundUse this option to control how to combine text and image.
cursorSet the cursor to show when the mouse pointer hovers the radiobutton.
disabledforegroundSet the text color of the radiobutton when it’s disabled.
fontSet the text font for the radiobutton.
foregroundSet the color of the text for the radiobutton.
heightSet the height of the radiobutton.
imageSet the image to display in radiobutton.
indicatorUse this option to change the style of the radiobutton. Set this to 0. default is 1.
justifyUse this option to control the alignment of multi-line text. Use CENTER, LEFT, RIGHT.
padxUse this option to add extra horizontal padding between text and border.
padyUse this option to add extra vertical padding between text and border.
reliefSet the border style. use SUNKENRAISEDGROOVERIDGE, or FLAT(default)
selectcolorSet the color of the selector.
selectimageSet the image for the selector.
stateSet the state of the radiobutton. use NORMAL(default), ACTIVE, DISABLED.
textSet the text of the radiobutton.
textvariableSet the text variable to change the text of the radiobutton.
valueSet the value associated with the radiobutton. all buttons should have different values.
variableSet the variable associated with the button. all buttons should have the same variable.
widthSet the width of the radiobutton.

Tkinter Radiobutton Examples :

For a better understanding of the concepts let’s look at some code examples.

Example 1 : Simple Radio Button

In this code example, we will display a group of three radiobuttons and a button with the text “get value” on the Tkinter window, and on button click value of the selected radio button will be printed on the console.

from tkinter import * 

window = Tk()

# Create a string variable
var = StringVar()
var.set("2") # Set its default value to '2'

# Create three radiobuttons with different values
# but same variable
Radiobutton(window,text = "Radio 1",value = "1",variable = var).pack()
Radiobutton(window,text = "Radio 2",value = "2",variable = var).pack()
Radiobutton(window,text = "Radio 3",value = "3",variable = var).pack()

def get_value():
    # Function to be called on button click
    print("Selected : ",var.get())

# Create a button
Button(window,text = "Get Value",command = get_value).pack()

window.mainloop()

Output :

Tkinter radiobutton code example 1 output.

Example 2 : Indicator Radio Button

In this code example, instead of displaying standard radio buttons with circular holes, we will change its style to the rectangular button box. To do this we need to set the indicator/indicatoron option to 0.

from tkinter import *

window = Tk()

# Create a string variable
var = StringVar()
var.set("2") # Set its default value to '2'

# Change style by setting indicator to 0
Radiobutton(window,text = "Radio 1",width = 20,
    value = "1",variable = var,indicator = 0).pack()
Radiobutton(window,text = "Radio 2",width = 20,
    value = "2",variable = var,indicator = 0).pack()
Radiobutton(window,text = "Radio 3",width = 20,
    value = "3",variable = var,indicator = 0).pack()

window.mainloop()

Output :

Tkinter radiobutton example 2 output

Example 3 : Display Image In RadioButton

In this code example, we will display the image in the radio button.

from tkinter import *

window = Tk()

# Create a string variable
var = StringVar()
var.set("2") # Set its default value to '2'

# Load Image
image = PhotoImage(file="circle.png")

# Radiobutton with image
Radiobutton(window,image = image,
    value = "1",variable = var).pack()

# Radiobutton with image and text
Radiobutton(window,text = "Text + ",image = image,
    compound = RIGHT,value = "2",variable = var).pack()

window.mainloop()

Output :

tkinter radiobutton code example 3 : output.

Example 4 : Add Styles To RadioButton

In this code example, we will use the above-discussed options to add some basic styling to the radiobuttons.

from tkinter import *

window = Tk()

# Radiobutton with styles
Radiobutton(window,value = "1",background = "black",text = "White",
    foreground="white",font = "arial 15 bold",pady = 10).pack()
Radiobutton(window,value = "2",background = "black",text = "White",
    foreground="red",font = "arial 15 bold",pady = 10).pack()

window.mainloop()

Output :

code example 4

Example 5: Link A Function With RadioButton

In this code example, we will use the ‘command’ option to link a function with the radiobutton. When the user will click on the radiobutton, Tkinter will automatically call that function.

from tkinter import *

window = Tk()

# Create a string variable
var = StringVar()
var.set("2") # Set its default value to '2'

def get_value():
    # Function to be called on button click
    print("Selected : ",var.get())

# Create three radiobuttons with different values
# but same variable and came command fun
Radiobutton(window,text = "Radio 1",value = "1",variable = var,command = get_value).pack()
Radiobutton(window,text = "Radio 2",value = "2",variable = var,command = get_value).pack()
Radiobutton(window,text = "Radio 3",value = "3",variable = var,command = get_value).pack()


window.mainloop()

Output :

tkinter radiobutton code example 5 output.

That’s wrap for the tutorial of tkinter radiobutton

Leave a Reply