为什么在函数中创建的TKINTER映像不会显示?
此代码有效:
import tkinter
root = tkinter.Tk()
canvas = tkinter.Canvas(root)
canvas.grid(row = 0, column = 0)
photo = tkinter.PhotoImage(file = './test.gif')
canvas.create_image(0, 0, image=photo)
root.mainloop()
它显示了图像。
现在,此代码编译,但没有向我显示图像,我不知道为什么,因为它是同一代码,在类中:
import tkinter
class Test:
def __init__(self, master):
canvas = tkinter.Canvas(master)
canvas.grid(row = 0, column = 0)
photo = tkinter.PhotoImage(file = './test.gif')
canvas.create_image(0, 0, image=photo)
root = tkinter.Tk()
test = Test(root)
root.mainloop()
This code works:
import tkinter
root = tkinter.Tk()
canvas = tkinter.Canvas(root)
canvas.grid(row = 0, column = 0)
photo = tkinter.PhotoImage(file = './test.gif')
canvas.create_image(0, 0, image=photo)
root.mainloop()
It shows me the image.
Now, this code compiles but it doesn't show me the image, and I don't know why, because it's the same code, in a class:
import tkinter
class Test:
def __init__(self, master):
canvas = tkinter.Canvas(master)
canvas.grid(row = 0, column = 0)
photo = tkinter.PhotoImage(file = './test.gif')
canvas.create_image(0, 0, image=photo)
root = tkinter.Tk()
test = Test(root)
root.mainloop()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
变量
photo
是一个本地变量,该变量在实例化后会收集垃圾。该解决方案涉及保存对照片的引用,例如:如果您在“ TKINTER IMAGE未显示”上进行Google搜索,则第一个结果是:
为什么不出现我的tkinter images?
The variable
photo
is a local variable which gets garbage collected after the class is instantiated. The solution involves saving a reference to the photo, for example:If you do a Google search on "tkinter image doesn't display", the first result is this:
Why do my Tkinter images not appear?
问题是Python自动通过称为垃圾收集。解决方案是保存参考或创建新参考。
以下方式是:
self
增加参考计数并保存参考。在使用方法2时,您可以像我一样列出全局列表,也可以在类中使用列表。两者都可以工作。
一些有用的链接:
The problem is Python automatically deletes the references to the variable by a process known as Garbage Collection. The solution is to save the reference or to create a new reference.
The following are the ways:
self
to increase the reference count and to save the reference.While using method 2, you can either make a global list as i did or use list inside the class. Both would work.
Some useful links:
根据经验,每当您在缩进的代码块中创建图像时,您都需要安全地参考该图像。这是因为Python的自动化 垃圾收集 ,它以0何时收集0时。它会摧毁/将框架/页面/缩进的代码块丢弃。
典型的方式处理它是在全局名称空间中的某个位置列出了图像列表 ,并将您的图像引用添加到该列表中。这很方便,但效率不是很高,应用于小型应用程序。
更有效的方法是将属性绑定到您的小部件或班级,它为您提供了参考,正如布莱恩(Bryan)的回答中所提出的那样。如果您执行
self.image
或widget.image
,则分配了widget = tk.widget(..
之前。As a rule of thumb, whenever you create your image in an indented block of code you need to safe a reference to that image. This is because of the python's automated garbage collection and it collects everything with a refcount of 0 when it destroys/leaves that frame/page/indented block of code.
The canonical way to deal with it is to have a list of images somewhere in the global namespace and add your image-references to that list. This is convenient but not very efficient and should be used for small applications.
An more efficient way is to bound an attribute to your widget or class that holds that reference for you, as Bryan proposed in his answer. It doesn't make a difference if you do
self.image
orwidget.image
that was assignedwidget = tk.Widget(..
before. But this also might not the right approach if you want to use that image further even when the widget is destroyed and garbage collected.只需添加
全局照片
作为功能中的第一行。Just add
global photo
as the first line inside the function.