22

I've been working on a very simple Python script using Tkinter. I am using Python 2.7.3.

How can I change its application icon (the 'file' icon shown at the explorer window and the start/all programs window, for example - not the 'file type' icon nor the main window of the app icon) and the taskbar icon (the icon shown at the taskbar when the application is minimized)? I only need to support Windows XP and Win7 machines.

8 Answers 8

32

Another option on Windows would be the following:

To your python code add the following:

import ctypes

myappid = 'mycompany.myproduct.subproduct.version' # arbitrary string
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(myappid)
Sign up to request clarification or add additional context in comments.

5 Comments

Link to the original answer of this question would be useful for implementation: stackoverflow.com/questions/1551605/…
Does this work in windows 10?
@LoganPrice it works in Windows 11
This has always been the best solution for me, works up to Win 11
How does this code select the icon file?
16

Use

root.iconbitmap(default='ardulan.ico')

But the probleme is that it only replace the icon on the windows not on the taskbar. It's because the py file is executed from python interpreter so windows use his icon and not the tkinter icon.

You have to 'compile' it i think with py2exe, cx_Freeze, py2app ...

http://www.py2exe.org/index.cgi/CustomIcons

1 Comment

You say: "...the py file is executed from python interpreter so windows use his icon..." This is, in fact, the problem
5

You can do that by creating a new shortcut to the python.exe or pythonw.exe file, (option available in Window's explorer right-click menu), then right click the shortcut, select properties, and change target to:

"C:\Path\to\Python.exe" "Absolute\or\relative\path\to\file.py"

Then select change icon (still in the properties window), and select your .ico file. Executing the program from this shortcut will automaticaly set the selected icon as taskbar and window icon.

Note though that by executing set shortcut, instead of clicking directly your .py file, the current working directory will be the one of python.exe, and not of your file, you can change that too in the shortcut's properties window, in the "start in" entry field (underneath target), or by using the python methods os.getcwd() to find the current working directory and os.chdir(path) to set it

Comments

5

I have a music_app.py file and melody.ico file

With Tkinter you can use:

from tkinter import *
root = Tk()
root.title("melody")
root.iconbitmap(r"melody.ico")
root.mainloop()

With PyQT

from PyQt4 import QtGui
import sys

app = QtGui.QApplication(sys.argv)
Form = QtGui.QWidget()
Form.setWindowIcon(QtGui.QIcon('melody.ico'))
Form.setWindowTitle('melody')
Form.show()
sys.exit(app.exec_())

After that you can convert music_app.py to .exe or not and the result remain the same:

enter image description here

Hope it help !!!

Comments

4

To set both the window and the taskbar icon, you need to first use the ctypes commands and Afterwards the root.iconbitmap command. The skript should look something like this:

import tkinter as tk
import ctypes

# sets a basic window
root = tk.Tk()
root.title("Window")
root.geometry("800x600")

# this is the part that sets the icon
myappid = 'tkinter.python.test'
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(myappid)
root.iconbitmap(r"icon_path") # used raw string to avoid backslash issues

root.mainloop()

Reference: https://www.youtube.com/watch?v=pHpI8g0COZw

Comments

0

Create an single file exe using PyInstaller and use Inno Setup to build the windows installer package. Inno Setup will do the icon stuff for you.

Comments

0

add --icon=iconname.ico
to the pyinstaller command in the prompt

eg>> pyinstaller --windowed --add-data "pics/myicon.ico;pics" --add-data "pics/*.png;pics" --icon=pics/myicon.ico -d bootloader myscript.py

this will show your icon on the windows taskbar instead of the default python pkg icon

Comments

-1

For those arriving at this post looking for a more accurate and complete answer:

import tkinter as tk
root = tk.Tk()

root.iconbitmap('/path/to/ico/icon.ico')
root.mainloop()

Source reference is delftstack.com

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.