0

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()
4

3 回答 3

3

这是通过root.Tk()替换root.Toplevel()

于 2018-11-07T13:51:08.020 回答
1

在 Windows 7 上运行完全相同版本的 Python 3.4 时,您的脚本对我来说很好。我怀疑唯一的区别是我还通过从这个存储库下载了 wheel 包来安装 Pillow 。我认为这包括您需要的图像支持。

附带说明:在 Windows 上使用 ttk 包小部件来获取实际上看起来适合平台的按钮。然后import tkinter.ttk as ttk使用ttk.Button代替tk.Button.

更新

鉴于相同的代码在我的机器上运行,而不是你的,我想我会添加我如何获得这个版本。我使用Chocolatey ( )安装了 python ,然后从gohlke站点choco install python添加了 Pillow、lxml、numpy、requests 和 simplejson 。

检查 Tk 版本,我发现我得到了 Tk 8.6.1,所以我怀疑你正在选择本地安装的 Tcl/Tk 安装,而不是你的 Python 版本。尝试确保您的 PATH 中没有安装 Tk 并查看是否可以解决问题。我的输出是:

python     3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:44:40) [MSC v.1600 64 bit (AMD64)]
tkinter    8.6

测试TK位置:

>>> from tkinter import *
>>> root = Tk()
>>> root.eval('set tk_library')
'C:\\opt\\Python34\\tcl\\tk8.6'

我实际上是通过使用说服 python 安装到c:\opt\Python我认为的,choco install python -ia "TARGETDIR=c:\opt\Python"但我怀疑这是否相关。

于 2015-05-29T21:52:45.977 回答
0

您必须关闭所有打开的窗口,这些窗口挂在内存中。如果你有错误并且没有销毁窗口,下次启动时没有启动有几个窗口。查看!

于 2015-10-19T09:26:00.340 回答