Reputation: 39
So in my program I'm asking the user what kind of music and what kind of movies they like. I use checkbuttons in order to asses each of these categories, the problem is that the on/off value of the buttons is always 0 (instead of 1 when pressed). Meaning that I can never print out statements like "You clicked x" because it never gets that value.
I've tried making the variables in the music fcn dif than the one in the movies fcn, and I've tried changing it to "if x = 0, print you chose x", but that just prints out all the statements at once, instead of individually. I suspect it's something to do with being in a fcn, because it works fine outside. But I don't know what to do.
(Also I know this obviously isn't an HTML snippet but I couldn't get it into the question through the code sample because of indentation & I'm new to stackoverflow so wasn't sure what else to do)
from Tkinter import *
def movies():
def chBoxSel():
C1Var = CheckVar1.get()
C2Var = CheckVar2.get()
C3Var = CheckVar3.get()
C4Var = CheckVar4.get()
if C1Var == 1:
print "You like Moonlight!"
if C2Var == 1:
print "You like Les Choristes!"
if C3Var == 1:
print "You like Donnie Darko!"
if C4Var == 1:
print "You like Mommy!"
#end ChBoxSel()
top = Tk()
CheckVar1 = IntVar()
CheckVar2 = IntVar()
CheckVar3 = IntVar()
CheckVar4 = IntVar()
C1 = Checkbutton(top, text = "Moonlight(2016)", variable = CheckVar1, \
onvalue = 1, offvalue = 0, height = 5, \
width = 20, \
command = chBoxSel)
C2 = Checkbutton(top, text = "Les Chorites(2004)", variable = CheckVar2, \
onvalue = 1, offvalue = 0, height = 5, \
width = 20, \
command = chBoxSel)
C3 = Checkbutton(top, text = "Donnie Darko(2001)", variable = CheckVar3, \
onvalue = 1, offvalue = 0, height = 5, \
width = 20, \
command = chBoxSel)
C4 = Checkbutton(top, text = "Mommy(2014)", variable = CheckVar4, \
onvalue = 1, offvalue = 0, height = 5, \
width = 20, \
command = chBoxSel)
label = Label(top, text = "Which of these movies do you like?")
label.grid(row =0, column = 0)
C1.grid(row = 1, column = 0)
C2.grid(row = 2, column = 0)
C3.grid(row = 3, column = 0)
C4.grid(row = 4, column = 0)
top.mainloop()
def music():
def chBoxSel():
C1Var = CheckVar1.get()
C2Var = CheckVar2.get()
C3Var = CheckVar3.get()
C4Var = CheckVar4.get()
if C1Var == 1:
print ""
if C2Var == 1:
print "You like Kanye West!"
if C3Var == 1:
print "You like Mother Mother!"
if C4Var == 1:
print "You like ABBA!"
top = Tk()
CheckVar1 = IntVar()
CheckVar2 = IntVar()
CheckVar3 = IntVar()
CheckVar4 = IntVar()
C1 = Checkbutton(top, text = "Childish Gambino", variable = CheckVar1, \
onvalue = 1, offvalue = 0, height = 5, \
width = 20, \
command = chBoxSel)
C2 = Checkbutton(top, text = "Kanye West", variable = CheckVar2, \
onvalue = 1, offvalue = 0, height = 5, \
width = 20, \
command = chBoxSel)
C3 = Checkbutton(top, text = "Mother Mother", variable = CheckVar3, \
onvalue = 1, offvalue = 0, height = 5, \
width = 20, \
command = chBoxSel)
C4 = Checkbutton(top, text = "ABBA", variable = CheckVar4, \
onvalue = 1, offvalue = 0, height = 5, \
width = 20, \
command = chBoxSel)
label = Label(top, text = "Which of these artists do you like?")
label.grid(row=0, column = 0)
C1.grid(row = 1, column = 0)
C2.grid(row = 2, column = 0)
C3.grid(row = 3, column = 0)
C4.grid(row = 4, column = 0)
top.mainloop()
root = Tk()
var = IntVar()
label = Label(root, text = "What are/is your favourite...")
label.grid(row=0, column = 0)
R1 = Radiobutton(root, text = "Movies", variable = var, value = 1,\
command = movies)
R1.grid(row = 1, column = 0)
R2 = Radiobutton(root, text = "Music", variable = var, value = 2,\
command = music)
R2.grid(row = 2, column = 0)
root.mainloop()
Upvotes: 2
Views: 3035
Reputation: 82
It looks like the problem stems from you calling Tk() and .mainloop twice in your program. According to this answer: Here that's a definite issue. Instead of calling Tk() when you instantiate 'top', you need to call Toplevel(). When you create the frame this way, you also don't need to call mainloop on the frame. I've updated your code below and this seems to work pretty well.
from Tkinter import *
def movies():
def chBoxSel():
C1Var = CheckVar1.get()
C2Var = CheckVar2.get()
C3Var = CheckVar3.get()
C4Var = CheckVar4.get()
if C1Var == 1:
print "You like Moonlight!"
if C2Var == 1:
print "You like Les Choristes!"
if C3Var == 1:
print "You like Donnie Darko!"
if C4Var == 1:
print "You like Mommy!"
#end ChBoxSel()
top = Toplevel()
CheckVar1 = BooleanVar()
CheckVar2 = BooleanVar()
CheckVar3 = BooleanVar()
CheckVar4 = BooleanVar()
#CheckVar1.set(True)
C1 = Checkbutton(top, text = "Moonlight(2016)", variable = CheckVar1, \
onvalue = True, offvalue = False, height = 5, \
width = 20, \
command = chBoxSel)
C2 = Checkbutton(top, text = "Les Chorites(2004)", variable = CheckVar2, \
onvalue = True, offvalue = False, height = 5, \
width = 20, \
command = chBoxSel)
C3 = Checkbutton(top, text = "Donnie Darko(2001)", variable = CheckVar3, \
onvalue = True, offvalue = False, height = 5, \
width = 20, \
command = chBoxSel)
C4 = Checkbutton(top, text = "Mommy(2014)", variable = CheckVar4, \
onvalue = True, offvalue = False, height = 5, \
width = 20, \
command = chBoxSel)
label = Label(top, text = "Which of these movies do you like?")
label.grid(row =0, column = 0)
C1.grid(row = 1, column = 0)
C2.grid(row = 2, column = 0)
C3.grid(row = 3, column = 0)
C4.grid(row = 4, column = 0)
#top.mainloop()
def music():
def chBoxSel():
C1Var = CheckVar1.get()
C2Var = CheckVar2.get()
C3Var = CheckVar3.get()
C4Var = CheckVar4.get()
if C1Var == 1:
print ""
if C2Var == 1:
print "You like Kanye West!"
if C3Var == 1:
print "You like Mother Mother!"
if C4Var == 1:
print "You like ABBA!"
top = Toplevel()
CheckVar1 = BooleanVar()
CheckVar2 = BooleanVar()
CheckVar3 = BooleanVar()
CheckVar4 = BooleanVar()
C1 = Checkbutton(top, text = "Childish Gambino", variable = CheckVar1, \
onvalue = True, offvalue = False, height = 5, \
width = 20, \
command = chBoxSel)
C2 = Checkbutton(top, text = "Kanye West", variable = CheckVar2, \
onvalue = True, offvalue = False, height = 5, \
width = 20, \
command = chBoxSel)
C3 = Checkbutton(top, text = "Mother Mother", variable = CheckVar3, \
onvalue = True, offvalue = False, height = 5, \
width = 20, \
command = chBoxSel)
C4 = Checkbutton(top, text = "ABBA", variable = CheckVar4, \
onvalue = True, offvalue = False, height = 5, \
width = 20, \
command = chBoxSel)
label = Label(top, text = "Which of these artists do you like?")
label.grid(row=0, column = 0)
C1.grid(row = 1, column = 0)
C2.grid(row = 2, column = 0)
C3.grid(row = 3, column = 0)
C4.grid(row = 4, column = 0)
#top.mainloop()
root = Tk()
var = BooleanVar()
label = Label(root, text = "What are/is your favourite...")
label.grid(row=0, column = 0)
R1 = Radiobutton(root, text = "Movies", variable = var, value = 1,\
command = movies)
R1.grid(row = 1, column = 0)
R2 = Radiobutton(root, text = "Music", variable = var, value = 2,\
command = music)
R2.grid(row = 2, column = 0)
root.mainloop()
Upvotes: 1