admin管理员组文章数量:1401444
I've been struggling to get my application icon to appear correctly in both windows' titlebar and Windows taskbar for my PyInstaller-packaged Python Tkinter application. Here's what I've tried thus far:
- Standard Tkinter approach - Using
root.iconbitmap()
:
root = tk.Tk()
root.iconbitmap('flarepie_logo.ico')
- Setting
AppUserModelID
:
import ctypes
app_id = 'com.mycompany.myapp.5.5' ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(app_id)
- PyInstaller packaging
pyinstaller --onefile --windowed --icon=logo.ico myapp.py
- Both
iconbitmap
andiconphoto
root.iconbitmap('logo.ico')
logo_img = tk.PhotoImage(file='logo.png')
root.iconphoto(True, logo_img)
- Absoulute Path
ico_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'logo.ico')
root.iconbitmap(ico_path)
- PyWin32
import win32gui
import win32con
import win32api
def set_taskbar_icon(self):
hwnd = win32gui.GetParent(self.root.winfo_id())
icon_path = os.path.abspath("logo.ico")
big_icon = win32gui.LoadImage(
0, icon_path, win32con.IMAGE_ICON,
32, 32, win32con.LR_LOADFROMFILE
)
small_icon = win32gui.LoadImage(
0, icon_path, win32con.IMAGE_ICON,
16, 16, win32con.LR_LOADFROMFILE
)
win32api.SendMessage(hwnd, win32con.WM_SETICON, win32con.ICON_BIG, big_icon)
win32api.SendMessage(hwnd, win32con.WM_SETICON, win32con.ICON_SMALL, small_icon)
Sometimes logo appears on left upper side of the UI but not on the taskbar after I convert it to .exe , I use Windows 11 and Python 3.12 and I'm tired of this. Has anyone solved this issue with icons in PyInstaller packaged Tkinter applications on Windows? Any other approaches I should try?
I've been struggling to get my application icon to appear correctly in both windows' titlebar and Windows taskbar for my PyInstaller-packaged Python Tkinter application. Here's what I've tried thus far:
- Standard Tkinter approach - Using
root.iconbitmap()
:
root = tk.Tk()
root.iconbitmap('flarepie_logo.ico')
- Setting
AppUserModelID
:
import ctypes
app_id = 'com.mycompany.myapp.5.5' ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(app_id)
- PyInstaller packaging
pyinstaller --onefile --windowed --icon=logo.ico myapp.py
- Both
iconbitmap
andiconphoto
root.iconbitmap('logo.ico')
logo_img = tk.PhotoImage(file='logo.png')
root.iconphoto(True, logo_img)
- Absoulute Path
ico_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'logo.ico')
root.iconbitmap(ico_path)
- PyWin32
import win32gui
import win32con
import win32api
def set_taskbar_icon(self):
hwnd = win32gui.GetParent(self.root.winfo_id())
icon_path = os.path.abspath("logo.ico")
big_icon = win32gui.LoadImage(
0, icon_path, win32con.IMAGE_ICON,
32, 32, win32con.LR_LOADFROMFILE
)
small_icon = win32gui.LoadImage(
0, icon_path, win32con.IMAGE_ICON,
16, 16, win32con.LR_LOADFROMFILE
)
win32api.SendMessage(hwnd, win32con.WM_SETICON, win32con.ICON_BIG, big_icon)
win32api.SendMessage(hwnd, win32con.WM_SETICON, win32con.ICON_SMALL, small_icon)
Sometimes logo appears on left upper side of the UI but not on the taskbar after I convert it to .exe , I use Windows 11 and Python 3.12 and I'm tired of this. Has anyone solved this issue with icons in PyInstaller packaged Tkinter applications on Windows? Any other approaches I should try?
Share Improve this question edited Mar 25 at 7:26 Basheer Jarrah 5883 silver badges16 bronze badges asked Mar 22 at 21:41 ElexsizzElexsizz 11 bronze badge2 Answers
Reset to default 0It is suggested to go through the official document on PyInstaller Runtime Information.
To get what you want, you need to:
- use absolute path instead of relative path on resource files, like images.
- need to include those resource files into the executable generated by
PyInstaller
using--add-data
option
Below is a simplified example:
from pathlib import Path
from ctypes import windll
import tkinter as tk
# fix taskbar icon (on Windows platform only)
windll.shell32.SetCurrentProcessExplicitAppUserModelID('dummy.id')
# get the directory where this script is
appdir = Path(__file__).parent
root = tk.Tk()
root.iconbitmap(appdir/'icon.ico') # use absolute path instead
root.mainloop()
Then include the icon file into the executable when running PyInstaller
:
pyinstaller --onefile --windowed --icon=icon.ico --add-data=icon.ico:. myapp.py
Result when executing the generated executable:
I am not sure it will show in file explorer, but I had the same problem and I fixed it by:
Python File
from tkinter import *
window = Tk()
window.title(YOUR_TILE_HERE)
window.geometry(YOUR_WINDOW_SIZE_HERE)
window.iconbitmap(YOUR_ICON_PATH_HERE)
Pyinstaller
> pyinstaller MAIN_FILE_NAME_HERE ANY_OTHER_ARGS_NOT_RELEVENT
If you try now, it will give you a path not found error but if you move your icon to the dist
in the main
folder it should pick it up and run normally. I understand that you have to move every time but it is the quickest, simplest and easiest solution I came across. I hope you get it working.
本文标签: pythonLogo wont show on the TaskbarStack Overflow
版权声明:本文标题:python - Logo wont show on the Taskbar - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744302895a2599667.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论