5

問題の要点は、次のコード スニペットで何が間違っているのかということです。

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

このエラー メッセージには困惑しています。何を言おうとしているのか理解できません。何か案は?

また、変更の提案をいただければ幸いです...

4

2 に答える 2

7

myButtonエラーは、に渡された引数を指しているようPhotoImageです。コメントで指摘したようにPhotoImage、ウィジェット オブジェクトを文字列として扱っていました (文字列型のオプションがいくつかあります。ここで PhotoImage オプションのリストを参照してください)。

myButtonオブジェクトを参照せずにその行を実装すると、コードが機能します。

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

PhotoImageコンストラクターを変更する必要があるかどうかはわかりません。PhotoImageドキュメントを参照して、そのクラスの有効なオプション (リソース名など) を判断してください。ヘルプファイルの引用:

モジュール tkinter のクラス PhotoImage に関するヘルプ:

クラス 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.

参考: Python からコマンド ラインまたは IDLE からドキュメントにアクセスする最も簡単な方法:

from tkinter import PhotoImage
help(PhotoImage)

最後に、このクラスに関する別の便利なリンクがhttp://tkinter.unpythonic.net/wiki/PhotoImageにあります。

于 2012-07-22T18:17:34.137 に答える
-1

この例は、32 ビットおよび 64 ビットの python 2.7.9、3.2.5、3.3.5、3.4.3 でテストしました。(Win8.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()
于 2015-05-29T20:27:31.093 に答える