100 lines
2.9 KiB
Python
100 lines
2.9 KiB
Python
#!/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("<Return>", 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()
|