Skip to content
This repository has been archived by the owner on Jul 11, 2024. It is now read-only.

Commit

Permalink
refactor: rename symbols to make more sense
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas-Hopkins committed Apr 21, 2022
1 parent bdfd30c commit 234e96b
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 69 deletions.
93 changes: 49 additions & 44 deletions source/gui/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
import tkinter as tk
import os
from functools import partial
from gui.base import AppGuiBase
from gui.welcome import Welcome
from gui.gameboard import Gameboard
from gui.leaderboard import Leaderboard
from gui.help import Help
from gui.base import ContextBase
from gui.welcome import WelcomeContext
from gui.gameboard import GameboardContext
from gui.leaderboard import LeaderboardContext
from gui.help import HelpContext
from localization import localizer

THEME_FILE = f"{os.getcwd()}\gui\Sun-Valley-ttk-theme\sun-valley.tcl"
Expand All @@ -15,40 +15,45 @@

class Application(tk.Tk):
"""
Wrapper around the main Tk instance. Handles context switching, window setup, etc.
Wrapper around the main Tk instance. Handles context switching, window setup,
theme management, etc.
Holds an instance of all contexts. Contexts do not know about other contexts,
and so data should flow from a context <-> Application <-> other context if
required.
"""

def __init__(self) -> None:
tk.Tk.__init__(self)

# Set instance variables
self.current_context: AppGuiBase = None
self.previous_context: AppGuiBase = None
self.current_context: ContextBase = None
self.previous_context: ContextBase = None
self.using_theme: str = DEFAULT_THEME if self.__has_themes_installed() else None

