简体   繁体   English

如何使用tkinter / ttk在Python 3中显示图像?

[英]How can I display an image in Python 3 using tkinter/ttk?

The nub of the matter is, what am I doing wrong in the following code snippet? 问题的关键是,在以下代码片段中我在做什么错?

    from tkinter import *
    from tkinter.ttk import *

    root = Tk()

    myButton = Button(root)
    myImage = PhotoImage(myButton, file='myPicture.gif')
    myButton.image = myImage
    myButton.configure(image=myImage)

    root.mainloop()

The error message I get from idle3 is as follows: 我从idle3收到的错误消息如下:

    >>> 
    Traceback (most recent call last):
      File "/home/bob/Documents/Python/tkImageTest.py", line 9, in <module>
        myButton.configure(image=myImage)
      File "/usr/lib/python3.2/tkinter/__init__.py", line 1196, in configure
        return self._configure('configure', cnf, kw)
      File "/usr/lib/python3.2/tkinter/__init__.py", line 1187, in _configure
        self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
    TypeError: __str__ returned non-string (type Button)
    >>> 

This error message has me stumped, I simply don't understand what it is trying to say. 此错误消息使我感到困惑,我根本不明白它要说的是什么。 Any ideas? 有任何想法吗?

I would also appreciate suggestions for changes... 我也非常感谢您提出更改建议...

The error seems to point to the myButton argument passed to PhotoImage . 该错误似乎指向传递给PhotoImagemyButton参数。 As you noted in your comment, PhotoImage was treating the widget object as a string (there are several options of type string; see a list of PhotoImage options here ). 正如您在评论中指出的那样, PhotoImage将小部件对象视为一个字符串(有多个类型为string的选项;请参见此处的PhotoImage选项列表)。

Your code will work if you implement that line without referencing the myButton object: 如果在不引用myButton对象的情况下实现该行,则代码将起作用:

myImage = PhotoImage(file='myPicture.gif')

I'm not certain you need to alter the PhotoImage constructor. 我不确定您是否需要更改PhotoImage构造函数。 Look at the PhotoImage docs to determine the valid options (ie resource names) for that class. 查看PhotoImage文档,以确定PhotoImage的有效选项(即资源名称)。 Quoting the help file: 引用帮助文件:

Help on class PhotoImage in module tkinter: 关于模块tkinter中的PhotoImage类的帮助:

class PhotoImage(Image) 类PhotoImage(Image)

 | Widget which can display colored images in GIF, PPM/PGM format. | | Method resolution order: | PhotoImage | Image | builtins.object | | Methods defined here: | | __getitem__(self, key) | # XXX config | | __init__(self, name=None, cnf={}, master=None, **kw) | Create an image with NAME. | | Valid resource names: data, format, file, gamma, height, palette, | width. 

FYI: The easiest way to get to the docs from Python at the command line or from IDLE: 仅供参考:从Python在命令行或从IDLE获取文档的最简单方法是:

from tkinter import PhotoImage
help(PhotoImage)

And lastly, another useful link about this class is at http://tkinter.unpythonic.net/wiki/PhotoImage . 最后,有关此类的另一个有用链接是http://tkinter.unpythonic.net/wiki/PhotoImage

I tested the example with python 2.7.9, 3.2.5, 3.3.5, 3.4.3 in 32bit and 64bit. 我使用32位和64位的python 2.7.9、3.2.5、3.3.5、3.4.3测试了示例。 (Win 8.1 64bit) (Win 8.1 64位)

The code works. 该代码有效。

( in python 3.4.3 64bit I had first an error message. (在python 3.4.3 64bit中,我首先收到一条错误消息。

I've completely uninstalled 3.4.3 and then reinstalled. 我已经完全卸载了3.4.3,然后重新安装了。

Now, the example works also with 3.4.3 64 bit ) 现在,该示例也适用于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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM