-
Notifications
You must be signed in to change notification settings - Fork 369
Expand file tree
/
Copy pathmvc-after.py
More file actions
84 lines (66 loc) · 2.11 KB
/
mvc-after.py
File metadata and controls
84 lines (66 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import tkinter as tk
import uuid
from abc import ABC, abstractmethod
class Model:
def __init__(self):
self.uuid = []
def append(self, item):
self.uuid.append(item)
def clear(self):
self.uuid = []
class Controller:
def __init__(self, model, view):
self.model = model
self.view = view
def start(self):
self.view.setup(self)
self.view.start_main_loop()
def handle_click_generate_uuid(self):
# generate a uuid and add it to the list
newid = uuid.uuid4()
self.model.append(newid)
self.view.append_to_list(newid)
def handle_click_clear_list(self):
# clear the uuid list in the model and the view
self.model.clear()
self.view.clear_list()
class View(ABC):
@abstractmethod
def setup(self, controller):
pass
@abstractmethod
def append_to_list(self, item):
pass
@abstractmethod
def clear_list(self):
pass
@abstractmethod
def start_main_loop(self):
pass
class TkView(View):
def setup(self, controller):
# setup tkinter
self.root = tk.Tk()
self.root.geometry("400x400")
self.root.title("UUIDGen")
# create the gui
self.frame = tk.Frame(self.root)
self.frame.pack(fill=tk.BOTH, expand=1)
self.label = tk.Label(self.frame, text="Result:")
self.label.pack()
self.list = tk.Listbox(self.frame)
self.list.pack(fill=tk.BOTH, expand=1)
self.generate_uuid_button = tk.Button(self.frame, text="Generate UUID", command=controller.handle_click_generate_uuid)
self.generate_uuid_button.pack()
self.clear_button = tk.Button(self.frame, text="Clear list", command=controller.handle_click_clear_list)
self.clear_button.pack()
def append_to_list(self, item):
self.list.insert(tk.END, item)
def clear_list(self):
self.list.delete(0, tk.END)
def start_main_loop(self):
# start the loop
self.root.mainloop()
# create the MVC & start the application
c = Controller(Model(), TkView())
c.start()