2017-10-11 12 views
1

私のプログラムでは、どのような音楽とどのような映画が好きなのかをユーザーに尋ねています。これらのカテゴリのそれぞれを検証するためにチェックボタンを使用しますが、ボタンのオン/オフ値は常に0(押されたときは1)ではないという問題があります。私は決してその価値を得ることができないので、「あなたはxをクリックしました」のようなステートメントを印刷することはできません。Checkbuttonの値は常に0です。python

私は映画fcnのものよりもfcnの変数を変えようとしましたが、 "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()を呼び出す必要があります。このようにフレームを作成すると、フレーム上で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() 
+0

ありがとうございました – Nicky

関連する問題