How do I setup a window Icon using Tkinter?

2k Views Asked by At

I'm trying to set up a window Icon so it would appear right next to the name of a dictionary I'm doing. When I run the code, I get the following problem:

Traceback (most recent call last):
  File "/Users/sergioley-languren/Latin_app/windows.py", line 20, in <module>
    window.iconphoto(False, tk.PhotoImage(file='/Users/sergioley-languren/home/Latin_app/Logo.jpeg'))
  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 "/Users/sergioley-languren/Latin_app/Logo.jpeg"

When I tried to use the iconbitmap tkinter function with the .jpeg file converted to a .ico picture, it worked, but I got a blank page icon instead. The code below is the problematic code:

### Imports
import tkinter as tk
from tkinter import *
from pathlib import Path

##Extra Code
home = print(Path.home())
### Application screen code
window = Tk()

##Frame
frame1 = Frame(window)
frame1.pack()

## Window Title
window.title("Latin Unit 1 Dictionary - Based on Orion Academy Latin I , Unit 1")
window.geometry('1110x950')

#App Icon
window.iconphoto(False, tk.PhotoImage(file='/Users/sergioley-languren/home/Latin_app/Logo.jpeg'))

## Window Main Loop
window.mainloop()

Could anyone tell me how to fix it? (If it helps, I'm using MacOs Catilina.)

2

There are 2 best solutions below

6
jizhihaoSAMA On BEST ANSWER

tkinter.PhotoImage only support GM, PPM, GIF, PNG image.Your image is JPEG,for sure it will raise Exception.

There are some solutions,but the direct way is to change the image extension to ico and use iconbitmap()

from PIL import Image

img = Image.open(r"xxx.jpeg") # your jpeg image path
img.save(r"Icon.ico")

This will generate a new ico image in your current path.

Then you can use iconbitmap('Icon.ico') directly.

0
umakanth saikumar On

This works to set icon for Tkinter App

from tkinter import *
window =Tk()
##set title for window
window.title("Tkinter App")
##set window icon  
window.iconphoto(False,PhotoImage(file='logo.png'))
window.mainloop()