2012-07-22 77 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

相關問題