更新标签时泄漏

发布于 2024-12-20 20:00:15 字数 462 浏览 0 评论 0原文

我是 tkinter 的新手,并且在我正在做的项目中跟踪到了代码中的时钟的内存泄漏。事实证明,更新标签时会发生内存泄漏,我在代码中得到的最简单的例子是:

import Tkinter as tk

class Display:
    def __init__(self, master):
        self.master = master
        self.tick()

    def tick(self):
        self.label = tk.Label(self.master, text = 'a')
        self.label.place(x=0,y=0)
        self.master.after(50, self.tick)

root = tk.Tk()
disp = Display(root)

如果有人能告诉我为什么会泄漏内存,我将不胜感激。

谢谢,马特

I'm new to tkinter and have traced a memory leak in a project I'm doing down to a clock in my code. It turns out the memory leak happens when updating a label, the simplest example I've got it down to in code is:

import Tkinter as tk

class Display:
    def __init__(self, master):
        self.master = master
        self.tick()

    def tick(self):
        self.label = tk.Label(self.master, text = 'a')
        self.label.place(x=0,y=0)
        self.master.after(50, self.tick)

root = tk.Tk()
disp = Display(root)

If somebody could tell me why this leaks memory I'd be grateful.

Thanks, Matt

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北笙凉宸 2024-12-27 20:00:15

问题是 tick 不断创建新标签。没有理由在这样的循环中创建多个标签,除非您确实需要越来越多的标签。您可以使用 configure 方法更新标签小部件的文本。

例如:

def tick(self):
    self.label.configure(text=`my new text`)
    self.after(50, self.tick)

The problem is that tick keeps creating new labels. There's no reason to create more than one label in a loop like this unless you really do need an ever increasing number of labels. You can update the text of a label widget by using the configure method.

For example:

def tick(self):
    self.label.configure(text=`my new text`)
    self.after(50, self.tick)
怀念你的温柔 2024-12-27 20:00:15

问题似乎是您正在创建标签而不销毁它们。每次创建新标签并将其放置在旧标签之上时,它仍然被引用,因此无法被垃圾收集。

这是一个稍微修改过的版本,不会泄漏......

import Tkinter as tk

class Display:
    def __init__(self, master):
        self.label = None
        self.master = master
        self.tick()

    def tick(self):
        if self.label:
            self.label.destroy()
        self.label = tk.Label(self.master, text = 'a')
        self.label.place(x=0,y=0)
        self.master.after(50, self.tick)

root = tk.Tk()
disp = Display(root)

The problem appears to be that you are creating Labels without destroying them. Each time you create a new label and place it over the old one, so it is still being referenced and thus can't be garbage collected.

Here is a slightly revised version that doesn't leak....

import Tkinter as tk

class Display:
    def __init__(self, master):
        self.label = None
        self.master = master
        self.tick()

    def tick(self):
        if self.label:
            self.label.destroy()
        self.label = tk.Label(self.master, text = 'a')
        self.label.place(x=0,y=0)
        self.master.after(50, self.tick)

root = tk.Tk()
disp = Display(root)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文