0

我的应用程序在几次随机点击按钮后开始大幅减慢,有时标签和按钮完全消失,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()
4

1 回答 1

1

您的问题是您每次迭代都在clear函数中创建4新按钮。

  • 例如:如果您一开始有 5 个小部件(4 个按钮 + 1 个框架)。在第一次调用clear函数后,您创建了5 * 4新按钮!第二次调用功能按钮时,您创建了5 * 4 * 4新按钮。这是80GUI 中的新小部件。在第 4 次通话之后,您已经有了1280新按钮!
  • 你可以想象它有太多的小部件,GUI 会冻结。

因此,如果您在之前或之后for loop(而不是期间!)移动按钮的创建,它将使您的滞后消失:

def clear(self):
    for widget in self.top.winfo_children():
        widget.destroy()

    Button(self.top, text="X", bg="#d9d9d9", command=self.top.destroy).place(height=54,width=72)
    Button(self.top, text="i", bg="#d9d9d9", command=self.info_w).place(relx=0.25,height=54,width=72)
    Button(self.top, text="?", bg="#d9d9d9", command=self.help_w).place(relx=0.5,height=54,width=72)
    Button(self.top, text="O", bg="#d9d9d9", command=self.settings_w).place(relx=0.75,height=54,width=72)

还有一件事:

  • LearnTkinter类的所有方法中,您都指的是top变量。
  • 但是当你调用这个变量时,你没有使用前缀self.top,而只是使用top.
  • 在您的所有功能/方法中更改此设置。
  • 它之所以有效,是因为您还(偶然)top在程序中命名了变量。

top变量:

top = Tk() #You are referring in you 'LearnTkinter' class to this variable. Not the one 'inside' the class.
exa_gui = LearnTkinter(top)
top.mainloop()

我知道在 python 中你实际上指向的是同一个对象。但是这个工作的原因是同一个名字。尝试更改它,您会发现它不起作用。

于 2021-01-23T21:13:57.147 回答