+ -
当前位置:首页 → 问答吧 → Tk显示图片的问题

Tk显示图片的问题

时间:2011-04-27

来源:互联网

这是我的一个程序,运行是发现,def中的Label并没有显示图片,而主程序中却显示了,请问这是为什么呢?
  1. #!/usr/bin/python

  2. from Tkinter import *

  3. def doit(root):
  4.     img = PhotoImage(file = './logo.gif')
  5.     print img
  6.     Label(root, image = img).pack()  # This label failed to display the picture
  7.     Label(root, text = 'TEXT').pack()

  8. root = Tk()
  9. doit(root)
  10. img = PhotoImage(file = './logo.gif')
  11. print img
  12. Label(root, image = img).pack()      # This label succeeded to display the picture
  13. root.mainloop()
复制代码

作者: mengcq   发布时间: 2011-04-27

我知道了,需要加句柄防
  
   img = PhotoImage(file = './logo.gif')

    a = Label(root, image = img)
    a.image = img
    a.pack()  # This label failed to display the 止被“垃圾回收”了

作者: mengcq   发布时间: 2011-04-29