PhotoImage:Tkinter PhotoImage 对象及其特性
http://tkinter.unpythonic.net/wiki/PhotoImage
我用 32 位和 64 位的 python 2.7.9、3.2.5、3.3.5、3.4.3 测试了这个例子。(Win 8.1 64位)
该代码有效。(即使没有枕头)
(在 python 3.4.3 64bit 中,我首先收到一条错误消息。
我已经完全卸载了 3.4.3,然后重新安装。
现在,该示例也适用于 3.4.3 64 位)
# basic code from >>
# http://tkinter.unpythonic.net/wiki/PhotoImage
# extra code -------------------------------------------------------------------------
from __future__ import print_function
try:
import tkinter as tk
except:
import Tkinter as tk
import sys
import platform
print ()
print ('python ', sys.version)
print ('tkinter ', tk.TkVersion)
print ()
print (platform.platform(),' ',platform.machine())
print ()
# basic code -------------------------------------------------------------------------
root = tk.Tk()
def create_button_with_scoped_image():
# "w6.gif" >>
# http://www.inf-schule.de/content/software/gui/entwicklung_tkinter/bilder/w6.gif
img = tk.PhotoImage(file="w6.gif") # reference PhotoImage in local variable
button = tk.Button(root, image=img)
# button.img = img # store a reference to the image as an attribute of the widget
button.image = img # store a reference to the image as an attribute of the widget
button.grid()
create_button_with_scoped_image()
tk.mainloop()