[英]Tkinter not drawing image to canvas
Tkinter不会通过其他方法在画布上绘制图像。
示例1起作用,示例2不起作用。 有人可以解释为什么吗?
def init_gui(self):
window = tkinter.Tk()
self.canvas = tkinter.Canvas(self.window, width=1000, height=500)
photo = PIL.ImageTk.PhotoImage(image=PIL.Image.fromarray(self.img))
self.canvas.create_image(0, 0, image=photo, anchor=tkinter.NW)
self.canvas.pack()
window.mainloop()
pass
def init_gui(self):
window = tkinter.Tk()
self.canvas = tkinter.Canvas(self.window, width=1000, height=500)
self._draw_img() # the exact same code, only in another method
window.mainloop()
pass
def _draw_img(self):
photo = PIL.ImageTk.PhotoImage(image=PIL.Image.fromarray(self.img))
self.canvas.create_image(0, 0, image=photo, anchor=tkinter.NW)
self.canvas.pack()
pass
如果我没记错的话,当您显示的图像超出范围时,Tkinter会出现问题(因为这是局部变量)。 尝试使photo
成为类的属性(通过在_draw_image
函数中用self.photo
替换photo
),看看是否可以解决问题。
这有帮助吗?
编辑
有关更完整的说明,请访问以下网站: http : //effbot.org/pyfaq/why-do-my-tkinter-images-not-appear.htm
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.