4646font = bitmap_font .load_font ("/fonts/Arial-12.bdf" )
4747buttons = []
4848
49- # Some button placement functions
49+ # Some button functions
5050def button_grid (row , col ):
5151 return Coords (BUTTON_MARGIN * (row + 1 ) + BUTTON_WIDTH * row + 20 ,
5252 BUTTON_MARGIN * (col + 1 ) + BUTTON_HEIGHT * col + 40 )
5353
54- def make_button (row , col , label , width = 1 , color = WHITE , text_color = BLACK ):
54+ def add_button (row , col , label , width = 1 , color = WHITE , text_color = BLACK ):
5555 pos = button_grid (row , col )
5656 new_button = Button (x = pos .x , y = pos .y ,
5757 width = BUTTON_WIDTH * width + BUTTON_MARGIN * (width - 1 ),
@@ -60,29 +60,36 @@ def make_button(row, col, label, width=1, color=WHITE, text_color=BLACK):
6060 buttons .append (new_button )
6161 return new_button
6262
63+ def find_button (label ):
64+ result = None
65+ for _ , btn in enumerate (buttons ):
66+ if btn .label == label :
67+ result = btn
68+ return result
69+
6370border = Rect (20 , 8 , 280 , 35 , fill = WHITE , outline = BLACK , stroke = 2 )
6471calc_display = Label (font , text = "0" , color = BLACK , max_glyphs = MAX_DIGITS )
6572calc_display .y = 25
6673
67- clear_button = make_button (0 , 0 , "AC" )
68- make_button (1 , 0 , "+/-" )
69- make_button (2 , 0 , "%" )
70- make_button (3 , 0 , "/" , 1 , ORANGE , WHITE )
71- make_button (0 , 1 , "7" )
72- make_button (1 , 1 , "8" )
73- make_button (2 , 1 , "9" )
74- make_button (3 , 1 , "x" , 1 , ORANGE , WHITE )
75- make_button (0 , 2 , "4" )
76- make_button (1 , 2 , "5" )
77- make_button (2 , 2 , "6" )
78- make_button (3 , 2 , "-" , 1 , ORANGE , WHITE )
79- make_button (0 , 3 , "1" )
80- make_button (1 , 3 , "2" )
81- make_button (2 , 3 , "3" )
82- make_button (3 , 3 , "+" , 1 , ORANGE , WHITE )
83- make_button (0 , 4 , "0" , 2 )
84- make_button (2 , 4 , "." )
85- make_button (3 , 4 , "=" , 1 , ORANGE , WHITE )
74+ clear_button = add_button (0 , 0 , "AC" )
75+ add_button (1 , 0 , "+/-" )
76+ add_button (2 , 0 , "%" )
77+ add_button (3 , 0 , "/" , 1 , ORANGE , WHITE )
78+ add_button (0 , 1 , "7" )
79+ add_button (1 , 1 , "8" )
80+ add_button (2 , 1 , "9" )
81+ add_button (3 , 1 , "x" , 1 , ORANGE , WHITE )
82+ add_button (0 , 2 , "4" )
83+ add_button (1 , 2 , "5" )
84+ add_button (2 , 2 , "6" )
85+ add_button (3 , 2 , "-" , 1 , ORANGE , WHITE )
86+ add_button (0 , 3 , "1" )
87+ add_button (1 , 3 , "2" )
88+ add_button (2 , 3 , "3" )
89+ add_button (3 , 3 , "+" , 1 , ORANGE , WHITE )
90+ add_button (0 , 4 , "0" , 2 )
91+ add_button (2 , 4 , "." )
92+ add_button (3 , 4 , "=" , 1 , ORANGE , WHITE )
8693
8794# Add the display and buttons to the main calc group
8895calc_group .append (border )
@@ -96,15 +103,25 @@ def make_button(row, col, label, width=1, color=WHITE, text_color=BLACK):
96103while True :
97104 point = ts .touch_point
98105 if point is not None :
99- for i , b in enumerate (buttons ):
106+ # Button Down Events
107+ for _ , b in enumerate (buttons ):
100108 if b .contains (point ) and button == "" :
101109 b .selected = True
102110 button = b .label
103- time .sleep (0.1 )
111+ elif button != "" :
112+ # Button Up Events
113+ last_op = calculator .get_current_operator ()
114+ op_button = find_button (last_op )
115+ # Deselect the last operation when certain buttons are pressed
116+ if op_button is not None :
117+ if button in ('=' , 'AC' , 'CE' ):
118+ op_button .selected = False
119+ elif button in ('+' , '-' , 'x' , '/' ) and button != last_op :
120+ op_button .selected = False
121+ calculator .add_input (button )
122+ b = find_button (button )
123+ if b is not None :
124+ if button not in ('+' , '-' , 'x' , '/' ) or button != calculator .get_current_operator ():
104125 b .selected = False
105- break
106- else :
107- if button != "" :
108- calculator .add_input (button )
109- button = ""
126+ button = ""
110127 time .sleep (0.05 )
0 commit comments