Skip to content

Commit bbd80e8

Browse files
committed
Add icon to PyInstaller exe
1 parent e3fe6fd commit bbd80e8

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

kasatk.spec

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# -*- mode: python ; coding: utf-8 -*-
22

3+
RELATIVE_ICON_PATH = "./extra/icon.ico"
34
block_cipher = None
45

56
def kasa_dist_info_datas():
@@ -20,7 +21,8 @@ def kasa_dist_info_datas():
2021
a = Analysis(['kasatk/__main__.py'],
2122
pathex=['.\\env\\Lib\\site-packages\\', '.'],
2223
binaries=[],
23-
datas=[kasa_dist_info_datas()],
24+
# Note: the second item in each tuple MUST be a directory.
25+
datas=[kasa_dist_info_datas(), (RELATIVE_ICON_PATH, "./extra")],
2426
hiddenimports=[],
2527
hookspath=[],
2628
runtime_hooks=[],
@@ -44,5 +46,7 @@ exe = EXE(pyz,
4446
upx=True,
4547
upx_exclude=[],
4648
runtime_tmpdir=None,
47-
icon='extra/icon.ico',
49+
icon=RELATIVE_ICON_PATH,
4850
console=False )
51+
52+
# vi:syntax=python

kasatk/__main__.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@
1818
logger = logging.getLogger(__name__)
1919

2020

21+
def resource_path(relative_path):
22+
"""Find a resource in a PyInstaller executable, or in the local directory"""
23+
if hasattr(sys, "_MEIPASS"):
24+
logger.info("Finding resource inside PyInstaller executable")
25+
p = os.path.join(sys._MEIPASS, relative_path)
26+
assert os.path.exists(p)
27+
return p
28+
return os.path.join(os.path.abspath("."), relative_path)
29+
30+
2131
class LightState(TypedDict):
2232
on_off: int
2333
mode: str
@@ -370,7 +380,11 @@ def main():
370380
root = tkinter.Tk()
371381
root.title("Kasa Devices")
372382
root.geometry("500x400")
373-
root.iconbitmap("extra/icon.ico")
383+
try:
384+
root.iconbitmap(resource_path("extra/icon.ico"))
385+
except Exception as e:
386+
logger.warning("Failed to load application icon.")
387+
logger.error(e)
374388
scroll_frame = ScrollableFrame(root)
375389
device_frame = KasaDevices(scroll_frame.scrollable_frame)
376390
device_frame.pack()

0 commit comments

Comments
 (0)