Python Tkinter program for Radiobutton with an event handling


In this program, you will learn how to create a Tkinter Radiobutton with an event handling in Python.


self.r1 = Radiobutton(tk, text="male").place(x=80,y=90)
self.r2 = Radiobutton(tk, text="female").place(x=80,y=90)

Example: How to create a Tkinter Radiobutton with an event handling in Python

from tkinter import *


class Test:
    def __init__(self, tk):
        self.radio = StringVar()
        self.var = StringVar()

        self.r1 = Radiobutton(tk, text="Male", variable=self.radio, value="Male", command=self.selection).place(x=80,y=90)
        self.r2 = Radiobutton(tk, text="Female", variable=self.radio, value="Female", command=self.selection).place(x=80, y=130)
        self.label = Label(tk, textvariable=self.var).place(x=80, y=200)

    def selection(self):
        value = "You selected the option " + str(self.radio.get())
        self.var.set(value)


tk = Tk()
tk.geometry("600x500")
tk.title("Xiith.com")
myTest = Test(tk)
tk.mainloop()

Output:

tkinter radiobutton example