Tkinter(Python 3.5):在标签对象上调用`.configure`时出现TypeError

时间:2016-07-12 15:32:15

标签: python python-3.x tkinter

我有一个label对象需要显示各种图像。根据我的研究,以下脚本(下面)应该成功更新我的label对象中显示的图像。但是,当我调用self.ImageViewer.configure(image=self.imgToDisp)时,我得到一个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)

1 个答案:

答案 0 :(得分:2)

您需要将self.imgToDisp = PhotoImage(im)更改为self.imgToDisp = ImageTk.PhotoImage(im)

当然,您必须在导入语句中添加ImageTk。我想你已经做过:from PIL import Image。如果是,请将其修改为:from PIL import Image, ImageTk