Willeh
Willeh

Reputation: 11

Python tkinter checkboxes always returning value as 0 even when they are checked

It's supposed to be a GUI pizza order form so it has more but the problem that i'm encountering is when it gets the a variable that is supposed to be oliveSelection it always returns as 0 when checked and not 1.

from tkinter import *
myGui=Tk()
myGui.geometry("800x600")
myGui.title("Pete's Pizza Parlour~Order Form")


TOPPING SELECTION
    toppings_lbl=Label(myGui,text="Toppings:",font=("Good Times",10),fg="blue").pack()
    a=IntVar()
    olives_chk=Checkbutton(myGui,text="Olives",variable=a).pack()
    b=IntVar()
    tomatoes_chk=Checkbutton(myGui,text="Tomatoes",variable=b).pack()
    c=IntVar()
    pepperoni_chk=Checkbutton(myGui,text="Pepperoni",variable=c).pack()
    d=IntVar()
    hotPeppers_chk=Checkbutton(myGui,text="Hot Peppers",variable=d).pack()
    e=IntVar()
    onions_chk=Checkbutton(myGui,text="Onions",variable=e).pack()
    f=IntVar()
    ham_chk=Checkbutton(myGui,text="Ham",variable=f).pack()
    g=IntVar()
    sausage_chk=Checkbutton(myGui,text="Sausage",variable=g).pack()
    h=IntVar()
    greenPeppers_chk=Checkbutton(myGui,text="Green Peppers",variable=h).pack()

olivesSelection=a.get()
tomatoesSelection=b.get()
pepperoniSelection=c.get()
hotPeppersSelection=d.get()
onionsSelection=e.get()
hamSelection=f.get()
sausageSelection=g.get()
greenPeppersSelection=h.get()

olivesSelectionStr="olives"
tomatoesSelectionStr="tomatoes"
pepperoniSelectionStr="pepperoni"
hotPeppersSelectionStr="hot peppers"
onionsSelectionStr="onions"
hamSelectionStr="ham"
sausageSelectionStr="sausage"
greenPeppersSelectionStr="green peppers"
noToppingsStr="no toppings."
def checkToppings():
    toppingsList=""
    if(olivesSelection==1):
        toppingsList=toppingsList+olivesSelectionStr
    elif(tomatoesSelection==1):
        toppingsList=toppingsList+tomatoesSelectionStr
    elif(pepperoniSelection==1):
        toppingsList=toppingsList+pepperoniSelectionStr
    elif(hotPeppersSelection==1):
        toppingsList=toppingsList+hotPeppersSelectionStr
    elif(onionsSelection==1):
        toppingsList=toppingsList+onionsSelectionStr
    elif(hamSelection==1):
        toppingsList=toppingsList+hamSelectionStr
    elif(sausageSelection==1):
        toppingsList=toppingsList+sausageSelectionStr
    elif(greenPeppersSelection==1):
        toppingsList=toppingsList+greenPeppersSelectionStr
    else:
        toppingsList=noToppingsStr

Olive selection always comes back as 0 not 1 when it should while checked print(olivesSelection)

Upvotes: 1

Views: 2520

Answers (3)

Dgomn D
Dgomn D

Reputation: 131

For me (after a lot of reading through posts here and trial-and-error) it was a simple as switching

from Tkinter import Tk
....
root = Tk()
....
myvar = IntVar()
mybutton = Checkbutton(root, text="foo", variable=myvar, onvalue=1, offvalue=0)

to

from Tkinter import Tk
....
root = Tk()
....
myvar = IntVar(root)
mybutton = Checkbutton(root, text="foo", variable=myvar, onvalue=1, offvalue=0)

Upvotes: 0

TigerhawkT3
TigerhawkT3

Reputation: 49318

  1. There was no myGui.mainloop() in your program.
  2. TOPPING SELECTION is not a valid statement. Also, there was no need for the following lines to be an indented block.
  3. Don't one-line the creation and placement of your tkinter widgets, or else you'll end up with a whole bunch of variables that reference None (the value returned by pack()).
  4. Statements like olivesSelection=a.get() are not doing what you think they're doing. That statement calls a.get(), gets a returned value of 0 (since it happens on program start), then assigns that 0 to olivesSelection. Checking boxes won't change that variable's value. If you want things to occur dynamically as you check boxes, you have to trace() those IntVars and add commands to the Checkbuttons.
  5. checkToppings() is never called.
  6. Checking whether olivesSelection==1 will always be False, since a.get() was 0 when it was assigned to olivesSelection (see above). Use a.get() here.
  7. If you use elif in checkToppings(), it'll only add one topping to the list (the first one that has a checked box).
  8. You don't need to use ==1 for a value that will only be 0 or 1 - just say if pepperoniSelection:. 1 is a truthy value and 0 is a falsy value (technically 1 is actually True and 0 is actually False since bool is a subclass of int).

from tkinter import *
myGui=Tk()
myGui.geometry("800x600")
myGui.title("Pete's Pizza Parlour~Order Form")


def checkem():
    print(a.get(), b.get(), c.get(), d.get(), e.get(), f.get(), g.get(), h.get())
    print(checkToppings())

