34

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

with something else.

martineau
  • 119,623
  • 25
  • 170
  • 301
It's Willem
  • 656
  • 3
  • 8
  • 15

8 Answers8

33

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")
Brionius
  • 13,858
  • 3
  • 38
  • 49
Steven Summers
  • 5,079
  • 2
  • 20
  • 31
19

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()
Thingamabobs
  • 7,274
  • 5
  • 21
  • 54
7

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
Sagitario
  • 102
  • 8
Shaun
  • 71
  • 1
  • 1
3

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()

Simao
  • 371
  • 2
  • 13
1

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

Jawad Saqib
  • 95
  • 1
  • 3
  • 10
  • 1
    There is no need to do so, you can just upload it from the current directory and it works perfectly fine. – Pycoder Oct 23 '22 at 05:43
1

If you are using CustomTkinter:

app.wm_iconbitmap('yt.ico')

CustomTkinter documentation

Shub
  • 168
  • 1
  • 4
0
from tkinter import *

root = Tk()

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

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

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()
Slava S.
  • 11
  • 3