DeeWBee
DeeWBee

Reputation: 695

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

I have a label object that needs to display a variety of images. Based on my research, the following script (below) should successfully update the image displayed in my label object. However, when I call self.ImageViewer.configure(image=self.imgToDisp), I get a TypeError (seen below).

I'm not entirely sure what the error means, but it looks like .configure is expecting a string? Nobody else seems to encounter this problem so I must be doing something syntactically wrong with the script below. Any input is appreciated.

Script:

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

Error:

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)

Upvotes: 1

Views: 93

Answers (1)

Billal BEGUERADJ
Billal BEGUERADJ

Reputation: 22804

You need to change self.imgToDisp = PhotoImage(im) to self.imgToDisp = ImageTk.PhotoImage(im)

Of course, you must add ImageTk to your import statements. I think you already did: from PIL import Image. If So, modify it to: from PIL import Image, ImageTk.

Upvotes: 2

Related Questions