From 8e7a1cbf765d57f0d938c0e97fa483f70e634dfd Mon Sep 17 00:00:00 2001 From: Martin Putzlocher Date: Wed, 1 Dec 2021 13:01:28 +0100 Subject: [PATCH] twolights added --- tk_first_steps/two_lights_simple_mvc.py | 99 +++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 tk_first_steps/two_lights_simple_mvc.py diff --git a/tk_first_steps/two_lights_simple_mvc.py b/tk_first_steps/two_lights_simple_mvc.py new file mode 100644 index 0000000..9015ca8 --- /dev/null +++ b/tk_first_steps/two_lights_simple_mvc.py @@ -0,0 +1,99 @@ +#!/usr/bin/env python3 +from tkinter import * +from tkinter import ttk + +# -------------------------- +# Model +# -------------------------- +class DoubleLight: + def __init__(self): + self.l1 = 1 + self.l2 = 0 + + def switch(self): + if self.l1 == 1: + self.set_l2() + else: + self.set_l1() + + def set_l1(self): + self.l1 = 1 + self.l2 = 0 + + def set_l2(self): + self.l1 = 0 + self.l2 = 1 + + def print_status(self): + print("l1: " + str(self.l1) + "| l2: " + str(self.l2)) + + def get_active(self): + if self.l1 == 1: + return 1 + else: + return 2 + +# -------------------------- +# Controller +# -------------------------- +class TwoLightsController: + def __init__(self, app): + self.app = app + self.lights = DoubleLight() + + def switch_lights(self, event=None): + self.lights.switch() + self.update_view() + + def update_view(self): + active_light = self.lights.get_active() + self.app.change_light_display(active_light) + +# -------------------------- +# View / Main Application +# -------------------------- +class TwoLightsApp(Tk): + def __init__(self): + super().__init__() + self.controller = TwoLightsController(self) + + self.color1 = "dark green" + self.color2 = "green yellow" + + self.create_widgets() + self.create_lights(self.color1) + self.bind_events() + + def create_widgets(self): + mainframe = ttk.Frame(self, padding="3 3 3 3") + mainframe.grid(column=0, row=0, sticky=N+W+S+E) + self.columnconfigure(0, weight=1) + self.rowconfigure(0, weight=1) + button_switch = ttk.Button(mainframe, text="Schalte um", command=self.controller.switch_lights) + button_switch.grid(column=0, row=0, sticky=W) + light_canvas = Canvas(mainframe, width=50, height=50, background="black") + light_canvas.grid(column=1, row=0, sticky=W) + self.lc = light_canvas + + def create_lights(self, color="dark green"): + self.light1 = self.lc.create_rectangle(5,5,25,45,fill=color) + self.light2 = self.lc.create_rectangle(25,5,45,45,fill=color) + self.controller.update_view() + + def bind_events(self): + self.bind("", self.controller.switch_lights) + + def change_light_display(self, active_light): + if active_light == 1: + self.lc.itemconfig(self.light1, fill=self.color2) + self.lc.itemconfig(self.light2, fill=self.color1) + else: + self.lc.itemconfig(self.light1, fill=self.color1) + self.lc.itemconfig(self.light2, fill=self.color2) + +# -------------------------- +# Execution +# -------------------------- +if __name__ == "__main__": + app = TwoLightsApp() + app.mainloop()