Skip to content

Instantly share code, notes, and snippets.

@JakuWorks
Last active February 7, 2024 15:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JakuWorks/fb905082c27ab74961cc12a3bcb46931 to your computer and use it in GitHub Desktop.
Save JakuWorks/fb905082c27ab74961cc12a3bcb46931 to your computer and use it in GitHub Desktop.
Understanding the original code is not necessary at all to answering the StackOverflow question
# Understanding the original code is not necessary at all to answering the StackOverflow question
# IGNORE THE WRONG TYPE HINTS in a few places
import tkinter as tk
from tkinter import ttk
from custom_tkinter_styling import CustomTkinterStyles
from typing import Union
from typing import Type
from dataclasses import dataclass
from debug_messenger import DEBUG_MESSENGER
@dataclass
class InformationGuiSingletonClassBuilder:
window_title: str
text_heading: str
text_main: str
text_close_button: str = "Ok"
window_size: str = '550x350'
debug_title: Union[None, str] = None
def build(self) -> Type['InformationGuiSingletonBase']: # probably wrong typehint
our_builder: InformationGuiSingletonClassBuilder = self
class InformationGuiSingleton(InformationGuiSingletonBase):
def __init__(self, master: tk.Tk) -> None:
super().__init__(master=master, builder=our_builder)
return InformationGuiSingleton
class InformationGuiSingletonBase:
_current_instance: Union['InformationGuiSingletonBase', None] = None
_current_highest_id: int = 0
def __init__(self, master: tk.Tk, builder: InformationGuiSingletonClassBuilder) -> None:
self.id: int = self.generate_unique_id()
self.debug_title: str = builder.debug_title or builder.window_title
self.debug_name: str = f"{builder.debug_title} {self.id}"
DEBUG_MESSENGER.log(f"Initializing Help Gui - '{self.debug_name}'", 2)
if self.__class__._current_instance is not None:
DEBUG_MESSENGER.log(f"Detected Already Existing Window - '{self.__class__._current_instance.debug_name}'", 3)
self.__class__._current_instance.close_window()
self.__class__._current_instance = self
DEBUG_MESSENGER.log(f"Creating GUI Root - '{self.debug_name}'", 3)
self.root: tk.Toplevel = tk.Toplevel(master=master)
self.CSF: CustomTkinterStyles = CustomTkinterStyles.get_instance()
DEBUG_MESSENGER.log(f"Setting Window Title - '{self.debug_name}'", 3)
self.root.title(string=builder.window_title)
DEBUG_MESSENGER.log(f"Setting Window Geometry - '{self.debug_name}'", 3)
self.root.geometry(newGeometry=builder.window_size)
DEBUG_MESSENGER.log(f"Creating MainFrame - '{self.debug_name}'", 3)
self.mainframe: ttk.Frame = ttk.Frame(master=self.root)
self.mainframe.pack(expand=True, fill=tk.BOTH, side=tk.TOP)
DEBUG_MESSENGER.log(f"Creating Heading Text Label - '{self.debug_name}'", 3)
heading_label_text: str = builder.text_heading
self.heading_label: ttk.Label = ttk.Label(master=self.mainframe, anchor=tk.CENTER,
text=heading_label_text, font=self.CSF.FONT_LABEL_HEADING_MEDIUM)
self.heading_label.pack(expand=False, fill=tk.BOTH, side=tk.TOP,
padx=15, pady=15)
DEBUG_MESSENGER.log(f"Creating Main Text Label - '{self.debug_name}'", 3)
main_label_text: str = builder.text_main
self.main_label: ttk.Label = ttk.Label(master=self.mainframe, anchor=tk.NW,
text=main_label_text, font=self.CSF.FONT_LABEL_NORMAL_SMALL)
self.main_label.pack(expand=True, fill=tk.BOTH, side=tk.TOP,
padx=50, pady=10)
DEBUG_MESSENGER.log(f"Creating '{builder.text_close_button}' Button - '{self.debug_name}'", 3)
self.close_button: ttk.Button = ttk.Button(master=self.mainframe,
text=builder.text_close_button,
style=self.CSF.STYLE_BUTTON_MEDIUM_NAME,
command=self.close_window)
self.close_button.pack(expand=False, fill=tk.NONE, side=tk.TOP,
padx=15, pady=15, ipadx=3, ipady=3)
def close_window(self) -> None:
DEBUG_MESSENGER.log(f"Closing Window '{self.debug_name}'", 4)
self.__class__._current_instance = None
self.root.destroy()
@classmethod
def generate_unique_id(cls) -> int:
cls._current_highest_id = cls._current_highest_id + 1
return cls._current_highest_id
# example usage
# HOW IT LOOKS: https://i.imgur.com/sLh9NvU.png
# creating a master
root: tk.Tk = tk.Tk()
tab_raw_information_gui_builder: InformationGuiSingletonClassBuilder = InformationGuiSingletonClassBuilder(
window_title = "Raw Input Guide",
text_heading = "Raw Input Guide",
text_main = """Input Group IDs/Links. Each entry on a new line and separated TODO UNFINISHED HELP MESSAGE"""
)
# probably wrong typehint below
tab_raw_information_gui: Type[InformationGuiSingletonBase] = tab_raw_information_gui_builder.build()
tab_fancy_information_gui_builder: InformationGuiSingletonClassBuilder = InformationGuiSingletonClassBuilder(
window_title = "Fancy Input Guide",
text_heading = "Fancy Input Guide",
text_main = """Press the + button to create TODO UNFINISHED HELP MESSAGE"""
)
# probably wrong typehint below
tab_fancy_information_gui: Type[InformationGuiSingletonBase] = tab_fancy_information_gui_builder.build()
# SPAWNING A CREATED INFORMATION GUI SINGLETON examples
tab_raw_information_gui(master=root) # you can bind spawning an information gui to a tkinter button
tab_fancy_information_gui(master=root)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment