Skip to content
Advertisement

Tkinter displaying users choice back to them once they have clicked a button and submitted and submitted

I’m trying to create a form where a user picks a time, and depending on the option they pick. It is then displayed back to the user once they click the submit button. But when i click an option and submit it prints all four options regardless. I also am trying to make it such that once a user clicks one of the options they are unable to click another. Here is my code so far.

from tkinter import *
master = Tk()

btn1 = IntVar()
btn2 = IntVar()
btn3 = IntVar()
btn4 = IntVar()

def variables():
    w = btn1.get()
    print(w)
    x = btn2.get()
    print(x)
    y = btn3.get()
    print(y)
    z = btn4.get()
    print(z)

theLabel = Label(master, text="What time would you like to go?")
theLabel.grid(row=0, columnspan=3)

option1 = Checkbutton(master, text="Now:", variable=btn1).grid(row=2,
                                                                       column=1, sticky=W)
option2 = Checkbutton(master, text="15 Mins:", variable=btn2).grid(row=3,
                                                                       column=1, sticky=W)
option3 = Checkbutton(master, text="30 Mins:", variable=btn3).grid(row=4,
                                                                       column=1, sticky=W)
option4 = Checkbutton(master, text="1 Hour:", variable=btn4).grid(row=5,
                                                                       column=1, sticky=W)

Button(master, text='Submit', command=variables).grid(row=7, sticky=W, pady=4)
Button(master, text='Quit', command=master.destroy).grid(row=8, sticky=W, pady=4)


mainloop()

Advertisement

Answer

Try this:

from tkinter import *
master = Tk()

btn1 = IntVar()
btn1.set(-1)

def variables():
    w = btn1.get()
    if w == -1:
        print("You didn't choose any of the options")
    else:
        print(str(w)+" min")
        option1.config(state="disabled")
        option2.config(state="disabled")
        option3.config(state="disabled")
        option4.config(state="disabled")

theLabel = Label(master, text="What time would you like to go?")
theLabel.grid(row=0, columnspan=3)

option1 = Radiobutton(master, text="Now:", variable=btn1, value=0)
option1.grid(row=2, column=1, sticky=W)
option2 = Radiobutton(master, text="15 Mins:", variable=btn1, value=15)
option2.grid(row=3, column=1, sticky=W)
option3 = Radiobutton(master, text="30 Mins:", variable=btn1, value=30)
option3.grid(row=4, column=1, sticky=W)
option4 = Radiobutton(master, text="1 Hour:", variable=btn1, value=60)
option4.grid(row=5, column=1, sticky=W)

submit_button = Button(master, text='Submit', command=variables)
submit_button.grid(row=7, sticky=W, pady=4)
destroy_button = Button(master, text='Quit', command=master.destroy)
destroy_button.grid(row=8, sticky=W, pady=4)

It uses Radiobuttons and notice that all of them have the same variable (btn1) but different value. Also I made it so that the buttons get disabled when the user clicks submit.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement