How to select only one Radiobutton in tkinter

Question:

I have two radiobutton in my GUI but i want to able select only one at a time with the code below am able to select both radiobutton . I tried the checkbutton which also i can select both options.

from tkinter import *


def content():
    if not option1.get() and not option2.get():
        print("not allowed, select one dude")
    else:
        print("welcome dude")

    option1.set(False) 
    option2.set(False)   


root = Tk()
root.geometry("400x400")

option1 = BooleanVar(value=False)
R1 = Radiobutton(root, text="MALE", value=1, var=option1)
R1.pack()

option2 = BooleanVar(value=False)
R2 = Radiobutton(root, text="FEMALE", value=2, var=option2)
R2.pack()

b = Button(root, text="print", command=content)
b.pack(side="bottom")

root.mainloop()
Asked By: O JOE

||

Answers:

I presume you are wanting to create one radio button with multiple values which only allows one selection? You would be better to populate an array and run a loop to fill the radio button. Perhaps something like this?

from tkinter import *




root = Tk()
root.geometry("400x400")

GENDERS = [
    ("Male", "M"),
    ("Female", "F"),
    ("Other", "O")
]

v = StringVar()
v.set("L")  # initialize

for text, gender in GENDERS:
    b = Radiobutton(root, text=text,
                    variable=v, value=gender)
    b.pack(anchor=W)

root.mainloop()
Answered By: Jack Nicholson

You must bind both radiobuttons to the same variable.
Besides, the variable will receive the value specified in the value keyword argument.
I suggest you do the following:

option = StringVar()
R1 = Radiobutton(root, text="MALE", value="male", var=option)
R2 = Radiobutton(root, text="FEMALE", value="female", var=option)

You can know what item is currently selected, by tracing the option variable, and by calling its get method.
For instance, the following will print either "male" or "female" whenever the corresponding radiobutton is checked.

def print_var(*_):
    print(option.get())

root = Tk()
root.geometry("400x400")

option = StringVar()
R1 = Radiobutton(root, text="MALE", value="male", var=option)
R2 = Radiobutton(root, text="FEMALE", value="female", var=option)
R1.pack()
R2.pack()

option.trace('w', print_var)

root.mainloop()

A more complete example, according to your demand.
This script will display a window with two radiobuttons and a button.
When the button is clicked, a message is printed that depends upon whether an option was selected or not.

from tkinter import *

def validate():
    value = option.get()
    if value == "male":
        print("Welcome dude")
    elif value == "female":
        print("Welcome gurl")
    else:
        print("An option must be selected")

root = Tk()
root.geometry("400x400")

option = StringVar()
R1 = Radiobutton(root, text="MALE", value="male", var=option)
R2 = Radiobutton(root, text="FEMALE", value="female", var=option)
button = Button(root, text="OK", command=validate)

R1.pack()
R2.pack()
button.pack()

root.mainloop()

As a side note, you should never import a module with a star, eg from tkinter import *.
In short, it pollutes the namespace. More on this post.

Answered By: Right leg

The easiest way to do it that i found is this –
you have to give them both the same variable so that compiler can know that the user can only choose one…

from tkinter import *
window = Tk()
window.geometry("100x100")
var = IntVar()

radio = Radiobutton(window, text="this", variable=var, value=1)
radio.pack()
radio2 = Radiobutton(window, text="or this", variable=var, value=2)
radio2.pack()

window.mainloop()
Answered By: Deepinder Singh
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.