2012-04-14 66 views
0

目前,我有我的餐館工作菜單測試讀取由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)是我做我的比較,並打印我想出現在結果屏幕上的輸出,這也是在那裏我有消息框彈出。

+0

我正在讀的一些您發佈的其他問題的代碼。你的代碼的設計是非常程序化的 - 看起來你可以使用更多的面向數據的方法。爲什麼不爲每個單獨的任務構建獨立的通用對象,然後將它們「粘合」在一起,而不是逐步接近程序。它可以讓你重複使用更多的代碼,除了輸入,最終使你的程序更具可讀性。 – 2012-04-14 02:19:26

+0

我同意您的說法,我是新來的蟒蛇,這是我實際上它編碼,以便暫時我剛學的一切,我走,然後通過返回並清除它計劃第一個重大的事情,從GUI中分離邏輯和所有這些。感謝評論。 – crenfro 2012-04-14 09:36:08

回答

2

我會從GUI繼承GUI,
,然後將不同的「窗口」放在幀中。
然後,您可以在幀上使用grid_forget()destroy(),使其消失。
在框架上使用grid()以使其重新出現,如果您選擇不銷燬它。
這裏是一個簡化的演示,其中原始幀(aFrame)在顯示結果幀(rFrame)3000毫秒後重新出現在
之間。
result_screengo_back方法底部:

import tkinter as tk 

class GUI(tk.Tk): 
    def __init__(self): 
     tk.Tk.__init__(self) 

     self.score = 0 

     self.buttonDic = { 
     'Brown Rice':0, 
     'Banzai Veg':0, 
     'Red Cabbage':0, 
     'Black Beans':0 
     } 

     aFrame = self.aFrame = tk.Frame(self) 
     aFrame.grid() 

     for key in self.buttonDic: 
      self.buttonDic[key] = tk.IntVar() 
      aCheckButton = tk.Checkbutton(aFrame, text=key, 
              variable=self.buttonDic[key]) 
      aCheckButton.grid(sticky='w') 

     submitButton = tk.Button(aFrame, text="Submit", 
             command=self.query_checkbuttons) 
     submitButton.grid() 

     self.trueList = ['Brown Rice', 'Black Beans'] 

    def query_checkbuttons(self): 
     for key, value in self.buttonDic.items(): 
      state = value.get() 
      if state != 0: 
       if key in self.trueList: 
        self.score += 1 
       else: 
        self.score -= 1 
       self.buttonDic[key].set(0) 
     self.result_screen() 

    def result_screen(self): 
     self.aFrame.grid_forget() 
     self.rFrame = tk.Frame(self) 
     self.rFrame.grid() 
     self.scoreText = tk.Text(self.rFrame, width=20, height=1) 
     self.scoreText.grid() 
     self.scoreText.insert('end', self.score) 
     self.after(3000, func=self.go_back) 

    def go_back(self): 
     self.score = 0 
     self.rFrame.destroy() 
     self.aFrame.grid() 


gui = GUI() 
gui.mainloop() 
+0

非常感謝您的回覆!我能夠使用這種方法進行工作。但是,是否可以將我最初打印到命令行的輸出打印到新框架?我嘗試了一堆不同的東西,似乎無法做任何工作?我可以明顯地打印出多行或多個我需要的內容,但我無法弄清楚如何從字典本身打印鍵和值......? – crenfro 2012-04-22 13:15:40

+0

@crenfro據我所知,任何你可以打印的東西都可以插入到一個Text小部件中。您可以先嚐試格式化字符串,然後將該變量放入insert()方法中。我需要看看你現在的例子來說更多的東西,這在技術上是一個單獨的問題/答案。 – 2012-04-22 17:38:56

+0

啊,你知道我以錯誤的方式使用StringVar()。我得到它的工作。再次感謝!! – crenfro 2012-04-22 23:38:31