1

I am trying to display an image in Tkinter canvas along with some text and I am running into the following error. Also, my mac doesn't show background colors for buttons when run using Spyder in anaconda (Spyder up-to-date).

My python code is:

from tkinter import *  
from PIL import ImageTk,Image  

def plot_best_batsmen():
    best_batsmen = dataset.loc[dataset.loc[dataset['Innings']>=15,'Average'].idxmax(),'Names']
    message = ("The best Batsman of the Tournament could possibly be: ", best_batsmen)
    canvas_width = 500
    canvas_height = 500
    root = Tk()
    root.geometry("600x600")
    root.title("New Window")
    canvas = Canvas(root, width=canvas_width, height=canvas_height)
    canvas.create_text(1, 10, anchor=W, text=message)
    img = ImageTk.PhotoImage(Image.open("prediction.jpg"))
    canvas.create_image(20, 20, anchor=NW, image=img)
    canvas.image = img
    canvas.pack()
    root.mainloop()

It's displaying an error message as follows when running:

Exception in Tkinter callback
Traceback (most recent call last):
  File "/anaconda3/lib/python3.7/tkinter/__init__.py", line 1705, in __call__
    return self.func(*args)
  File "/Users/vivekchowdary/Documents/PS3_Final_Project/Batsmen.py", line 110, in plot_best_batsmen
    canvas.create_image(20, 20, anchor=NW, image=img)
  File "/anaconda3/lib/python3.7/tkinter/__init__.py", line 2489, in create_image
    return self._create('image', args, kw)
  File "/anaconda3/lib/python3.7/tkinter/__init__.py", line 2480, in _create
    *(args + self._options(cnf, kw))))
_tkinter.TclError: image "pyimage3" doesn't exist

Code for buttons is as follows:

b1 = Button(root, text="Elbow Method", command=plot_elbow, bg="green", fg="white").pack(side = LEFT)
b2 = Button(root, text="K-Means Clustering", command=plot_kmeans, bg="blue", fg="white").pack(side = LEFT)
b3 = Button(root, text="Batsmen who scored 4 or more Hundreds", command=plot_hundreds, bg="#D35400", fg="white").pack(side = LEFT)
b4 = Button(root, text="Runs Scored by Various Players", command=plot_runs, bg="#117A65", fg="white").pack(side = LEFT)
b5 = Button(root, text="Best Batsmen", command=plot_best_batsmen, bg="#34495E", fg="white").pack(side = LEFT)
b6 = Button(root, text="Stop", command=root.destroy, bg="red", fg="white").pack(side = BOTTOM)

I want Tkinter to display the following image. But it's reporting an error instead. Can anyone please help me in solving this error?

1
  • Comments are not for extended discussion; this conversation has been moved to chat. Commented Apr 22, 2019 at 23:13

3 Answers 3

2

tkinter also has a class/function called Image. You also imported Image from PIL. You need to make sure which Image.open you are trying to use. tkinter.Image doesn't have an attribute 'open'.

Sign up to request clarification or add additional context in comments.

Comments

0

it seems that the error has nothing to do with your tkinter. To identify the problem, can you please just

from PIL import Image 
print(Image.open)

the result should look like the following if you have installed everything correctly.

< function open at 0x0000000008A26488 >

2 Comments

yes its printing <function open at 0x105928b70> when I type in the above code.
If it shows Image.open as a function (right before you use the function), then it should work. Unless, you have done from tkinter import * and overrode the first import. Generally speaking, do not use wildcard in import, you don't know what you have imported.
0

I figured out the answer myself and just felt like sharing it to help someone with the same doubt.

We need to import PIL after Tkinter as Tkinter also has its own class Image which doesn't have 'open' functionality and this Image from Tkinter replaces the Image from PIL if imported after PIL.

and the second thing which has to be done is to replace root = Tk() with root = Toplevel() because the problem is when Python/Tkinter is trying to build canvas from the button, it's essentially trying to create two windows under root and falling over.

Now, the code is importing the photo into the GUI and is working fine.

So, finally the code below is working fine: def plot_best_batsmen(): best_batsmen = dataset.loc[dataset.loc[dataset['Innings']>=15,'Average'].idxmax(),'Names'] message = ("The best Batsman of the Tournament could possibly be: ",best_batsmen) canvas_width = 400 canvas_height = 500 root = Toplevel() root.geometry("700x600") root.title("New Window") canvas = Canvas(root, width=canvas_width, height=canvas_height) canvas.create_text(1, 10, anchor=W, text=message) img = ImageTk.PhotoImage(Image.open("virat.jpg")) canvas.create_image(150, 20, anchor=NW, image=img) canvas.image = img canvas.pack() root.mainloop()

1 Comment

Did you even check other answers before saying you figured out by yourself?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.