-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKivyUI.py
111 lines (89 loc) · 3.93 KB
/
KivyUI.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
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#This programme can be found on https://github.com/NepoBigo/TuringGame/
#Please read the readme.md attached
from kivy.uix.spinner import Spinner
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.app import App
from kivy.uix.button import Button
from kivy.core.window import Window
from kivy.config import Config
#ensure window cannot be resized so all options will be shown
#Config.set('graphics', 'resizable', False)
#Config.write()
##Defines KivyUI
#sets up drop down menu (the "columns")
class DropMenu(Spinner):
def __init__ (self, list, **kwargs):
super().__init__(**kwargs)
self.text_autoupdate = True
self.values=(list)
self.size_hint=(None, None)
self.size=(100, 44)
self.pos_hint={'center_x': .5, 'center_y': .5}
#sets up on state (the "rows")
class StateBox(BoxLayout):
def __init__(self, state, states, **kwargs):
super().__init__(**kwargs)
self.orientation = "horizontal"
self.state = state
#label of state machine
self.stateLabel = Label(text = "State " + self.state, size=(100, 44))
self.add_widget(self.stateLabel)
#possible vals and dirs of turing machine
vals = ["X", "0", "1"]
dirs = ["1", "0", "-1"]
stateList = [str(i) for i in range(states)] + ["S"]
self.dropMenuDict = {}
#creates a row of dropdown to programme a state
for val in vals:
prop = [["val", vals], ["dir", dirs], ["sta", stateList]]
for name, menuList in prop:
varname = name + val
self.dropMenuDict[varname] = DropMenu(menuList)
self.add_widget(self.dropMenuDict[varname])
#defines the app
class ConfDictMaker(App):
def __init__(self, states, **kwargs):
#initialise values
super().__init__(**kwargs)
self.states = states
self.outputDict = None
Window.size = (1000, 44*states+100)
def build(self):
self.root = BoxLayout(orientation = "vertical") #box layout for whole window
#label for instruction
self.root.add_widget(Label(text = "The Turing Machine will start from state 0. Values in the drop down menu may be hidden. Scroll after selection to see more."))
#labels for table
self.labels = BoxLayout(orientation = "horizontal")
self.root.add_widget(self.labels)
self.labels.add_widget(Label(text = "State", size=(100, 44)))
vals = ["X", "0", "1"]
for val in vals:
prop = ["Write if " + val, "Move if " + val, "State if " + val]
for Name in prop:
self.labels.add_widget(Label(text = Name, size=(100, 44)))
#sets up row of dropdown menu :
self.stateDict = {}
for state in range(self.states):
varname = "state" + str(state)
self.stateDict[varname] = StateBox(str(state), self.states)
self.root.add_widget(self.stateDict[varname])
#button to close dictionary
self.Btn = Button(text = "Submit Configuration", on_press = self.outputAndClose)
self.root.add_widget(self.Btn)
#closes kivy app
def outputAndClose(self, instance):
App.get_running_app().stop() #closes app
#on close, returns dictionary. Prevents crashing if user accidentally closes window instead
def on_stop(self):
self.outputDict = {} #dictionary to compile different states
for stateMenu in self.stateDict.values():
update = stateMenu.dropMenuDict #imports dictionary from a stateBox
confState = {stateMenu.state: {"X": {"output": {"val": update["valX"].text, "dir": int(update["dirX"].text)}, "nextstate": update["staX"].text},
"0": {"output": {"val": update["val0"].text, "dir": int(update["dir0"].text)}, "nextstate": update["sta0"].text},
"1": {"output": {"val": update["val1"].text, "dir": int(update["dir1"].text)}, "nextstate": update["sta1"].text}}}
self.outputDict.update(confState) #updates dictionary
# test = ConfDictMaker(7)
# if __name__ == '__main__':
# test.run()
# print(test.outputDict)