Tuesday, April 22, 2025

Tkinter - Radiobuttons

Radiobuttons are easy, just as Checkbuttons. First, make sure that this simple source is working, and then we will explain:

from tkinter import *
import tkinter.messagebox as mb
import os

top_win = Tk()
top_win.geometry("800x800")

lab_1 = Label(top_win, text = "Select only one: ").pack(anchor = W)

real_stuff = IntVar()

rad_1 = Radiobutton(top_win, text = "Python", variable = real_stuff, value = 1)
rad_1.pack(anchor = W)

rad_2 = Radiobutton(top_win, text = "C++", variable = real_stuff, value = 2)
rad_2.pack(anchor = W)

rad_3 = Radiobutton(top_win, text = "Perl", variable = real_stuff, value = 3)
rad_3.pack(anchor = W)

top_win.mainloop()

To explain what needs to be done we will have Tkinter label:

lab_1 = Label(top_win, text = "Select only one: ").pack(anchor = W)

Place that will hold choice will be real_stuff:

real_stuff = IntVar()

Then we will have 3 radiobuttons, where all of them will point to real_stuff. Radio buttons will have dedicated value: 1, 2 and 3. This is important:

rad_1 = Radiobutton(top_win, text = "Python", variable = real_stuff, value = 1)
rad_1.pack(anchor = W)

rad_2 = Radiobutton(top_win, text = "C++", variable = real_stuff, value = 2)
rad_2.pack(anchor = W)

rad_3 = Radiobutton(top_win, text = "Perl", variable = real_stuff, value = 3)
rad_3.pack(anchor = W)

What about function that will process values ? Again, make sure that code works, and then we will explain: 

from tkinter import *
import tkinter.messagebox as mb
import os

top_win = Tk()
top_win.geometry("800x800")

def selected():
    selected = real_stuff.get()

    output.config(text = selected)

    if selected == 1:
        print("Selected: Python at position", selected)
    if selected == 2:
        print("Selected: C++ at position", selected)
    if selected == 3:
        print("Selected: Perl at position", selected)

lab_1 = Label(top_win, text = "Select only one: ").pack(anchor = W)

real_stuff = IntVar()

rad_1 = Radiobutton(top_win, text = "Python", variable = real_stuff,
                    value = 1, command = selected)
rad_1.pack(anchor = W)

rad_2 = Radiobutton(top_win, text = "C++", variable = real_stuff, value = 2,
                    command = selected)
rad_2.pack(anchor = W)

rad_3 = Radiobutton(top_win, text = "Perl", variable = real_stuff, value = 3,
                    command = selected)
rad_3.pack(anchor = W)

output = Label(top_win, text = "You Selected: ")
output.pack(anchor = W)

top_win.mainloop()

Function selected() will get values from real_stuff using get() method.

Then it will set what is captured as config text for output. Output is external Label where results will be printed, in Tkinter Window.

Also, based on choice, values will be printed in Python Shell using if checks: 

def selected():
    selected = real_stuff.get()

    output.config(text = selected)

    if selected == 1:
        print("Selected: Python at position", selected)
    if selected == 2:
        print("Selected: C++ at position", selected)
    if selected == 3:
        print("Selected: Perl at position", selected)

All radio buttons now have connection to function using "command = ":

rad_1 = Radiobutton(top_win, text = "Python", variable = real_stuff,
                    value = 1, command = selected)
rad_1.pack(anchor = W)

rad_2 = Radiobutton(top_win, text = "C++", variable = real_stuff, value = 2,
                    command = selected)
rad_2.pack(anchor = W)

rad_3 = Radiobutton(top_win, text = "Perl", variable = real_stuff, value = 3,
                    command = selected)
rad_3.pack(anchor = W)

And this is source for output Label:

output = Label(top_win, text = "You Selected: ")
output.pack(anchor = W)

As always, you have corresponding YT tutorial on Tkinter Radiobuttnos.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...