Can't set the icon on the Windows build
-
Hey there,
I've been fighting over something so seemingly trivial for over several months (had to move on from this, but come it from time to time and it's time to fix it for good.
I've tried all kinds of fixes to actually set the Windows icon (I works on MacOS), we've tested on several Windows machines.
I'm using PyQt6 and building using pyinstaller.
I've tried:
On the pyinstaller build:
- setting the icon on the pyinstaller command --icon="./resources/images/icon.ico"
- adding the icon using --add-data "./resources/images/;./resources/images"
- using every combination of --noconsole and --windowed
(I'm building using --onefile, and storing the .exe using a GitHub Action, I have no idea how would I ship it using --onedir)
On main.py
- setting the appid:
if sys.platform == "win32": # Check if the OS is Windows myappid = "company.product.subproduct.version" ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(myappid)
- setting the icon on the app
- setting the icon on the window
- I'm using this to get the icon_path:
if getattr(sys, "frozen", False) and hasattr(sys, "_MEIPASS"): applicationPath = sys._MEIPASS elif __file__: applicationPath = os.path.dirname(__file__) icon_path = os.path.join(applicationPath, "resources", "images", "icon.ico") if __name__ == "__main__": app = QApplication([]) app.setWindowIcon(QIcon(icon_path)) window = MainWindow() window.setWindowIcon(QIcon(icon_path)) window.show() sys.exit(app.exec())
I'm using a 64x64 ico for the icon. I managed to set it on the .exe, but not on the running app (in the taskbar and on the window). It only appears, when I pin the app on the taskbar.
I even tried to reload the caches using the command below and restarting the taskbar, nothing...
ie4uinit.exe -ClearIconCache
Adding the GitHub Action for reference:
name: Release on: release: types: [published] permissions: contents: write jobs: build-and-release: runs-on: windows-latest steps: - name: Check out code uses: actions/checkout@v3 - name: Set up Python uses: actions/setup-python@v4 with: python-version: "3.12" - name: Install dependencies run: | python -m pip install --upgrade pip pip install -r requirements.txt - name: Build with PyInstaller run: | pyinstaller --name=name-${{ github.ref_name }} --noconsole --icon="./resources/images/icon.ico" --add-data "./resources/images/;./resources/images" --onefile main.py - name: Upload Release Assets uses: actions/upload-release-asset@v1 env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} with: upload_url: ${{ github.event.release.upload_url }} asset_path: dist/name-${{ github.ref_name }}.exe asset_name: "name-${{ github.ref_name }}-windows-latest.exe" asset_content_type: application/octet-stream
Thank you VERY MUCH for any help/tips! :)
-
@Luuk00101 Did you verify that icon_path is a valid path to the icon file?
Usually you would put icons into a resource file and load it from there. -
@jsulm
Hey! Thanks for stopping by :)I tried to run it locally on my mac and print the icon_path. It points to the correct spot, but it also works fine on my Mac :D
Do you think the path resolves to an invalid one on when we run it on Windows? Gotta have to ask my coworker to check there.
Any tip on how to bundle the icons and load them from the resource file? I also load fonts from "./resources/fonts".
Thanks once again
-
@Luuk00101 said in Can't set the icon on the Windows build:
Do you think the path resolves to an invalid one on when we run it on Windows?
This is the first thing I would check.
Resource files: https://doc.qt.io/qtforpython-6/overviews/resources.html