4

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()
O JOE
  • 587
  • 2
  • 17
  • 31

3 Answers3

4

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.

Right leg
  • 16,080
  • 7
  • 48
  • 81
  • I dont want print the value of the radiobutton selected but i want one to be selected if not should print ** not allowed** – O JOE Jan 08 '18 at 09:16
  • if one is selected should print welcome dude but the challenge now is am able to select both radiobuttons but i want only one to be selected. – O JOE Jan 08 '18 at 09:18
  • 1
    @NewpyBoi That's a proof of concept, you can do whatever you want from this point. Simply check in your button callback that the `option` variable's value is set. – Right leg Jan 08 '18 at 09:19
0

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()
  • 2
    whether or not they use an array is irrelevant. It's good programming practice, but what they want can be done without an array so your answer is a little bit misleading. – Bryan Oakley Jan 08 '18 at 13:12
0

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()