-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
49 lines (41 loc) · 1.11 KB
/
main.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
from kivy.app import App
from kivy.lang import Builder
# .kv of main_window
main_window = Builder.load_string("""
#:import KivyLexer kivy.extras.highlight.KivyLexer
BoxLayout:
orientation: "horizontal"
CodeInput:
id: kv_code
style_name: "native"
lexer: KivyLexer()
on_text: app.update(text=self.text)
BoxLayout:
id: view_space
size_hint_x: 0.6
""")
# kivy app class
class app(App):
def update(self, text):
if len(text) == 0: # .kv is empty
return
try: # .kv is correct
self.widget = Builder.load_string(text)
except: # else don't load
pass
# load correct .kv
main_window.ids.view_space.clear_widgets()
try:
main_window.ids.view_space.add_widget(self.widget)
except:
return
# save correct .kv
with open("correct.kv", "w") as file:
file.write(text)
file.close()
# kivy main function
def build(self):
self.widget = Builder.load_string("BoxLayout:")
return main_window
# kivy run
app().run()