按下按钮时刷新 tkinter 框架 [英] Refresh a tkinter frame on button press

查看:53
本文介绍了按下按钮时刷新 tkinter 框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 在 tkinter 中的两个帧之间切换 的代码来制作我的图形用户界面.我有一个带有刷新和重启按钮的框架.

I am utilising code from Switch between two frames in tkinter to make my GUI. I have a frame with refresh and restart buttons.

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

My original idea was for the restart button to go to the start page as in the code below but if this frame is called again it has the entries from the previous attempt still showing.

我已经为刷新按钮尝试了.destroy(),但是当我再次调用 PLG 框架时,我收到一条回溯消息.

I've tried.destroy() for the refresh button but then I get an traceback message when I call the PLG frame again.

对于重启按钮,如何关闭PLG框架,进入开始页面,然后再次选择PLG?

For the restart button, how would I close the PLG frame, go to the Start page and then be able to select PLG again?

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

For the refresh button, how would I remove the entries in the entry widget and text arrear so that another entry can be made and new answer returned?

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

对这两个元素的建议将不胜感激.

Advice on both elements would be appreciated.

推荐答案

第一步是让您的按钮调用正确的函数,而不是使用 lambda.除非您了解为什么以及何时使用 lambda,否则它通常只会使代码更难编写和理解.

The first step is to have your button call a proper function rather than using lambda. Unless you understand why and when to use lambda, it usually just makes the code harder to write and understand.

一旦你让它调用一个函数,你就可以使用该函数来清除条目.

Once you have it call a function, you can use the function to clear the entries.

示例:

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

这篇关于按下按钮时刷新 tkinter 框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