1
我有一個需要顯示各種圖像的label
對象。根據我的研究,以下腳本(下面)應成功更新我的label
對象中顯示的圖像。但是,當我撥打self.ImageViewer.configure(image=self.imgToDisp)
時,出現TypeError(見下文)。Tkinter(Python 3.5):在標籤對象上調用`.configure`時出現TypeError
我不完全確定錯誤是什麼意思,但它看起來像.configure
期待一個字符串?沒有人似乎遇到這個問題,所以我必須在下面的腳本語法錯誤。任何輸入讚賞。
腳本:
def getImgs(self):
folderPath = filedialog.askdirectory()
self.imgArray, self.imagePaths = batchImpt.importAllImgs(folderPath)
halThrThsnd.saveAllData(self.imgArray)
im = Image.open(self.imagePaths[0])
self.imgToDisp = PhotoImage(im)
self.imageViewer.configure(image = self.imgToDisp)
self.imageViewer.image = self.imgToDisp
錯誤:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\biegad1\AppData\Local\Continuum\Anaconda3\lib\tkinter\__init__.py", line 1549, in __call__
return self.func(*args)
File "//mspm1bnas50s/home58/biegad1/Python Scripts/GUI_0_1.py", line 40, in getImgs
self.imageViewer.configure(image = self.imgToDisp)
File "C:\Users\biegad1\AppData\Local\Continuum\Anaconda3\lib\tkinter\__init__.py", line 1330, in configure
return self._configure('configure', cnf, kw)
File "C:\Users\biegad1\AppData\Local\Continuum\Anaconda3\lib\tkinter\__init__.py", line 1321, in _configure
self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
TypeError: __str__ returned non-string (type TiffImageFile)
完全合作!那麼調用'PhotoImage'和'ImageTk.PhotoImage'有什麼區別? – DeeWBee
不同的是tkinter只能處理GIF圖像。所以編寫'self.imgToDisp = PhotoImage(im)'意味着你要求tkinter處理你的圖像,但它不能。所以你需要依賴PIL庫,它具有相同的名稱方法'PhotoImage()',但它屬於模塊'ImageTk' @DeeWBee –