-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcalc_widget.py
196 lines (167 loc) · 5.47 KB
/
calc_widget.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import kivy
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
from kivy.core.window import Window
import subprocess
import configparser
Builder.load_string('''
<CalcButt@Button>
on_press: self.parent.parent.parent.do_action(self.text)
font_size: sp(30)
<CalcScreen>:
display: entry
GridLayout:
rows: 7
padding: 2
spacing: 4
BoxLayout:
TextInput:
id: entry
multiline: False
on_text_validate: root.handle_input(self.text)
readonly: True
is_focusable: False
use_bubble: False
use_handles: False
font_size: sp(30)
Button:
size_hint_x: None
width: dp(100)
text: '<-'
font_size: sp(30)
on_press: entry.text = entry.text[:-1]
Button:
size_hint_x: None
width: dp(100)
text: 'Clr'
font_size: sp(30)
on_press: entry.text = ""
BoxLayout:
CalcButt:
text: '1'
CalcButt:
text: '2'
CalcButt:
text: '3'
CalcButt:
text: '+'
BoxLayout:
CalcButt:
text: '4'
CalcButt:
text: '5'
CalcButt:
text: '6'
CalcButt:
text: '-'
BoxLayout:
CalcButt:
text: '7'
CalcButt:
text: '8'
CalcButt:
text: '9'
CalcButt:
text: '/'
BoxLayout:
CalcButt:
text: '_'
CalcButt:
text: '0'
CalcButt:
text: '.'
CalcButt:
text: '*'
BoxLayout:
Button:
text: 'Off'
font_size: sp(30)
on_press: root.manager.current = 'main'
ToggleButton:
text: 'mm' if self.state == 'normal' else 'inch'
on_press: root.do_mm(self.state == 'normal')
CalcButt:
text: 'Space'
CalcButt:
text: '='
''')
class CalcScreen(Screen):
def __init__(self, **kwargs):
super(CalcScreen, self).__init__(**kwargs)
self.app = App.get_running_app()
config = configparser.ConfigParser()
config.read('smoothiehost.ini')
self.backend = config.get('General', 'calc_backend', fallback="eval")
def _add_line_to_log(self, s):
self.app.main_window.display(s)
def do_action(self, key):
if key == '=':
res = self._do_calc(self.display.text)
self.display.text = res
elif key == 'Space':
self.display.text += " "
elif key == '_' and self.backend != "dc":
self.display.text += "-"
else:
self.display.text += key
def do_mm(self, flg):
try:
v = float(self.display.text)
if flg:
r = v * 25.4
unit = 'inch'
else:
r = v / 25.4
unit = 'mm'
self.display.text = f"{r:1.4f}"
self.app.main_window.display(f"< {v:1.4f} {unit} = {r:1.4f} {'mm' if flg else 'inch'}")
except Exception as err:
# self.app.main_window.display(f'< calculator error: {err}')
pass
def _do_calc(self, txt):
self.app.main_window.display(f"> {txt}")
# send to unix shell
try:
if self.backend == 'dc':
p = subprocess.Popen("dc", stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
result, err = p.communicate(timeout=5, input="10 k {} p".format(txt))
elif self.backend == 'bc':
p = subprocess.Popen("bc", stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
result, err = p.communicate(timeout=5, input="scale=10; {}\n".format(txt))
else:
result = f"{eval(txt)}"
self.app.main_window.display(f"< {result}")
return result
if p.returncode == 0:
self.app.main_window.display(f"< {result}")
return " ".join(result.splitlines())
except subprocess.TimeoutExpired:
p.kill()
self.app.main_window.display('< calculator timed out')
except Exception as err:
self.app.main_window.display(f'< calculator error: {err}')
return "Error"
if __name__ == '__main__':
Builder.load_string('''
<ExitScreen>:
on_enter: app.stop()
''')
class ExitScreen(Screen):
pass
class MainWindow:
def display(self, x):
print(x)
class CalcApp(App):
def __init__(self, **kwargs):
super(CalcApp, self).__init__(**kwargs)
self.main_window = MainWindow()
def build(self):
Window.size = (800, 600)
self.sm = ScreenManager()
self.sm.add_widget(CalcScreen(name='calculator'))
self.sm.add_widget(ExitScreen(name='main'))
self.sm.current = 'calculator'
return self.sm
CalcApp().run()