From 22831cfc194bbac075932bace36c5f5b34be07f0 Mon Sep 17 00:00:00 2001 From: Martin Putzlocher Date: Tue, 28 Dec 2021 14:31:58 +0100 Subject: [PATCH] added template for tk applications and first sketch of canvas test --- tk_canvas/my_canvas.py | 68 ++++++++++++++++++++++++++++++++++++++++++ tk_template.py | 32 ++++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 tk_canvas/my_canvas.py create mode 100644 tk_template.py diff --git a/tk_canvas/my_canvas.py b/tk_canvas/my_canvas.py new file mode 100644 index 0000000..beaf153 --- /dev/null +++ b/tk_canvas/my_canvas.py @@ -0,0 +1,68 @@ +import tkinter as tk +import tkinter.ttk as ttk + +class Controller(object): + def __init__(self, app): + self.app = app + + self.selected = None + self.selected_start = None + self.selected_end = None + + def run_process(self): + print("Run") + self.app.main_canvas.bind('<1>', self.make_circle) + self.app.main_canvas.bind('<3>', self.select_item) + self.app.main_canvas.bind('', self.select_for_line) + return 0 + + def make_circle(self, event): + x = event.x + y = event.y + r = 20 + self.app.main_canvas.create_oval(x-r, y-r, x+r, y+r, fill="white", outline="black", tag="circle") + + def select_item(self, event): + self.app.main_canvas.bind('', self.move_item) + self.app.main_canvas.bind('<1>', self.deselect_item) + self.app.main_canvas.addtag_withtag('selected', tk.CURRENT) + + def deselect_item(self, event): + self.app.main_canvas.dtag('selected') + self.app.main_canvas.unbind('') + self.app.main_canvas.bind('<1>', self.make_circle) + + def move_item(self, event): + x = event.x + y = event.y + r = 20 + self.app.main_canvas.coords("selected",x-r, y-r, x+r, y+r) + + def select_for_line(self, event): + pass + + +class Application(tk.Tk): + def __init__(self): + super().__init__() + self.con = Controller(self) + self.create_control_variables() + self.create_widgets() + + def create_control_variables(self): + pass + + def create_widgets(self): + self.main_canvas = tk.Canvas(master=self, width=500, height=400, background='gray75') + self.main_canvas.grid(row=0, column=0) + + self.b = ttk.Button(master=self, text="Start", command=self.con.run_process) + self.b.grid(row=1, column=0) + + +# -------------------------- +# Execution +# -------------------------- +if __name__ == "__main__": + app = Application() + app.mainloop() diff --git a/tk_template.py b/tk_template.py new file mode 100644 index 0000000..d1d95c0 --- /dev/null +++ b/tk_template.py @@ -0,0 +1,32 @@ +import tkinter as tk +import tkinter.ttk as ttk + +class Controller(object): + def __init__(self, app): + self.app = app + + def run_process(self): + print("Run") + return 0 + +class Application(tk.Tk): + def __init__(self): + super().__init__() + self.con = Controller(self) + self.create_control_variables() + self.create_widgets() + + def create_control_variables(self): + pass + + def create_widgets(self): + self.b = ttk.Button(master=self, text="Start", command=self.con.run_process) + self.b.grid(row=1, column=0) + + +# -------------------------- +# Execution +# -------------------------- +if __name__ == "__main__": + app = Application() + app.mainloop()