Skip to content

Commit

Permalink
Make package runnable by directory
Browse files Browse the repository at this point in the history
  • Loading branch information
johannesbuchholz committed Jan 22, 2023
1 parent 40fe6f0 commit ab2dba3
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 18 deletions.
18 changes: 14 additions & 4 deletions Screens/GUI.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@

import os
import pathlib
import sys
from tkinter import (Tk, Frame)

from Screens import (RuleScreenClass, OptionScreenClass, GameScreenClass, TitleScreenClass)

from FieldObjects.PlayerClassBot import PlayerBotContLossStrategy, PlayerBotConeStrategy


class GUI(Tk):
"""
Expand Down Expand Up @@ -66,7 +66,8 @@ def __init__(self):
# GUI objects
self.frames = {"Title": TitleScreenClass.TitleScreen(parent=container, controller=self),
"Rules": RuleScreenClass.RuleScreen(parent=container, controller=self),
"Options": OptionScreenClass.OptionScreen(parent=container, controller=self),
"Options": OptionScreenClass.
OptionScreen(parent=container, controller=self),
"Game": GameScreenClass.GameScreen(parent=container, controller=self)}

self.frames["Title"].grid(row=0, column=0, sticky="nsew")
Expand All @@ -91,3 +92,12 @@ def exit_session(self):
"""
self.destroy()
exit()


def get_execution_path() -> str:
execution_path = pathlib.Path(sys.argv[0])
if execution_path.is_file():
return os.getcwd() + "/" + str(execution_path.parent)
else:
return os.getcwd() + "/" + str(execution_path)

7 changes: 2 additions & 5 deletions Screens/GameScreenClass.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from timeit import default_timer
from pynput import keyboard

from Screens import GUI
from Utils.Const import *
from Utils.HelperFunctions import get_rectangle_corners

Expand Down Expand Up @@ -234,7 +235,7 @@ def place_item(self, name, pos):
"""
# -- Create and store item
item_to_place = self.create_item_by_name(name)
icon = PhotoImage(file=item_to_place.image_path)
icon = PhotoImage(file=GUI.get_execution_path() + "/" + item_to_place.image_path)
self.items_spawned[self.item_id] = (item_to_place, icon, pos)
# -- Place item on the field
# visually
Expand Down Expand Up @@ -677,11 +678,7 @@ def ticker(self):
"\nTick: {}, wanted: {}, actual: {}".format(self.tick_count,
round(self._interval, 4),
round(update - start, 4)))
# print("Sleep length:", self._interval - (update - start))
# self.notify_players()
sleep(max([0, self._interval - (update - start)]))
# end = default_timer()
# print("Tick length:", end - start, "\n==============")

def move(self):
"""
Expand Down
12 changes: 6 additions & 6 deletions Screens/OptionScreenClass.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

from tkinter import (Frame, Label, Button, Entry, IntVar, Radiobutton, PhotoImage,
TOP,
)
from pynput import keyboard
from FieldObjects import PlayerClass
from Screens import GUI


class OptionScreen(Frame):
Expand Down Expand Up @@ -60,13 +60,13 @@ def __init__(self, controller, parent):
cnf_button_thin1 = {"width": 10, "height": 1, "borderwidth": 1}

# -- Labels
Label(master=self, text="Options", font=self.controller.font_bigger)\
Label(master=self, text="Options", font=self.controller.font_bigger) \
.grid(row=1, column=1, pady=10)
Label(master=self, text="Name", font=self.controller.font_medium)\
Label(master=self, text="Name", font=self.controller.font_medium) \
.grid(row=3, column=1, columnspan=1, pady=10)
Label(master=self, text="Keys", font=self.controller.font_medium)\
Label(master=self, text="Keys", font=self.controller.font_medium) \
.grid(row=4, column=1, columnspan=1, pady=10)
Label(master=self, text="Max rounds", font=self.controller.font_medium)\
Label(master=self, text="Max rounds", font=self.controller.font_medium) \
.grid(row=6, column=1, columnspan=1, pady=10)
Label(master=self, text="Items", font=self.controller.font_medium) \
.grid(row=7, column=1, columnspan=1, pady=10)
Expand Down Expand Up @@ -128,7 +128,7 @@ def __init__(self, controller, parent):
for item_name, i in zip(self.controller.all_items, range(len(self.controller.all_items))):
name_short = item_name.replace("Item", "")
# Icon
icon = PhotoImage(file="./Data/Icon{}.png".format(name_short))
icon = PhotoImage(file=GUI.get_execution_path() + "/Data/Icon{}.png".format(name_short))
self.item_icons.append(icon)
# Button
button_item = Button(master=self, text=name_short, relief="sunken",
Expand Down
11 changes: 8 additions & 3 deletions __main__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@

from Screens import GUI

gui = GUI.GUI()
gui.mainloop()

def run():
gui = GUI.GUI()
gui.mainloop()


if __name__ == "__main__":
run()

0 comments on commit ab2dba3

Please sign in to comment.