Reputation: 816
I am using Python 3.5.0 on Windows 10 and want to replace this:
Upvotes: 48
Views: 124098
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
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
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
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:
Supported formats since TkVersion 8.6 of tk.PhotoImage(filepath)
:
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
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
Reputation: 101
from tkinter import *
app = Tk()
app.title('Tk')
app.geometry('')
app.iconbitmap(r'C:\Users\User\PycharmProjects\HelloWorld\my.ico')
app.mainloop()
pyinstaller --onefile -w -F --add-binary "my.ico;." my.py
Upvotes: 10
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