Reputation: 119
I am unable to understand and unable to solve a tkinter.TclError in this following code that i have written till now. Pls solve this question i have got homework to make a mp3 player program using python and i want Submitted it quickly. And also conivence i have given the tracback also. Code
From tkinter import
root = Tk()
root.title("Creeper Awww Man MP3 Player")
root.geometry("500x400")
# Create Playlist Box
playlist_box = Listbox(
root,
bg = "black",
fg = "#4666FF",
width = 60
)
playlist_box.pack(pady=20)
# Define Button Image For Controls
back_btn_img = PhotoImage(file='images/back.png')
forward_btn_img = PhotoImage(file='images/forward.png')
play_btn_img = PhotoImage(file='images/play.png')
pause_btn_img = PhotoImage(file='images/pause.png')
stop_btn_img = PhotoImage(file='images/stop.png')
# Create Button Frame
control_frame = Frame(root)
control_frame.pack(pady=20)
# Create Play/Stop etc Buttons
back_button = Button(control_frame, image=back_btn_img,)
forward_button = Button(control_frame, image=forward_btn_img,)
play_button = Button(control_frame, image=play_btn_img,)
pause_button = Button(control_frame, image=pause_btn_img,)
stop_button = Button(control_frame, image=stop_btn_img,)
back_button.grid(row=0, column=0, padx=10)
forward_button.grid(row=0, column=1, padx=10)
play_button.grid(row=0, column=2, padx=10)
pause_button.grid(row=0, column=3, padx=10)
stop_button.grid(row=0, column=4, padx=10)
root.mainloop()
And Error
Traceback (most recent call last):
File "mp3_player.py", line 19, in <module>
forward_btn_img = PhotoImage(file='images/forward.png')
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/tkinter/__init__.py", line 4061, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/tkinter/__init__.py", line 4006, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't recognize data in image file "images/forward.png"
If you could help me I would be Thankful of you all and I would appreciate it.
Regards,
Shiven
Upvotes: 1
Views: 826
Reputation: 15088
CASE 1:
Issue might be because png is not supported by your version of tkinter. So here is how to proceed using PIL
/Pillow
.
Start by installing it, say this in your terminal.
pip install Pillow
then...
from PIL import Image, ImageTk #import it
....
back_btn_img = ImageTk.PhotoImage(Image.open('images/back.png')) #instantiate the image
You can replicate the same format for the rest of the image files too.
One advantage is, with this, you can also resize you images.
CASE 2:
The error might also be because the file is not in the right format, are you sure you converted it correctly or is the file corrupted? You might want to check all these, and do the proper method for conversion(if converted by changing extension only). Keep in mind, if a file hello.jpg
is to be converted to png, you should use a software or an online website to do so, but not renaming the file to hello.png
.
Hope this helped, do let me know if any errors or doubts.
Cheers
Upvotes: 1
Reputation: 78
use PIL :
from tkinter import *
from PIL import ImageTk, Image
root = Tk()
root.title("Creeper Awww Man MP3 Player")
root.geometry("500x400")
# Create Playlist Box
playlist_box = Listbox(
root,
bg = "black",
fg = "#4666FF",
width = 60
)
playlist_box.pack(pady=20)
back_btn_img = ImageTk.PhotoImage(Image.open('back.png').resize((50, 50), Image.ANTIALIAS))
# .resize(xx, xx) ,, changing image size
# Create Button Frame
control_frame = Frame(root)
control_frame.pack(pady=20)
# Create Play/Stop etc Buttons
back_button = Button(control_frame, image=back_btn_img,)
back_button.grid(row=0, column=0, padx=10)
root.mainloop()
Upvotes: 0