It's Willem
It's Willem

Reputation: 816

How to replace the icon in a Tkinter app?

I am using Python 3.5.0 on Windows 10 and want to replace this:

with something else.

Upvotes: 48

Views: 124098

Answers (8)

Shub
Shub

Reputation: 468

If you are using CustomTkinter:

app.wm_iconbitmap('yt.ico')

CustomTkinter documentation

Upvotes: 1

Slava S.
Slava S.

Reputation: 31

Here's another way via Tcl command:

import tkinter as tk

window=tk.Tk()
window.tk.call('wm', 'iconphoto', win._w, tk.PhotoImage(file=r"my_icon.png"))
window.mainloop()

Upvotes: 0

Steven Summers
Steven Summers

Reputation: 5394

To change the icon you should use iconbitmap or wm_iconbitmap I'm under the impression that the file you wish to change it to must be an ico file.

import tkinter as tk

root = tk.Tk()
root.iconbitmap("myIcon.ico")

Upvotes: 49

Hany Alashram
Hany Alashram

Reputation: 11

from tkinter import *

root = Tk()

root.title('how to put icon ?')

root.iconbitmap('C:\Users\HP\Desktop\py.ico')
   
root.mainloop()

Upvotes: 1

Thingamabobs
Thingamabobs

Reputation: 8072

If you haven't an icon.ico file you can use an ImageTk.PhotoImage(ico) and wm_iconphoto.

import tkinter as tk
from PIL import Image, ImageTk

root = tk.Tk()

ico = Image.open('test.jpg')
photo = ImageTk.PhotoImage(ico)
root.wm_iconphoto(False, photo)

root.mainloop()

Note:

If default is True, this is applied to all future created toplevels as well. The data in the images is taken as a snapshot at the time of invocation.

Detailed implementations under different OS:

  • On Windows, the images are packed into a Windows icon structure. This will override an ico specified to wm iconbitmap, and vice versa.
  • On X, the images are arranged into the _NET_WM_ICON X property, which most modern window managers support. A wm iconbitmap may exist simultaneously. It is recommended to use not more than 2 icons, placing the larger icon first.
  • On Macintosh, this sets the Dock icon with the specified image.

See more

Supported formats since TkVersion 8.6 of tk.PhotoImage(filepath):

  • PNG
  • GIF
  • PPM/PGM

Therefore code can be simplified with a .png file to:

import tkinter as tk 
root = tk.Tk() 
photo = tk.PhotoImage(file = 'test.png')
root.wm_iconphoto(False, photo)
root.mainloop()

Upvotes: 30

Simao
Simao

Reputation: 611

Here is another solution, wich doesn't force you to use an ico file :

from tkinter import *

root = Tk()
root.geometry("200x200")
root.iconphoto(False, tk.PhotoImage(file='C:\\Users\\Pc\\Desktop\\icon.png'))
root.mainloop()

Upvotes: 4

Shaun
Shaun

Reputation: 101

input for tkinter

from tkinter import *

app = Tk()
app.title('Tk')
app.geometry('')

app.iconbitmap(r'C:\Users\User\PycharmProjects\HelloWorld\my.ico')
app.mainloop()

input for pyinstaller

pyinstaller --onefile -w -F --add-binary "my.ico;." my.py

Upvotes: 10

Jawad Saqib
Jawad Saqib

Reputation: 95

You must not have favicon.ico in the same directory as your code or namely on your folder. Put in the full Pathname. For examples:

from tkinter import *
root = Tk()

root.iconbitmap(r'c:\Python32\DLLs\py.ico')
root.mainloop()

This will work

Upvotes: 1

Related Questions