Skip to content

Commit 988f09d

Browse files
authored
Enhance Image class with better init and config methods
Refactor Image class to improve initialization and configuration methods.
1 parent 3cc4d99 commit 988f09d

File tree

1 file changed

+37
-3
lines changed

1 file changed

+37
-3
lines changed

src/ingame/objects.py

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,46 @@ def destroy(
103103

104104
class Image(Object):
105105
"""
106-
Displays an image (from file or URL) on the given Screen, packs it with optional args,
107-
and provides a destroy() method for cleanup. Supports basic transformations (resize, scale, position).
106+
Displays an image on the given Screen, packs it with optional args,
107+
and provides a destroy() method for cleanup.
108108
"""
109109

110110
image_obj: tk.Label
111-
_photo_image: Any
111+
112+
def __init__(
113+
self,
114+
screen_obj: Screen,
115+
/,
116+
image_path: str,
117+
packargs: Optional[dict[Any, Optional[Any]]] = None,
118+
**kwargs
119+
) -> None:
120+
121+
if packargs is None:
122+
packargs = {}
123+
124+
if not isinstance(screen_obj, Screen):
125+
raise TypeError("screen_obj must be an instance of Screen")
126+
127+
img = tk.PhotoImage(file=image_path)
128+
self.image_obj = tk.Label(screen_obj.root, image=img, **kwargs)
129+
self.image_obj.image = img
130+
self.image_obj.pack(**{k: v for k, v in packargs.items() if v is not None})
131+
132+
def config(
133+
self,
134+
**kwargs
135+
) -> None:
136+
"""Configure object"""
137+
138+
self.image_obj.config(**kwargs)
139+
140+
def destroy(
141+
self
142+
) -> None:
143+
"""Destroy image"""
144+
145+
self.image_obj.destroy()
112146

113147
class Input(Object):
114148
"""

0 commit comments

Comments
 (0)