added simple traffic light example
This commit is contained in:
		
							
								
								
									
										84
									
								
								tk_first_steps/traffic_light_simple_mvc.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										84
									
								
								tk_first_steps/traffic_light_simple_mvc.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,84 @@
 | 
			
		||||
#!/usr/bin/env python3
 | 
			
		||||
 | 
			
		||||
from tkinter import *
 | 
			
		||||
from tkinter import ttk
 | 
			
		||||
 | 
			
		||||
# --------------------------
 | 
			
		||||
# Model
 | 
			
		||||
# --------------------------
 | 
			
		||||
 | 
			
		||||
class TLight:
 | 
			
		||||
    def __init__(self):
 | 
			
		||||
        self.colors = {0: "red", 1: "yellow", 2: "green", 3: "yellow"}
 | 
			
		||||
        self.status = 0
 | 
			
		||||
        
 | 
			
		||||
    def switch(self):
 | 
			
		||||
        if self.status < max(self.colors.keys()):
 | 
			
		||||
            self.status += 1
 | 
			
		||||
        else:
 | 
			
		||||
            self.status = 0
 | 
			
		||||
            
 | 
			
		||||
    def print_color(self):
 | 
			
		||||
        print(self.colors[self.status])
 | 
			
		||||
        
 | 
			
		||||
    def get_color(self):
 | 
			
		||||
        return self.colors[self.status]
 | 
			
		||||
    
 | 
			
		||||
# --------------------------
 | 
			
		||||
# Controller
 | 
			
		||||
# --------------------------
 | 
			
		||||
 | 
			
		||||
class TLightController:
 | 
			
		||||
    def __init__(self, app):
 | 
			
		||||
        self.app = app
 | 
			
		||||
        self.tlight = TLight()
 | 
			
		||||
        
 | 
			
		||||
    def change_light(self, event=None):
 | 
			
		||||
        self.tlight.switch()
 | 
			
		||||
        self.update_view()
 | 
			
		||||
        
 | 
			
		||||
    def update_view(self):
 | 
			
		||||
        color = self.tlight.get_color()
 | 
			
		||||
        self.app.change_light_display(color)
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
# --------------------------
 | 
			
		||||
# View / Main Application
 | 
			
		||||
# --------------------------
 | 
			
		||||
 | 
			
		||||
class TLightApp(Tk):
 | 
			
		||||
    def __init__(self):
 | 
			
		||||
        super().__init__()
 | 
			
		||||
        self.controller = TLightController(self)
 | 
			
		||||
        
 | 
			
		||||
        self.create_widgets()
 | 
			
		||||
        self.create_light()
 | 
			
		||||
        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.change_light)
 | 
			
		||||
        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_light(self, color="red"):
 | 
			
		||||
        self.light = self.lc.create_oval(10,10,40,40,fill=color)
 | 
			
		||||
        
 | 
			
		||||
    def bind_events(self):
 | 
			
		||||
        self.bind("<Return>", self.controller.change_light)
 | 
			
		||||
    
 | 
			
		||||
    def change_light_display(self, color):
 | 
			
		||||
        self.lc.itemconfigure(self.light, fill=color)
 | 
			
		||||
        
 | 
			
		||||
# --------------------------
 | 
			
		||||
# Execution
 | 
			
		||||
# --------------------------
 | 
			
		||||
if __name__ == "__main__":
 | 
			
		||||
    app = TLightApp()
 | 
			
		||||
    app.mainloop()
 | 
			
		||||
		Reference in New Issue
	
	Block a user