From ca10d3df4feb40795b5676d895d794210d9eb66c Mon Sep 17 00:00:00 2001 From: Joost Agterhoek Date: Thu, 15 Aug 2024 21:23:04 +0200 Subject: [PATCH] trying to better organize and class with example --- tkinter-enter-classed.py | 61 +++++++++++++++++++++++++ tkinter-oop-2.py | 96 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 157 insertions(+) create mode 100644 tkinter-enter-classed.py create mode 100644 tkinter-oop-2.py diff --git a/tkinter-enter-classed.py b/tkinter-enter-classed.py new file mode 100644 index 0000000..84d2d67 --- /dev/null +++ b/tkinter-enter-classed.py @@ -0,0 +1,61 @@ +# based on tkinter-oop-2.py + +# Thinking in Frames: one Frame is the user input and its label, four more for the host type Listboxes, a last one for the host formatting? + +import tkinter as tk +from tkinter import ttk + +from generic import sanitize, type_hosts_listed + +ENTRY_LABEL = "Enter some IPs, domains, email addresses:" +APP_TITLE = "Extract and look up hosts" + + +class InputFrame(ttk.Frame): + def __init__(self, container): + super().__init__(container) + self.columnconfigure(0, weight=1) + self.columnconfigure(1, weight=1) + self.columnconfigure(2, weight=1) + self.columnconfigure(3, weight=1) + + self.__create_widgets() + + def __create_widgets(self) -> None: + ttk.Label(self, text=ENTRY_LABEL).grid(column=0, row=0) + self.entry = ttk.Entry(self) + self.entry.focus() + self.entry.grid(column=0, row=1, columnspan=4, sticky="nsew") + self.entry.bind("", self.key_handler_function) + + for widget in self.winfo_children(): + widget.grid(padx=0, pady=5) + + def key_handler_function(self, event) -> None: + inputs = self.entry.get() + sanitized = sanitize.strip_and_list(inputs) + typed = type_hosts_listed.type_hosts(sanitized) + + +class App(tk.Tk): + def __init__(self): + super().__init__() + self.title(APP_TITLE) + self.geometry("400x150") + self.resizable(None, None) + + # layout on the root window + self.columnconfigure(0, weight=4) + self.columnconfigure(1, weight=1) + + self.__create_widgets() + + def __create_widgets(self) -> None: + # create the input frame + input_frame = InputFrame(self) + input_frame.grid(column=0, row=0) + + +if __name__ == "__main__": + app = App() + app.mainloop() diff --git a/tkinter-oop-2.py b/tkinter-oop-2.py new file mode 100644 index 0000000..bb9bcfb --- /dev/null +++ b/tkinter-oop-2.py @@ -0,0 +1,96 @@ +# from: https://www.pythontutorial.net/tkinter/tkinter-object-oriented-frame/ + +import tkinter as tk +from tkinter import ttk + + +class InputFrame(ttk.Frame): + def __init__(self, container): + super().__init__(container) + # setup the grid layout manager + self.columnconfigure(0, weight=1) + self.columnconfigure(0, weight=3) + + self.__create_widgets() + + def __create_widgets(self): + # Find what + ttk.Label(self, text='Find what:').grid(column=0, row=0, sticky=tk.W) + keyword = ttk.Entry(self, width=30) + keyword.focus() + keyword.grid(column=1, row=0, sticky=tk.W) + + # Replace with: + ttk.Label(self, text='Replace with:').grid( + column=0, row=1, sticky=tk.W) + replacement = ttk.Entry(self, width=30) + replacement.grid(column=1, row=1, sticky=tk.W) + + # Match Case checkbox + match_case = tk.StringVar() + match_case_check = ttk.Checkbutton( + self, + text='Match case', + variable=match_case, + command=lambda: print(match_case.get())) + match_case_check.grid(column=0, row=2, sticky=tk.W) + + # Wrap Around checkbox + wrap_around = tk.StringVar() + wrap_around_check = ttk.Checkbutton( + self, + variable=wrap_around, + text='Wrap around', + command=lambda: print(wrap_around.get())) + wrap_around_check.grid(column=0, row=3, sticky=tk.W) + + for widget in self.winfo_children(): + widget.grid(padx=0, pady=5) + + +class ButtonFrame(ttk.Frame): + def __init__(self, container): + super().__init__(container) + # setup the grid layout manager + self.columnconfigure(0, weight=1) + + self.__create_widgets() + + def __create_widgets(self): + ttk.Button(self, text='Find Next').grid(column=0, row=0) + ttk.Button(self, text='Replace').grid(column=0, row=1) + ttk.Button(self, text='Replace All').grid(column=0, row=2) + ttk.Button(self, text='Cancel').grid(column=0, row=3) + + for widget in self.winfo_children(): + widget.grid(padx=0, pady=3) + + +class App(tk.Tk): + def __init__(self): + super().__init__() + self.title('Replace') + self.geometry('400x150') + self.resizable(0, 0) + # windows only (remove the minimize/maximize button) + # self.attributes('-toolwindow', True) + + # layout on the root window + self.columnconfigure(0, weight=4) + self.columnconfigure(1, weight=1) + + self.__create_widgets() + + def __create_widgets(self): + # create the input frame + input_frame = InputFrame(self) + input_frame.grid(column=0, row=0) + + # create the button frame + button_frame = ButtonFrame(self) + button_frame.grid(column=1, row=0) + + +if __name__ == "__main__": + app = App() + app.mainloop()