2017-10-11 120 views
1

因此,在我的程序中,我询问用户他们喜欢什么样的音乐和什么样的电影。我使用checkbuttons来评估每个类别,问题是按钮的开/关值始终为0(而不是按下时为1)。这意味着我永远不会打印出像“你点击X”这样的语句,因为它从来没有得到那个价值。Checkbutton Onvalue always = 0 python

我试着让音乐fcn中的变量不同于电影fcn中的变量,我试着将它改为“if x = 0,打印您选择了x”,但是这只是打印出所有这些陈述立即,而不是单独的。我怀疑这与做一个fcn有关,因为它在外面很好。但我不知道该怎么做。 (我也知道这显然不是一个HTML代码片段,但我不能通过代码示例将它带入问题,因为缩进&我是新来的stackoverflow,所以不知道还有什么要做)

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

它看起来像从问题你叫TK(茎),并在程序.mainloop两次。根据这个答案:Here这是一个明确的问题。当你实例化'top'时,你不需要调用Tk(),而需要调用Toplevel()。当您以这种方式创建框架时,您也不需要在框架上调用主循环。我在下面更新了你的代码,这似乎工作得很好。

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

谢谢,这个工作很棒 – Nicky