【问题标题】:Python 3 tkinter Photoimage "pyimage1" doesn't existPython 3 tkinter Photoimage“pyimage1”不存在
【发布时间】:2015-05-29 20:52:41
【问题描述】:

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 64bit)

代码有效。 (即使没有枕头)

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

【问题讨论】:

  • 我已经编辑了我的帖子:代码有效。 (即使没有枕头)(在 python 3.4.3 64bit 中我首先收到一条错误消息。我已经完全卸载 3.4.3 然后重新安装。现在,该示例也适用于 3.4.3 64 位)

标签: python-3.x tkinter photoimage


【解决方案1】:

这是通过将root.Tk() 替换为root.Toplevel() 来完成的

【讨论】:

    【解决方案2】:

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

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

    更新

    鉴于相同的代码在我的机器上运行,而不是在你的机器上运行,我想我会补充一下我是如何获得这个版本的。我使用chocolatey (choco install python) 安装了 python,然后从gohlke 站点添加了 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" 但我怀疑这是否相关。

    【讨论】:

    • @patthoyts 感谢您的快速回复。这不是我的代码,它是 Tkinter Wiki 的代码。我还安装了 Pillow(在 2.7.9 和 3.4.3 中)。你刚刚更改了代码示例吗?
    • @patthoyts:对于 PhotoImage 问题,tk.Button 和 ttk.Button 没有区别(2.7.9 有效,3.4.3 无效)
    • 我根本没有更改您的代码。我什至创建了一个同名的小 gif。关于使用 ttk 的评论是关于按钮和其他控件的外观——与图像处理无关。 Tk 按钮看起来像 WIndows 95,而 ttk 按钮看起来像它们。
    • 我已经编辑了我的帖子:代码有效。 (即使没有枕头)(在 python 3.4.3 64bit 中我首先收到一条错误消息。我已经完全卸载 3.4.3 然后重新安装。现在,该示例也适用于 3.4.3 64 位)
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-19
      • 2020-08-22
      • 2014-06-07
      • 2022-07-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多