# Init all of the GUIs this app will use
self.welcome_screen = Welcome(
# Init all of the context GUIs this app will use
self.welcome_context = WelcomeContext(
master=self,
theme=self.using_theme,
rows=3,
columns=3,
name=localizer.get("WELCOME_SCREEN"),
)
self.gameboard_screen = Gameboard(
self.gameboard_context = GameboardContext(
master=self,
theme=self.using_theme,
rows=3,
columns=3,
name=localizer.get("GAMEBOARD_SCREEN"),
)
self.leaderboard_screen = Leaderboard(
self.leaderboard_context = LeaderboardContext(
master=self,
theme=self.using_theme,
rows=3,
columns=3,
name=localizer.get("LEADERBOARD_SCREEN"),
)
self.help_screen = Help(
self.help_context = HelpContext(
master=self,
theme=self.using_theme,
rows=3,
Expand All @@ -57,61 +62,61 @@ def __init__(self) -> None:
)

# Setup the GUIs commands
self.welcome_screen.set_theme_cmd(partial(self.__change_theme))
self.welcome_context.set_theme_cmd(partial(self.__change_theme))

self.welcome_screen.set_play_cmd(
partial(self.__switch_context, screen=self.gameboard_screen)
self.welcome_context.set_play_cmd(
partial(self.__switch_context, context=self.gameboard_context)
)
self.welcome_screen.set_highscores_cmd(
self.welcome_context.set_highscores_cmd(
partial(
self.__switch_context,
screen=self.leaderboard_screen,
# When switching to leaderboard from welcome screen: refresh scores, disable back btn, enable main menu btn
context=self.leaderboard_context,
# When switching to leaderboard from welcome: refresh scores, disable back btn, enable main menu btn
funcs=(
self.leaderboard_screen.refresh_scores,
partial(self.leaderboard_screen.set_back_btn_state, enabled=False),
self.leaderboard_context.refresh_scores,
partial(self.leaderboard_context.set_back_btn_state, enabled=False),
partial(
self.leaderboard_screen.set_mainmenu_btn_state, enabled=True
self.leaderboard_context.set_mainmenu_btn_state, enabled=True
),
),
)
)

self.leaderboard_screen.set_mainmenu_cmd(
partial(self.__switch_context, screen=self.welcome_screen)
self.leaderboard_context.set_mainmenu_cmd(
partial(self.__switch_context, context=self.welcome_context)
)

self.leaderboard_screen.set_back_cmd(
self.leaderboard_context.set_back_cmd(
partial(self.__switch_context, previous=True)
)

self.gameboard_screen.set_mainmenu_cmd(
partial(self.__switch_context, screen=self.welcome_screen)
self.gameboard_context.set_mainmenu_cmd(
partial(self.__switch_context, context=self.welcome_context)
)

self.gameboard_screen.set_highscores_cmd(
self.gameboard_context.set_highscores_cmd(
partial(
self.__switch_context,
screen=self.leaderboard_screen,
# When switching to leaderboard from welcome screen: refresh scores, enable back btn, disable main menu btn
context=self.leaderboard_context,
# When switching to leaderboard from gameboard: refresh scores, enable back btn, disable main menu btn
funcs=(
self.leaderboard_screen.refresh_scores,
partial(self.leaderboard_screen.set_back_btn_state, enabled=True),
self.leaderboard_context.refresh_scores,
partial(self.leaderboard_context.set_back_btn_state, enabled=True),
partial(
self.leaderboard_screen.set_mainmenu_btn_state, enabled=False
self.leaderboard_context.set_mainmenu_btn_state, enabled=False
),
),
)
)

self.gameboard_screen.set_help_cmd(
partial(self.__switch_context, screen=self.help_screen)
self.gameboard_context.set_help_cmd(
partial(self.__switch_context, context=self.help_context)
)

self.help_screen.set_back_cmd(partial(self.__switch_context, previous=True))
self.help_context.set_back_cmd(partial(self.__switch_context, previous=True))

# Set welcome screen as current context and try to import/use theme
self.__switch_context(self.welcome_screen)
self.__switch_context(self.welcome_context)
self.__import_theme()

self.update()
Expand Down Expand Up @@ -139,26 +144,26 @@ def __change_theme(self):
self.tk.call("set_theme", "dark")

def __switch_context(
self, screen: AppGuiBase = None, previous: bool = False, funcs: tuple = None
self, context: ContextBase = None, previous: bool = False, funcs: tuple = None
):
"""
Handles switching the context from one screen to another.
Handles switching the context from one context to another.
screen: the screen you wish to switch to
previous: pass true if you wish to return to previous screen. This will ignore screen param.
context: the context you wish to switch to
previous: pass true if you wish to return to previous context. This will ignore context param.
funcs: optionally pass a tuple of functions to execute immediately after switching
"""
# Remove current context from the screen
if self.current_context:
self.current_context.pack_forget()

# use previous context
if previous or not screen:
screen = self.previous_context
if previous or not context:
context = self.previous_context

# Set previous context to current context and current context to requested screen
self.previous_context = self.current_context
self.current_context = screen
self.current_context = context
self.current_context.pack(fill="both", expand=True)

# Execute functions from passed funcs if exists
Expand Down
26 changes: 21 additions & 5 deletions source/gui/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,46 @@
import tkinter as tk


class AppGuiBase(ttk.Frame):
class ContextBase(ttk.Frame):
"""
The base layout of the game window.
The base layout of a context. Each context represents an entirely different screen of
the app. Contexts do not know about other contexts. If a context needs to know some data
in a different context, it is preferred to have the main application use a getter on the
context with the needed data, and then to use a setter on the context which needs the data.
This class should be inherited by all contexts.
"""

def __init__(
self, master, rows: int, columns: int, theme: str, name: str, *args, **kwargs
self,
master: tk.Tk,
rows: int,
columns: int,
theme: str,
name: str,
*args,
**kwargs
) -> None:
ttk.Frame.__init__(self, master, *args, **kwargs)

# Instance variables
self.name: str = name
self.using_theme: str = theme
self.num_rows: int = rows
self.num_columns: int = columns
self.app: tk.Tk = master # Reference back to application

# Setup rows/columns
for row in range(rows):
self.rowconfigure(index=row, weight=1, minsize=100)

self.num_columns: int = columns
for column in range(columns):
self.columnconfigure(index=column, weight=1, minsize=150)


if __name__ == "__main__":
root = tk.Tk()
app = AppGuiBase(master=root, rows=3, columns=3)
app = ContextBase(master=root, rows=3, columns=3)
app.pack(fill="both", expand=True)

root.update()
Expand Down
10 changes: 5 additions & 5 deletions source/gui/gameboard.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
from tkinter import ttk, Tk, messagebox
from functools import partial
from gui.base import AppGuiBase
from gui.base import ContextBase
from localization import localizer


class Gameboard(AppGuiBase):
class GameboardContext(ContextBase):
"""
The main game window. Has the gameboard, all game buttons, etc.
"""

def __init__(self, master, *args, **kwargs) -> None:
AppGuiBase.__init__(self, master, *args, **kwargs)
def __init__(self, *args, **kwargs) -> None:
ContextBase.__init__(self, *args, **kwargs)

self.rowconfigure(index=0, weight=5, minsize=100)
self.rowconfigure(index=1, weight=90, minsize=100)
Expand Down Expand Up @@ -82,7 +82,7 @@ def set_help_cmd(self, command) -> None:

if __name__ == "__main__":
root = Tk()
app = Gameboard(master=root, rows=3, columns=3, theme=None, name="")
app = GameboardContext(master=root, rows=3, columns=3, theme=None, name="")
app.pack(fill="both", expand=True)

root.update()
Expand Down
10 changes: 5 additions & 5 deletions source/gui/help.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
from tkinter import ttk
from gui.base import AppGuiBase
from gui.base import ContextBase
from functools import partial


class Help(AppGuiBase):
class HelpContext(ContextBase):
"""
Shows information about how this game works.
"""

def __init__(self, master, *args, **kwargs) -> None:
AppGuiBase.__init__(self, master, *args, **kwargs)
def __init__(self, *args, **kwargs) -> None:
ContextBase.__init__(self, *args, **kwargs)

self.label = ttk.Label(
self,
Expand All @@ -30,5 +30,5 @@ def set_back_cmd(self, command) -> None:


if __name__ == "__main__":
app = Help(master=None)
app = HelpContext(master=None)
app.mainloop()
10 changes: 5 additions & 5 deletions source/gui/leaderboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
import tkinter as tk
from functools import partial
from turtle import st
from gui.base import AppGuiBase
from gui.base import ContextBase
from localization import localizer


class Leaderboard(AppGuiBase):
class LeaderboardContext(ContextBase):
"""
Shows the top 10 scores.
"""

def __init__(self, master, *args, **kwargs) -> None:
AppGuiBase.__init__(self, master, *args, **kwargs)
def __init__(self, *args, **kwargs) -> None:
ContextBase.__init__(self, *args, **kwargs)

# Title
self.title = ttk.Label(
Expand Down Expand Up @@ -114,7 +114,7 @@ def refresh_scores(self) -> None:

if __name__ == "__main__":
root = tk.Tk()
app = Leaderboard(master=root, theme=None, rows=3, columns=3)
app = LeaderboardContext(master=root, theme=None, rows=3, columns=3)
app.pack(fill="both", expand=True)

root.update()
Expand Down
10 changes: 5 additions & 5 deletions source/gui/welcome.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
from tkinter import ttk
import tkinter as tk
from functools import partial
from gui.base import AppGuiBase
from gui.base import ContextBase
from localization import localizer


class Welcome(AppGuiBase):
class WelcomeContext(ContextBase):
"""
Initial screen upon starting the game.
"""

def __init__(self, master, *args, **kwargs) -> None:
AppGuiBase.__init__(self, master, *args, **kwargs)
def __init__(self, *args, **kwargs) -> None:
ContextBase.__init__(self, *args, **kwargs)

self.use_dark_theme = tk.BooleanVar(
value=True if self.using_theme == "dark" else False
Expand Down Expand Up @@ -116,7 +116,7 @@ def set_highscores_cmd(self, command) -> None:

if __name__ == "__main__":
root = tk.Tk()
app = Welcome(master=root, rows=3, columns=3)
app = WelcomeContext(master=root, rows=3, columns=3)
app.pack(fill="both", expand=True)

root.update()
Expand Down

0 comments on commit 234e96b

Please sign in to comment.