[英]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.