按下按钮时刷新tkinter框架

时间:2017-07-12 18:04:21

标签: python-3.x tkinter

我正在利用Switch between two frames in tkinter中的代码制作我的GUI。我有一个带刷新和重启按钮的框架。

我最初的想法是让重启按钮转到开始页面,如下面的代码所示,但是如果再次调用此框架,它仍会显示上一次尝试中的条目。

我已经尝试了刷新按钮的刷子按钮,但是当我再次调用PLG帧时,我得到一条回溯消息。

对于重启按钮,如何关闭PLG帧,转到“开始”页面,然后再次选择PLG?

对于刷新按钮,如何删除条目小部件中的条目和文本欠费,以便可以创建另一个条目并返回新的答案?

class PLG(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="Enter the engine size (cc) below", font=controller.title_font)
        label.pack(side="top", fill="x", pady=10)
        vcmd = (self.register(self.onValidate), '%S')
        self.weight_entry = tk.Entry(self, validate='key', vcmd = vcmd)
        self.weight_entry.pack(pady = 10)
        tk.Button(self, text='Click here to display price', command=self.show_option).pack()
        self.text = tk.Text(self)
        self.text.pack(pady = 10)
        self.text.config(state='disabled')
        restart_button = tk.Button(self, text="Restart",
              command=self.restart)
        restart_button.pack()
        refresh_button = tk.Button(self, text="Refresh", command=self.refresh).pack() 
        refresh_button.pack()  

    def onValidate(self,S):
    if S in ['0','1','2', '3', '4', '5', '6', '7', '8', '9']: 
        return True
    else:
        self.bell() # adds a sound effect to error
        self.text.delete(1.0, tk.END) # deletes the error message if valid entry provided
        self.text.insert(tk.END, "Invalid entry.  Please try again.") # displays an error message if a number not provided in entry widget
        return False

    def restart(self):
        self.refresh()
        show_frame("StartPage")

    def refresh(self):
        self.text.config(state='normal')
        self.weight_entry.delete(0,tk.END)
        self.text.delete("1.0", "end")

对这两个要素的建议将不胜感激。

3 个答案:

答案 0 :(得分:0)

第一步是让按钮调用正确的功能,而不是使用lambda。除非您了解使用lambda的原因和时间,否则通常只会使代码更难以编写和理解。

一旦调用了函数,就可以使用该函数清除条目。

示例:

class PLG(tk.Frame):
    def __init__(self, parent, controller):
        ...
        tk.Button(self, text="Restart", command=self.restart)
        tk.Button(self, text="Refresh", command=self.refresh)
        ...

    def restart(self):
        self.refresh()
        self.controller.show_frame("StartPage")

    def refresh(self):
        self.weight_entry.delete(0, "end")
        self.text.delete("1.0", "end")

答案 1 :(得分:0)

OP的问题是清除输入字段,因此当您希望看到用于新输入的空字段时,先前的输入仍不在页面中。我在发布完成的代码时忽略了OP原始代码的功能,这些功能与他的问题无关,因此可以轻松地在整个上下文中看到解决方案。我一直在寻求使用Bryan Oakley著名的有关该主题的教程中的相同帧切换代码来解决此问题。我还提供了一个使用grid_remove而不是tkraise的替代版本,因为这是我解决了在用户尝试浏览页面时试图参与焦点遍历的,经常活动但看不见的帧的问题。它还可以防止所有帧都试图保持相同的大小。

import tkinter as tk

class SampleApp(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}

        # alternate ways to create the frames & append to frames dict: comment out one or the other

        for F in (StartPage, PLG):
            page_name = F.__name__
            frame = F(parent=container, controller=self)
            self.frames[page_name] = frame
            frame.grid(row=0, column=0, sticky="nsew")

        # self.frames["StartPage"] = StartPage(parent=container, controller=self) 
        # self.frames["PLG"] = PLG(parent=container, controller=self)
        # self.frames["StartPage"].grid(row=0, column=0, sticky="nsew")
        # self.frames["PLG"].grid(row=0, column=0, sticky="nsew")

        self.show_frame("StartPage")

    # alternate version of show_frame: comment out one or the other

    def show_frame(self, page_name):
        for frame in self.frames.values():
            frame.grid_remove()
        frame = self.frames[page_name]
        frame.grid()

    # def show_frame(self, page_name):
        # frame = self.frames[page_name]
        # frame.tkraise()

class StartPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller

        label = tk.Label(self, text="start page")
        label.pack(side="top", fill="x", pady=10)

        button1 = tk.Button(self, text="Go to Page One", command=lambda: controller.show_frame("PLG"))
        button1.pack()        

        button2 = tk.Button(self, text="focus traversal demo only")
        button2.pack()
        button2.focus_set()

        button3 = tk.Button(self, text="another dummy button")
        button3.pack()

        lbl = tk.Label(self, text="tkraise messes up focus traversal\nwhich you can see by testing the two versions of show_frame.()\nUsing grid_remove instead of tkraise solves that,\nwhile preventing frames from being unable to resize to fit their own contents.")
        lbl.pack()

class PLG(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="Enter something below; the two buttons clear what you type.")
        label.pack(side="top", fill="x", pady=10)
        self.wentry = tk.Entry(self)
        self.wentry.pack(pady = 10)
        self.text = tk.Text(self)
        self.text.pack(pady = 10)
        restart_button = tk.Button(self, text="Restart", command=self.restart)
        restart_button.pack()
        refresh_button = tk.Button(self, text="Refresh", command=self.refresh) 
        refresh_button.pack()  

    def restart(self):
        self.refresh()
        self.controller.show_frame("StartPage")

    def refresh(self):
        self.wentry.delete(0, "end")
        self.text.delete("1.0", "end")
        # set focus to any widget except a Text widget so focus doesn't get stuck in a Text widget when page hides
        self.wentry.focus_set()

if __name__ == "__main__":
    app = SampleApp()
    app.mainloop()

答案 2 :(得分:0)

简单方法: 只需使用按钮调用该窗口或绑定其中的框架即可。

对于Windows刷新效果很好。