python - Tkinter(Python 3.5): TypeError when calling `.configure` on a label object

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

最佳答案

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

当然,您必须将 ImageTk 添加到导入语句中。我认为您已经这样做了:from PIL import Image。如果是,则修改为:from PIL import Image, ImageTk

关于python - Tkinter(Python 3.5): TypeError when calling `.configure` on a label object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38333226/

相关文章:

python - Python 中的元组真的是不可变的吗?

python - 导入关系中使用的 SQLAlchemy 模型?

python - 在 Tk 小部件中显示标准输出

python - 如何从组合框中获取属性并将其放入变量中?

python - 如何将参数从一个函数传递到另一个函数?

python - 独立移动数组中的行

python - 如何为函数 Python 中的局部变量赋值?

python - 解析 1000 年之前的日期

python - 当 implicitly_wait 不起作用时使用 find_element_by_link_text 的 NoSuchElement 异常?

python - 如何使用 tkinter 清除窗口