Skip to content

Commit 0d1ab62

Browse files
committed
Calculator_widget: Allow dot and comma decimals
1 parent 6f877af commit 0d1ab62

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed

lib/python/gladevcp/calculatorwidget.py

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class Calculator( Gtk.Box ):
4646

4747
def __init__( self, *a, **kw ):
4848
Gtk.Box.__init__( self, *a, **kw )
49+
self.allow_dot_and_comma = False
4950
self.preset_value = None
5051
self.eval_string = ""
5152
self.font = "sans 12"
@@ -170,19 +171,19 @@ def get_preset_value( self ):
170171

171172
def compute( self ):
172173
qualified = ''
173-
# print"string:",self.eval_string
174174
temp = self.eval_string.strip( " " ).replace("Pi", "math.pi")
175+
if self.allow_dot_and_comma: temp = temp.replace(",", ".")
175176
# this loop adds only spaces around the mentioned operators
176177
for i in( '-', '+', '/', '*', 'math.pi', '(', ')' ):
177178
new = " %s " % i
178179
temp = temp.replace( i, new )
179180
for i in temp.split():
180-
# print ( "i in compute = ", i )
181-
try:
182-
i = str( locale.atof( i ) )
183-
# print ( "converted i in compute = ", i )
184-
except:
185-
pass
181+
if not self.allow_dot_and_comma:
182+
try:
183+
# convert to decimal dot format
184+
i = str( locale.atof( i ) )
185+
except:
186+
pass
186187
if i.isdigit():
187188
qualified = qualified + str( float( i ) )
188189
else:
@@ -194,18 +195,16 @@ def compute( self ):
194195
b = str( eval( qualified ) )
195196
except:
196197
b = "Error"
197-
print("Calculator widget error, string:", self.eval_string, sys.exc_info()[0])
198198
self.eval_string = ''
199-
else : self.eval_string = b
200-
# if locale.localeconv()["decimal_point" = comma ,
201-
# we have to replace the internal dot by a comma,
202-
# otherwise it will be interpreted as an thousend separator
203-
try:
204-
b = locale.format_string( "%f", float( b ) ).rstrip( "0" )
205-
if b[-1] == locale.localeconv()["decimal_point"]:
206-
b = b.rstrip( locale.localeconv()["decimal_point"] )
207-
except:
208-
b = "Error"
199+
if not self.allow_dot_and_comma:
200+
try:
201+
b = locale.format_string( "%f", float( b ) ).rstrip( "0" )
202+
if b[-1] == locale.localeconv()["decimal_point"]:
203+
b = b.rstrip( locale.localeconv()["decimal_point"] )
204+
else:
205+
self.eval_string = b
206+
except:
207+
b = "Error"
209208
self.entry.set_text( b )
210209
self.entry.set_position(len(self.eval_string))
211210

src/emc/usr_intf/gmoccapy/dialogs.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ def entry_dialog(self, caller, data = None, header = _("Enter value") , label =
7878
label.modify_font(Pango.FontDescription("sans 20"))
7979
label.set_margin_top(15)
8080
calc = gladevcp.Calculator()
81+
calc.allow_dot_and_comma = True
8182
content_area = dialog.get_content_area()
8283
content_area.pack_start(child=label, expand=False, fill=False, padding=0)
8384
content_area.add(calc)

0 commit comments

Comments
 (0)