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()
謝謝,這個工作很棒 – Nicky