简体   繁体   English

Python Tkinter:在函数内部创建画布会导致内存泄漏吗?

[英]Python Tkinter: creating canvas inside function will cause memory leak?

I have a function that creates a new canvas every time it's called我有一个每次调用时都会创建一个新画布的函数

def show_cities():
    height = 20
    lb.selection_clear(0, tk.END)
    
    canvas = tk.Canvas(root, width=150, height=350)
    canvas.place(x=0, y=0)
        
    for i in cities_to_select:
        print(i)
        city = tk.Label(canvas, text=cities[i])
        city.place(x=10, y=height)
        height += 20;
    cities_to_select[:] = []

Will the canvas object be recreated with every function call or will it be just created along old one? canvas 对象会在每次函数调用时重新创建,还是只会沿旧函数调用创建? Will such code cause memory leak?这样的代码会导致内存泄漏吗?

Each time show_cities is called, a new canvas will be created.每次调用show_cities时,都会创建一个新画布。 Since you aren't destroying the old canvas, then yes, this will create a small memory leak.既然你没有破坏旧的画布,那么是的,这会造成一个小的内存泄漏。

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

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