Python 3.2 tkinter创建一个结果框来显示输出结果[英] Python 3.2 tkinter create a results frame to display output

本文是小编为大家收集整理的关于Python 3.2 tkinter创建一个结果框来显示输出结果的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我目前对餐厅进行了工作菜单测试,该菜单测试读取由Checkbuttons收到的输入,将它们进行比较以纠正答案,然后显示A 好工作 messagebox或a 继续工作 消息框.我还设置了它,以便如果他们没有正确解决问题,它将打印出他们回答的内容,然后打印出正确的答案.而不是简单地打印他们做错了什么,我想拥有最终的 结果 屏幕,该屏幕的工作(或类似的内容)或印刷错误与正确的答案是否正确.这是我的代码,向您展示了我到目前为止如何实施所有内容,就像我说的一切正常一样,我只是想找出一种使其更具呈现的方法,并且无法找到一种方法来做到这一点./p>

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)是我进行比较的地方,并打印要在结果屏幕上显示的输出,这也是我弹出消息框的地方.

推荐答案

我会从TK继承GUI,
然后将不同的"窗口"放入框架中.
然后,您可以在框架上使用grid_forget()或destroy()使其消失.
如果您选择不销毁它,则在框架上使用grid()将其重新出现.
这是一个简化的演示,
在3000ms显示结果框架(rframe)之后,原始帧(Aframe)重新出现.
请参阅底部的result_screen和go_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()

本文地址:https://www.itbaoku.cn/post/2091158.html

问题描述

I currently have a working menu test for my restaurant that reads input received by checkbuttons, compares them to correct answers and then displays either a good job messagebox or a keep working messagebox. I also have it set up so that if they do not get the questions right, it prints what they answered followed by the correct answer. Instead of the message boxes and simply printing what they did wrong I would like to have a final results screen that either has a good job (or something along those lines) message or has the printed wrong vs correct answers on it. Here is my code to show you how I have implemented everything so far, like I said everything is working I am just trying to figure out a way to make it more presentable and haven't been able to find a way to do this.

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) is where I do my comparisons and print the output that I would like to appear on the results screen, this is also where I have the messageboxes pop up.

推荐答案

I'd have the GUI inherit from Tk,
and then put the different "windows" in Frames.
You could then use grid_forget(), or destroy(), on a Frame to make it disappear.
Use grid() on a frame to make it reappear, if you chose to not destroy it.
Here's a simplified demonstration,
where the original frame (aFrame) reappears after 3000ms of displaying the result frame (rFrame).
See the result_screen and go_back methods at the bottom:

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()