【问题标题】:Onvalue of Checkbutton always = 0 pythonCheckbutton 的 Onvalue 始终 = 0 python
【发布时间】:2017-10-12 02:49:38
【问题描述】:

所以在我的程序中,我询问用户他们喜欢什么样的音乐和什么样的电影。我使用检查按钮来评估这些类别中的每一个,问题是按钮的开/关值始终为 0(而不是按下时为 1)。这意味着我永远无法打印出诸如“您单击了 x”之类的语句,因为它永远不会获得该值。

我尝试使音乐 fcn 中的变量与电影 fcn 中的变量相比,我尝试将其更改为“如果 x = 0,则打印您选择 x”,但这只会打印出所有一次声明,而不是单独声明。我怀疑这与在 fcn 中有关,因为它在外面工作得很好。但我不知道该怎么办。

(我也知道这显然不是 HTML sn-p,但由于缩进,我无法通过代码示例将其纳入问题,我是 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()
    

【问题讨论】:

    标签: python tkinter


    【解决方案1】:

    看起来问题源于您在程序中两次调用 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()
    

    【讨论】:

      猜你喜欢
      • 2017-01-18
      • 1970-01-01
      • 2023-03-10
      • 2014-01-09
      • 1970-01-01
      • 2011-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多