-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathui.py
executable file
·59 lines (45 loc) · 1.39 KB
/
ui.py
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
#!/usr/bin/env python3
# add parent to the path to include local xstate module
import sys
sys.path.insert(0, "..")
import tkinter # noqa
from xstate import Machine # noqa
class ApplicationBasic:
def __init__(self):
self.init_ui()
self.init_FSM()
def init_FSM(self):
# Trafic light example
# green -> yellow -> red -> green ..
self.fsm = Machine(
{
"id": "lights",
"initial": "green",
"states": {
"green": {
"on": {"TIMER": "yellow"},
},
"yellow": {"on": {"TIMER": "red"}},
"red": {"on": {"TIMER": "green"}},
},
}
)
self.state = self.fsm.initial_state
self.update_label()
def init_ui(self):
self.fen = tkinter.Tk()
self.label = tkinter.Label(self.fen, text="")
self.label.pack()
self.button = tkinter.Button(self.fen, text="TIMER", command=self.action)
self.button.pack()
def action(self):
print("action")
self.state = self.fsm.transition(self.state, "TIMER")
self.update_label()
def update_label(self):
self.label["text"] = self.state.value
def run(self):
self.fen.mainloop()
if __name__ == "__main__":
app = ApplicationBasic()
app.run()