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...
我也很欣赏关于改变的建议……
2 个解决方案
#1
7
The error seems to point to the myButton
argument passed to PhotoImage
. 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的myButton参数。正如您在评论中提到的,PhotoImage将小部件对象作为一个字符串处理(有几个类型字符串的选项;请参见这里的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. Look at the PhotoImage
docs to determine the valid options (i.e. resource names) for that class. Quoting the help file:
我不确定您是否需要修改PhotoImage构造函数。查看PhotoImage文档,以确定该类的有效选项(即资源名称)。引用的帮助文件:
Help on class PhotoImage in module tkinter:
模块tkinter上的类PhotoImage帮助:
class PhotoImage(Image)
类PhotoImage(图片)
| 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:
FYI:在命令行或空闲时从Python获取文档的最简单方法:
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。
#2
-1
I tested the example with python 2.7.9, 3.2.5, 3.3.5, 3.4.3 in 32bit and 64bit. (Win 8.1 64bit)
我用python 2.7.9, 3.2.5, 3.3.5, 3.4.3, 32位和64位测试了这个例子。(赢8.1 64位)
The code works.
工作的代码。
( in python 3.4.3 64bit I had first an error message.
(在python 3.4.3 64位中,我首先有一个错误消息。
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()
#1
7
The error seems to point to the myButton
argument passed to PhotoImage
. 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的myButton参数。正如您在评论中提到的,PhotoImage将小部件对象作为一个字符串处理(有几个类型字符串的选项;请参见这里的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. Look at the PhotoImage
docs to determine the valid options (i.e. resource names) for that class. Quoting the help file:
我不确定您是否需要修改PhotoImage构造函数。查看PhotoImage文档,以确定该类的有效选项(即资源名称)。引用的帮助文件:
Help on class PhotoImage in module tkinter:
模块tkinter上的类PhotoImage帮助:
class PhotoImage(Image)
类PhotoImage(图片)
| 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:
FYI:在命令行或空闲时从Python获取文档的最简单方法:
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。
#2
-1
I tested the example with python 2.7.9, 3.2.5, 3.3.5, 3.4.3 in 32bit and 64bit. (Win 8.1 64bit)
我用python 2.7.9, 3.2.5, 3.3.5, 3.4.3, 32位和64位测试了这个例子。(赢8.1 64位)
The code works.
工作的代码。
( in python 3.4.3 64bit I had first an error message.
(在python 3.4.3 64位中,我首先有一个错误消息。
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()