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:

  1. Standard Tkinter approach - Using root.iconbitmap():
root = tk.Tk()
root.iconbitmap('flarepie_logo.ico')
  1. Setting AppUserModelID:
import ctypes
app_id = 'com.mycompany.myapp.5.5'    ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(app_id)
  1. PyInstaller packaging

pyinstaller --onefile --windowed --icon=logo.ico myapp.py

  1. Both iconbitmap and iconphoto
root.iconbitmap('logo.ico')
logo_img = tk.PhotoImage(file='logo.png')
root.iconphoto(True, logo_img)
  1. Absoulute Path
ico_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'logo.ico')
root.iconbitmap(ico_path)
  1. 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:

  1. Standard Tkinter approach - Using root.iconbitmap():
root = tk.Tk()
root.iconbitmap('flarepie_logo.ico')
  1. Setting AppUserModelID:
import ctypes
app_id = 'com.mycompany.myapp.5.5'    ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(app_id)
  1. PyInstaller packaging

pyinstaller --onefile --windowed --icon=logo.ico myapp.py

  1. Both iconbitmap and iconphoto
root.iconbitmap('logo.ico')
logo_img = tk.PhotoImage(file='logo.png')
root.iconphoto(True, logo_img)
  1. Absoulute Path
ico_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'logo.ico')
root.iconbitmap(ico_path)
  1. 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 badge
Add a comment  | 

2 Answers 2

Reset to default 0

It 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