2017-10-11 3 views
1

Donc, dans mon programme, je demande à l'utilisateur quel genre de musique et quel genre de films ils aiment. J'utilise des checkbuttons pour évaluer chacune de ces catégories, le problème est que la valeur on/off des boutons est toujours 0 (au lieu de 1 lorsqu'on appuie dessus). Ce qui signifie que je ne peux jamais imprimer des déclarations comme "Vous avez cliqué sur x" parce qu'elle n'a jamais cette valeur. J'ai essayé de faire les variables dans la musique fcn dif que celle dans les films fcn, et j'ai essayé de le changer en "si x = 0, print you choisi x", mais cela imprime tout les déclarations à la fois, au lieu de individuellement. Je soupçonne que ça a quelque chose à voir avec le fait d'être dans une fcn, parce que ça fonctionne bien dehors. Mais je ne sais pas quoi faire.Valeur de Checkbutton toujours = 0 python

(Aussi, je sais que ce n'est évidemment pas un extrait de code HTML, mais je ne pouvais pas l'inclure dans la question à cause de l'indentation & Je ne sais pas quoi faire d'autre)

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

Répondre

0

Il semble que le problème vient de vous appeler Tk() et .mainloop deux fois dans votre programme. D'après cette réponse: Here c'est un problème. Au lieu d'appeler Tk() lorsque vous instanciez 'top', vous devez appeler Toplevel(). Lorsque vous créez le cadre de cette façon, vous n'avez pas besoin d'appeler mainloop sur le cadre. J'ai mis à jour votre code ci-dessous et cela semble fonctionner plutôt bien.

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

Merci, cela a bien fonctionné – Nicky