toppings_lbl=Label(myGui,text="Toppings:",font=("Good Times",10),fg="blue")
toppings_lbl.pack()
a=IntVar()
olives_chk=Checkbutton(myGui,text="Olives",variable=a)
olives_chk.pack()
b=IntVar()
tomatoes_chk=Checkbutton(myGui,text="Tomatoes",variable=b)
tomatoes_chk.pack()
c=IntVar()
pepperoni_chk=Checkbutton(myGui,text="Pepperoni",variable=c)
pepperoni_chk.pack()
d=IntVar()
hotPeppers_chk=Checkbutton(myGui,text="Hot Peppers",variable=d)
hotPeppers_chk.pack()
e=IntVar()
onions_chk=Checkbutton(myGui,text="Onions",variable=e)
onions_chk.pack()
f=IntVar()
ham_chk=Checkbutton(myGui,text="Ham",variable=f)
ham_chk.pack()
g=IntVar()
sausage_chk=Checkbutton(myGui,text="Sausage",variable=g)
sausage_chk.pack()
h=IntVar()
greenPeppers_chk=Checkbutton(myGui,text="Green Peppers",variable=h)
greenPeppers_chk.pack()
thebutton = Button(myGui, text='check', command=checkem)
thebutton.pack()

olivesSelectionStr="olives"
tomatoesSelectionStr="tomatoes"
pepperoniSelectionStr="pepperoni"
hotPeppersSelectionStr="hot peppers"
onionsSelectionStr="onions"
hamSelectionStr="ham"
sausageSelectionStr="sausage"
greenPeppersSelectionStr="green peppers"
noToppingsStr="no toppings."

def checkToppings():
    toppings = [var for val,var in zip((a.get(), b.get(), c.get(), d.get(),
                                        e.get(), f.get(), g.get(), h.get()),
                                       (olivesSelectionStr, tomatoesSelectionStr,
                                        pepperoniSelectionStr, hotPeppersSelectionStr,
                                        onionsSelectionStr, hamSelectionStr,
                                        sausageSelectionStr, greenPeppersSelectionStr))
                if val]
    if not toppings:
        return 'no toppings.'
    elif len(toppings)==1:
        return toppings[0] + '.'
    elif len(toppings)==2:
        return ' and '.join(toppings) + '.'
    else:
        return ', '.join(toppings[:-1]) + ', and ' + toppings[-1] + '.'

myGui.mainloop()

Upvotes: 2

Kyrubas
Kyrubas

Reputation: 897

Ok, so a couple of things.

You need to have your topping.get() inside of your definition that calls for the toppings. Otherwise you're just executing the .get() when you run the program and not on demand

TOPPING SELECTION should be commented out as it serves no other purpose

The large elif tree is not going to be helpful to you in that as soon as you make one of the if statements True you're going to exit the if block. Also, as a matter of personal preference and code cleanliness, you may want to look up dictionaries for this. Once you get past 3 or 4 if statements in a block, they become rather messy.

Also, I know you said that there's more code, but just as a friendly reminder in case you have forgotten at the bottom of the rest of your code, be sure to have your main window instance enter mainloop()

from tkinter import *
myGui=Tk()
myGui.geometry("800x600")
myGui.title("Pete's Pizza Parlour~Order Form")

#TOPPING SELECTION
toppings_lbl=Label(myGui,text="Toppings:",font=("Good Times",10),fg="blue").pack()
a=IntVar()
olives_chk=Checkbutton(myGui,text="Olives",variable=a).pack()
b=IntVar()
tomatoes_chk=Checkbutton(myGui,text="Tomatoes",variable=b).pack()
c=IntVar()
pepperoni_chk=Checkbutton(myGui,text="Pepperoni",variable=c).pack()
d=IntVar()
hotPeppers_chk=Checkbutton(myGui,text="Hot Peppers",variable=d).pack()
e=IntVar()
onions_chk=Checkbutton(myGui,text="Onions",variable=e).pack()
f=IntVar()
ham_chk=Checkbutton(myGui,text="Ham",variable=f).pack()
g=IntVar()
sausage_chk=Checkbutton(myGui,text="Sausage",variable=g).pack()
h=IntVar()
greenPeppers_chk=Checkbutton(myGui,text="Green Peppers",variable=h).pack()

olivesSelectionStr="olives"
tomatoesSelectionStr="tomatoes"
pepperoniSelectionStr="pepperoni"
hotPeppersSelectionStr="hot peppers"
onionsSelectionStr="onions"
hamSelectionStr="ham"
sausageSelectionStr="sausage"
greenPeppersSelectionStr="green peppers"
noToppingsStr="no toppings."


def checkToppings():
    toppingsList=""
    olivesSelection=a.get()
    tomatoesSelection=b.get()
    pepperoniSelection=c.get()
    hotPeppersSelection=d.get()
    onionsSelection=e.get()
    hamSelection=f.get()
    sausageSelection=g.get()
    greenPeppersSelection=h.get()
    if(olivesSelection==1):
        toppingsList=toppingsList+olivesSelectionStr
    if(tomatoesSelection==1):
        toppingsList=toppingsList+tomatoesSelectionStr
    if(pepperoniSelection==1):
        toppingsList=toppingsList+pepperoniSelectionStr
    if(hotPeppersSelection==1):
        toppingsList=toppingsList+hotPeppersSelectionStr
    if(onionsSelection==1):
        toppingsList=toppingsList+onionsSelectionStr
    if(hamSelection==1):
        toppingsList=toppingsList+hamSelectionStr
    if(sausageSelection==1):
        toppingsList=toppingsList+sausageSelectionStr
    if(greenPeppersSelection==1):
        toppingsList=toppingsList+greenPeppersSelectionStr
    if toppingsList=="":
        toppingsList=noToppingsStr
    print(toppingsList)

topping_btn = Button(myGui,text='print toppings', command = checkToppings)
topping_btn.pack()
myGui.mainloop()

Upvotes: 0

Related Questions