2012-07-22 120 views
5

问题的关键是,我在下面的代码片段中做错了什么?如何使用tkinter/ttk在Python 3中显示图像?

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

的错误信息,我从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) 
    >>> 

此错误消息有我难住了,我根本不明白它是什么想说的。有任何想法吗?

我还希望更改建议...

+0

BTW我已经检查这个参考http://effbot.org/tkinterbook/photoimage.htm - 你会看到我的代码片段看起来非常相似! – Bobble 2012-07-22 07:24:28

+0

该错误似乎指向传递给'PhotoImage()'的'myButton'参数。我不相信'PhotoImage()'引用了一个widget对象,所以这可能会导致错误。尝试没有它的行,如'myImage = PhotoImage(file ='myPicture.gif')' – gary 2012-07-22 18:32:07

+1

@Gary,似乎这样做。我被一些文档(以及我产生的一些其他错误)误导为认为'PhotoImage'需要明确引用根窗口。经过一些更多的调整后,我发现可以通过'PhotoImage'构造函数的另一个配置选项来提供对根或者按钮本身的引用,'PhotoImage(master = myButton,file ='myFile.gif')',但是我写的方式,它看起来像Tkinter的名字,应该是一个字符串,ofc。 – Bobble 2012-07-23 03:09:48

回答

7

的错误似乎指向myButton参数传递给PhotoImage。正如您在评论中所述,PhotoImage将小部件对象视为字符串(有几个字符串类型的选项;请参阅PhotoImage选项列表here)。

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

我不能确定你需要改变PhotoImage构造:如果实现该行没有引用myButton对象

您的代码将工作。查看PhotoImage文档以确定该类的有效选项(即资源名称)。引用帮助文件:

帮助阶级光象模块Tkinter的:

类光象(图)

| 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:去的文档的最简单方法从Python在命令行或从IDLE:

from tkinter import PhotoImage 
help(PhotoImage) 

最后,关于此课程的另一个有用链接是http://tkinter.unpythonic.net/wiki/PhotoImage

+1

非常感谢加里!你对'主'参考也是正确的。这是我用多线程进行的一些实验(最好不要问!)。 – Bobble 2012-07-24 01:33:32

+0

@Bobble顺便说一句,如果这回答了这个问题,请将其标记为接受的答案(这看起来可以解决一些问题)。 – gary 2012-09-12 15:21:50

-1

我用32位和64位的python 2.7.9,3.2.5,3.3.5,3.4.3测试了这个例子。 (Win 8.1 64bit)

该代码有效。

(在Python 3.4.3 64位我第一次的错误消息。

我已经完全卸载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() 
+0

你确定这是一个答案,更具体地说是python-3.x的答案吗? – Deduplicator 2015-05-29 20:34:15

+0

一个好的答案不仅是一个可行的答案,而且是一个有记录的答案。如果OP不理解它,没有意义的提供代码。他/她应该从答案中学到一些东西,而不是仅仅复制粘贴它,这样他/她最终会在下一次再次提出同样的问题,只是在不同的背景下。 – ShellFish 2015-05-29 20:47:27

+0

问题是,gary和Tkinter Wiki对我的响应在Python中无效3.4.3 – 2015-05-29 21:31:43