probably too many files by now, but I'm slowly learning, will consolidate as soon as I have this controller setup down

This commit is contained in:
Joost Agterhoek 2024-08-22 22:37:08 +02:00
parent 74acacd80c
commit 2dcb189902
4 changed files with 128 additions and 98 deletions

View File

@ -0,0 +1,54 @@
import tkinter as tk
from tkinter import ttk
from generic import sanitize, type_hosts_listed, type_hosts_organized
class App(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.wm_title("Testing out classing my application")
container = tk.Frame(self, height=400, width=600)
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.var = tk.Variable()
# I think this goes wrong for my purposes: I'm not switching frames in place, but keeping them besides each other. Find a simpler setup maybe?
self.frames = {}
for F in (InputFrame, LookupFrame):
frame = F(container, self)
frame.grid(row=0, column=0, sticky='nsew')
class InputFrame(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="Input URL, IP, email address")
label.pack(padx=10, pady=10)
self.var = tk.Variable()
self.entry = ttk.Entry(self)
self.entry.focus()
self.entry.pack(padx=10, pady=10)
self.entry.bind("<Return>", self.key_handler_function)
def key_handler_function(self, event) -> None:
inputs = self.entry.get()
sanitized = sanitize.strip_and_list(inputs)
typed = type_hosts_organized.type_hosts(sanitized)
self.var.set(typed)
class LookupFrame(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="Lookup information")
label.pack(padx=10, pady=10)
var = controller.var.get()
print(var)
if __name__ == "__main__":
testObj = App()
testObj.mainloop()

View File

@ -56,8 +56,8 @@ class LookupFrame(ttk.Frame):
class App(tk.Tk):
def __init__(self):
super().__init__()
def __init__(self, *args, **kwargs):
super().__init__(self, *args, **kwargs)
self.title(APP_TITLE)
self.geometry("400x150")
self.resizable(None, None)

72
tkinter-example-oop-3.py Normal file
View File

@ -0,0 +1,72 @@
import tkinter as tk
from tkinter import ttk
class windows(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.wm_title("Test Application")
container = tk.Frame(self, height=400, width=600)
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (MainPage, SidePage, CompletionScreen):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(MainPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class MainPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="Main Page")
label.pack(padx=10, pady=10)
# We use the switch_window_button in order to call the show_frame() method as a lambda function
switch_window_button = tk.Button(
self,
text="Go to the Side Page",
command=lambda: controller.show_frame(SidePage),
)
switch_window_button.pack(side="bottom", fill=tk.X)
class SidePage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="This is the Side Page")
label.pack(padx=10, pady=10)
switch_window_button = tk.Button(
self,
text="Go to the Completion Screen",
command=lambda: controller.show_frame(CompletionScreen),
)
switch_window_button.pack(side="bottom", fill=tk.X)
class CompletionScreen(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="Completion Screen, we did it!")
label.pack(padx=10, pady=10)
switch_window_button = ttk.Button(
self, text="Return to menu", command=lambda: controller.show_frame(MainPage)
)
switch_window_button.pack(side="bottom", fill=tk.X)
if __name__ == "__main__":
testObj = windows()
testObj.mainloop()

View File

@ -1,96 +0,0 @@
# 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()