因此,在我的节目中,我向用户询问他们喜欢什么样的音乐和什么样的电影。我使用检查按钮来评估这些类别中的每一个,问题是按钮的开/关值始终为0(而不是按下时为1)。这意味着我永远不会打印出像&#34这样的陈述;你点击了x"因为它永远不会得到那个价值。
我试过让音乐fcn中的变量比电影fcn中的变量差,并且我尝试将其更改为"如果x = 0,打印你选择x&#34 ;但是,这只是一次打印出所有语句,而不是单独打印出来。我怀疑它与fcn有关,因为它在外面工作正常。但我不知道该怎么做。
(另外我知道这显然不是一个HTML代码段,但我无法通过代码示例将其纳入问题,因为缩进和我对stackoverflow的新功能所以并不是#39 ;确定还有什么可做的)
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()

答案 0 :(得分:0)
看起来这个问题源于你在程序中两次调用Tk()和.mainloop。根据这个答案:Here这是一个明确的问题。在实例化'top'时,不需要调用Tk(),而是需要调用Toplevel()。以这种方式创建框架时,您也不需要在框架上调用mainloop。我已经更新了下面的代码,这看起来效果很好。
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()