+ -
当前位置:首页 → 问答吧 → 在 Tkinter中显示图片,显示不出来,帮忙看看

在 Tkinter中显示图片,显示不出来,帮忙看看

时间:2011-08-03

来源:互联网

from Tkinter import *

class Flash:
  def __init__(self, root):
  self.root = root
  self.cnt = 0
  canvas = Canvas(self.root, width = 800, height = 600, bg = 'green')
  self.canvas = canvas
  photo = PhotoImage(file = 'google.gif')
  self.item = canvas.create_image(10, 10, image=photo)
   
  canvas.pack()

root = Tk()
flash = Flash(root)
root.mainloop()

作者: mark063   发布时间: 2011-08-03

you need to keep a reference to the photo object, otherwise, it will be out of the scope and be garbage collected.

change:

Python code
  photo = PhotoImage(file = 'google.gif')
  self.item = canvas.create_image(10, 10, image=photo)
    


to: 
Python code
  
  self.photo = PhotoImage(file = 'google.gif')
  self.item = canvas.create_image(10, 10, image=self.photo)
    

作者: panghuhu250   发布时间: 2011-08-03