我正在为我的餐厅的员工进行菜单测试。该计划是菜单项来遍历“环路项目在这里”此时,他们选择了正确的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