我的应用程序在几次随机点击按钮后开始大幅减慢,有时标签和按钮完全消失,GUI 冻结(“X”按钮除外,它只是破坏程序,这是故意的)。
我的猜测是我想清除窗口的方式(我的 clear(self) 函数)不是清除它的正确方法,因为无论您单击哪个按钮,我都希望按钮保持不变,它们下面的所有内容都应该被清除. 或者也许这是我的类结构,我已经看到了很多其他方法来创建一个 tkinter 类......,也许我错过了我的代码中的核心内容?
到目前为止我的代码:
from tkinter import *
class LearnTkinter:
def __init__(self, top):
self.top = top
self.title = top.title("LearnTkinter")
self.configure = top.configure(bg="#d9d9d9", highlightbackground="grey", highlightcolor="darkgrey")
wWidth = top.winfo_reqwidth()
wHeight = top.winfo_reqheight()
x = int(top.winfo_screenwidth() / 2 - (wWidth / 2))
y = int(top.winfo_screenheight() / 3 - (wHeight / 2))
top.geometry(f"+{x}+{y}")
top.minsize(274,520)
self.Kill = Button(top,text="X",bg="#d9d9d9", command = top.destroy).place(height=54,width=72)
self.Infos = Button(top, text="i", bg="#d9d9d9", command=self.info_w).place(relx=0.25,height=54,width=72)
self.Help = Button(top, text="?", bg="#d9d9d9", command=self.help_w).place(relx=0.5,height=54,width=72)
self.Settings = Button(top, text="O", bg="#d9d9d9", command=self.settings_w).place(relx=0.75,height=54,width=72)
self.Frame1 = Frame(top, bg="black", relief="groove", borderwidth="8").place(rely=0.1,relheight=0.202,relwidth=1.004)
def clear(self):
for widget in top.winfo_children():
widget.destroy()
Button(top,text="X", bg="#d9d9d9", command=top.destroy).place(height=54,width=72)
Button(top, text="i", bg="#d9d9d9", command=self.info_w).place(relx=0.25,height=54,width=72)
Button(top, text="?", bg="#d9d9d9", command=self.help_w).place(relx=0.5,height=54,width=72)
Button(top, text="O", bg="#d9d9d9", command=self.settings_w).place(relx=0.75,height=54,width=72)
def info_w(self):
self.clear()
top.title("Info")
Label(top, text="This is going to be the information section ", bg="grey").place(rely=0.1, relwidth=1)
def help_w(self):
self.clear()
top.title("Help")
Label(top, text="This is going to be the help center", bg="grey").place(rely=0.1, relwidth=1)
def settings_w(self):
self.clear()
top.title("Settings")
top = Tk()
exa_gui = LearnTkinter(top)
top.mainloop()