简体   繁体   English

Python 3.7 Tkinter框架参考不断增加

[英]Python 3.7 Tkinter frame reference ever increasing

Apologies I'm pretty new at this... 抱歉,我对此很陌生...

I can't seem to 'overwrite' my tkinter frame. 我似乎无法“覆盖”我的Tkinter框架。 I've written this to demonstrate my problem: 我写这个来证明我的问题:

from tkinter import *

bgCol = 'red'

def destroyMasterFrame():
    global master
    master.destroy()
    createMasterFrame()

def createMasterFrame():
    global master, bgCol

    if bgCol == 'green':
        bgCol = 'red'
    else:
        bgCol='green'

    master = Frame(root, bg=bgCol)
    master.bind("<Button-1>", lambda event: destroyMasterFrame())
    master.pack(side='top', fill='both', expand='yes')
    print(master)

root = Tk()
root.geometry('200x200+100+100')

createMasterFrame()

root.mainloop()

So every time I click the coloured square I thought I was destroying the frame and starting over, but print(master) gives me... 因此,每次单击彩色正方形时,我都以为自己是在破坏框架并重新开始,但是print(master)给了我...

.!frame
.!frame2
.!frame3
.!frame4
.!frame5

...for each time I click. ...每点击一次 I know I'm probably missing something basic here but any help would be much appreciated. 我知道我可能在这里缺少一些基本的知识,但是任何帮助将不胜感激。 Thanks. 谢谢。

Edit: 编辑:

If I add an image to the frame that I'm destroying, it seems to keep it, along with the frame reference, in the memory which seems to fill the memory quickly with repeated use. 如果将图像添加到要销毁的框架中,则似乎将其与框架参考一起保留在内存中,这似乎可以通过反复使用迅速填充内存。 This code demonstrates what I mean... How do I destroy the frame, its reference, and wipe everything contained within it from memory?... 这段代码演示了我的意思……如何销毁框架及其参考,并从内存中擦除其中包含的所有内容?

from tkinter import *

def destroyMasterFrame():
    global master
    master.destroy()
    createMasterFrame()

def createMasterFrame():
    global master

    master = Frame(root, bg='green')
    master.pack(side='top', fill='both', expand='yes')

    img = PhotoImage(file='test.png')
    label = Label(master, image=img)
    label.image = img
    label.bind("<Button-1>", lambda event: test())
    label.pack(side='top', fill='both', expand='yes')

    print(master)

def test():
    global myrange
    myrange = 1000
    for x in range(myrange):
        createMasterFrame()
        destroyMasterFrame()

root = Tk()
root.geometry('200x200+100+100')

createMasterFrame()

root.mainloop()

Thanks again for your help! 再次感谢你的帮助!

You are realy deleting the frame: 您确实要删除框架:

def destroyMasterFrame():
    master.destroy()
    print(master)

If you check the frame is deleted. 如果您检查框架被删除。
The reference this stack is still alive. 该堆栈的引用仍然有效。
Anyhow just the Frame is deletet but not the reference. 无论如何,仅删除框架,而不删除参考。
Therefor tkinter still counts up / even if the frame isnt behind. 因此,即使帧不在后面,tkinter仍会向上计数。
So dont worry and keep going this way. 因此,不要担心,请继续这样做。
Hint: You dont need to user global twice. 提示:您不需要两次全局用户。

EDIT: (based on your comment) 编辑:(根据您的评论)

Should work without clearing reference (no idea how to). 应该在不清除引用的情况下工作(不知道如何做)。
There should be a limit 2^63 <- max int in python. python中应该有一个2 ^ 63 <-max int的限制。

Test case: 测试用例:

myrange = 100000
for x in range(myrange):
    createMasterFrame()
    destroyMasterFrame()
    if x == myrange-1:
        print(master)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM