Skip to content

Commit 9123684

Browse files
committed
path resolver for pyinstaller compiling
1 parent e3ce2bf commit 9123684

3 files changed

Lines changed: 18 additions & 5 deletions

File tree

game/game.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from colorama import Fore
55
from typing import Any
66
from dataclasses import dataclass
7+
from path_resolver import resource_path
78

89

910
class Key:
@@ -17,10 +18,14 @@ class Key:
1718
white_key_pressed_path: Path = Path("game", "assets", "white_pressed.png")
1819
black_key_pressed_path: Path = Path("game", "assets", "black_pressed.png")
1920

20-
white_key: pg.Surface = pg.image.load(white_key_path)
21-
black_key: pg.Surface = pg.image.load(black_key_path)
22-
white_key_pressed: pg.Surface = pg.image.load(white_key_pressed_path)
23-
black_key_pressed: pg.Surface = pg.image.load(black_key_pressed_path)
21+
white_key: pg.Surface = pg.image.load(resource_path(str(white_key_path)))
22+
black_key: pg.Surface = pg.image.load(resource_path(str(black_key_path)))
23+
white_key_pressed: pg.Surface = pg.image.load(
24+
resource_path(str(white_key_pressed_path))
25+
)
26+
black_key_pressed: pg.Surface = pg.image.load(
27+
resource_path(str(black_key_pressed_path))
28+
)
2429

2530
white_keys_padding_pixels: int = 4
2631
white_key_full_width: int = white_key.get_width() + white_keys_padding_pixels

path_resolver.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import sys
2+
from pathlib import Path
3+
4+
5+
def resource_path(relative_path: str) -> Path:
6+
base_path = Path(getattr(sys, "_MEIPASS", Path(__file__).parent))
7+
return base_path / relative_path

pyqt/qt.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from PyQt6 import QtGui, QtWidgets, QtCore
2+
from path_resolver import resource_path
23
from dataclasses import dataclass
34
from pathlib import Path
45
import pyqt.main_ui as ui
@@ -21,7 +22,7 @@ def __init__(self) -> None:
2122
self.ui.setupUi(self.window)
2223

2324
self.icon_path: Path = Path("misc", "buzzer.icon")
24-
self.window.setWindowIcon(QtGui.QIcon(str(self.icon_path)))
25+
self.window.setWindowIcon(QtGui.QIcon(str(resource_path(str(self.icon_path)))))
2526

2627
self.ui.done_button.pressed.connect(self._on_connect_button_pressed)
2728

0 commit comments

Comments
 (0)