目前,我有我的餐馆工作菜单测试读取由checkbuttons接收到的输入,比较他们正确答案,然后显示无论是做好消息框或继续工作 messagebox。我还设置了它,以便如果他们没有正确回答问题,就会打印出他们回答的问题,然后输出正确答案。而不是消息框和简单的打印他们做错了什么,我想有一个最后的结果屏幕,要么具有良好的工作(或类似的规定)消息或已印刷错误VS上正确的答案。这里是我的代码,告诉你我到目前为止如何实现了一切,就像我说的一切工作一样,我只是试图想出一种方法来使其更加可以展示,并且还没有找到办法做到这一点。的Python 3.2 Tkinter的创建成果框架中显示输出
from tkinter import *
import tkinter.messagebox as tkMessageBox
class GUI(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.initUI()
self.count = -1
self.answers = {'nft':[], 'nckt':[]}
self.menuItems = {'nft': ['Cheese', 'Cabbage', 'Corn', 'Blackened Fish', 'Salsa'],
'nckt': ['Lettuce', 'Cheese', 'Corn', 'Blackened Chicken', 'Salsa']}
self.menu = ['nft', 'nckt']
#self.p = PhotoImage(file="wahoos.gif")
#self.l = Label(self, image=self.p).grid(row=7, column=7)
def initUI(self):
self.grid()
self.parent.title("Wahoos Menu Test")
self.create_buttons()
def create_buttons(self):
for r in range(20):
for c in range(14):
Label(self, text='',
borderwidth=0).grid(row=r,column=c)
self.b = Button(self, text ="Begin Exam", relief=RIDGE, fg="black", command= self.on_button_press)
self.b.grid(row=19, column=7)
self.m = Label(self, text="")
self.m.grid(row=7, column=0)
L = Label(self, text="What comes in the following", fg="blue").grid(row=6, column=0)
self.tortButton = {'Flour':0, 'Corn':0, 'Wheat':0}
self.vegButton = {'Lettuce':0, 'Cabbage':0, 'Cheese':0,
'Ahee Rice':0, 'Brown Rice':0, 'Banzai Veg':0, 'Red Cabbage':0, 'Beans':0}
self.protButton = {'Carne Asada':0, 'Flamebroiled Chicken':0, 'Blackened Fish':0,
'Blackened Chicken':0, 'Flamebroiled Fish':0, 'Pork':0, 'Shrimp':0,
'Tofu':0, 'Blackened Mushroom':0, 'Rice and Beans':0, 'Banzai Veggies':0}
self.sauceButton = {'Salsa':0, 'Guacamole':0, 'Sour Cream':0,
'Roasted Pepper':0, 'Ketchup':0, 'Ranch':0, 'Balsamic':0,
'Mr. Lees':0, 'Teriyaki':0, 'Tapatio':0, 'Cream Cheese':0, 'Aioli':0}
V = Label(self, text="Veggies", fg="green").grid(row=1, column=11, sticky=W)
T = Label(self, text="Tortillas ", fg="green").grid(row=1, column=12, sticky=W)
P = Label(self, text="Proteins", fg="green").grid(row=1, column=13, sticky=W)
S = Label(self, text="Sauces", fg="green").grid(row=1, column=14, sticky=W)
c = 1
for key in self.tortButton:
c +=1
self.tortButton[key] = IntVar()
to = Checkbutton(self, text=key, variable=self.tortButton[key]).grid(row=c, column=12, sticky=W)
c = 1
for key in self.vegButton:
c += 1
self.vegButton[key] = IntVar()
vo = Checkbutton(self, text=key, variable=self.vegButton[key]).grid(row=c, column=11, sticky=W)
c = 1
for key in self.protButton:
c +=1
self.protButton[key] = IntVar()
po = Checkbutton(self, text=key, variable=self.protButton[key]).grid(row=c, column=13, sticky=W)
c = 1
for key in self.sauceButton:
c +=1
self.sauceButton[key] = IntVar()
so = Checkbutton(self, text=key, variable=self.sauceButton[key]).grid(row=c, column=14, sticky=W)
def on_button_press(self):
self.count = self.count + 1
if self.count == len(self.menu):
self.m.configure(text="")
self.b.configure(text ="Your Done! Click here to see your results.", command = self.compare)
else:
self.m.configure(text=self.menu[self.count])
self.b.configure(text ="Submit and Continue", command= self.read_checks)
def read_checks(self):
for key, value in self.vegButton.items():
state = value.get()
if state !=0:
print (key)
self.answers[self.menu[self.count]].append(key)
self.vegButton[key].set(0)
for key, value in self.tortButton.items():
state = value.get()
if state !=0:
print (key)
self.answers[self.menu[self.count]].append(key)
self.tortButton[key].set(0)
for key, value in self.protButton.items():
state = value.get()
if state !=0:
print (key)
self.answers[self.menu[self.count]].append(key)
self.protButton[key].set(0)
for key, value in self.sauceButton.items():
state = value.get()
if state !=0:
print (key)
self.answers[self.menu[self.count]].append(key)
self.sauceButton[key].set(0)
print (self.answers)
print (self.menuItems)
self.on_button_press()
def compare(self):
self.match = True
self.count = -1
for key in self.answers:
if self.answers[key] != self.menuItems[key]:
print ("For ", self.menu[self.count], " you answered ", self.answers[key])
print ("The correct answer for ", self.menu[self.count], " is ", self.menuItems[key])
self.count = self.count + 1
self.match = False
if self.match == True:
tkMessageBox.showinfo("All Pau!", "Nice job! I think your ready!")
else:
tkMessageBox.showinfo("Uh Ohh", "Looks like you have some more studying to do.")
def main():
root = Tk()
app = GUI(root)
root.mainloop()
if __name__ == '__main__':
main()
def compare(self)
是我做我的比较,并打印我想出现在结果屏幕上的输出,这也是在那里我有消息框弹出。
我正在读的一些您发布的其他问题的代码。你的代码的设计是非常程序化的 - 看起来你可以使用更多的面向数据的方法。为什么不为每个单独的任务构建独立的通用对象,然后将它们“粘合”在一起,而不是逐步接近程序。它可以让你重复使用更多的代码,除了输入,最终使你的程序更具可读性。 – 2012-04-14 02:19:26
我同意您的说法,我是新来的蟒蛇,这是我实际上它编码,以便暂时我刚学的一切,我走,然后通过返回并清除它计划第一个重大的事情,从GUI中分离逻辑和所有这些。感谢评论。 – crenfro 2012-04-14 09:36:08