@@ -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
0 commit comments