我正在爲我的餐廳的員工進行菜單測試。該計劃是菜單項來遍歷「環路項目在這裏」此時,他們選擇了正確的checkbuttons(成分),然後點擊「提交併繼續按鈕」。當他們點擊提交按鈕時,我首先需要讀取檢查按鈕的開關值,以確定他們選擇了哪些項目,然後將這些值與我在列表字典中定義的正確答案進行比較,然後清除所有檢查按鈕以及是否答案是錯誤的或正確的程序將繼續,我最終會有一個結果屏幕,但現在我堅持如何讀取開關值的檢查按鈕。我只是試圖打印選定的蔬菜,現在無法弄清楚。Python 3.2 tkinter通過for循環添加讀取checkbutton值
我認爲這與它們在不同方法中的事實以及它們被添加到循環中的事實有關?我不確定,但我知道我的代碼正在嘗試閱讀錯誤的東西,任何幫助都會被感謝!
對不起,冗長的問題,我只是認爲這將是有益的給你儘可能多的信息儘可能地瞭解我試圖做...
from tkinter import *
class GUI(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.initUI()
def initUI(self):
self.grid()
self.parent.title("Wahoos Menu Test")
self.create_buttons()
global count
count = -1
def create_buttons(self):
for r in range(20):
for c in range(14):
Label(self, text='',
borderwidth=0).grid(row=r,column=c)
B = Button(self, text ="Begin Exam", relief=RIDGE, fg="black", command= self.on_button_press).grid(row=19, column=7)
L = Label(self, text="What comes in the following", fg="blue").grid(row=6, column=0)
self.veg = ['Lettuce', 'Cabbage', 'Cheese', 'Ahee Rice', 'Brown Rice', 'Banzai Veg', 'Red Cabbage', 'Black Beans', 'Cajun White Beans']
self.vegboxes = []
self.opt = []
c = 1
for ve in self.veg:
c +=1
self.v = IntVar()
self.vegboxes.append(self.v)
vo = Checkbutton(self, text=ve, variable=self.v, onvalue=1, offvalue=0).grid(row=c, column=11, sticky=W)
def on_button_press(self):
global count
count = count + 1
menuItems = {'nft': ['cabbage', 'cheese', 'corn', 'nf', 'salsa'],
'nckt': ['lettuce', 'cheese', 'corn', 'nck', 'salsa']}
menu = ['blackened fish taco', 'wahoos chicken salad']
if count == len(menu):
C = Button(self, text =" Your Done! ", relief=RIDGE, fg="black").grid(row=19, column=7)
else:
m = Label(self, text=menu[count], fg="black").grid(row=7, column=0)
C = Button(self, text ="Submit and Continue", relief=RIDGE, fg="black", command= self.read_checks).grid(row=19, column=7)
def read_checks(self):
for v in self.veg:
if self.v == 1:
print(self.veg[v])
def main():
root = Tk()
app = GUI(root)
root.mainloop()
if __name__ == '__main__':
main()
附註:類的好處之一是,你可以在'__init__'中插入類似self.count的東西,從而避免使用全局關鍵字。 – 2012-04-10 12:25:17