@@ -84,6 +84,13 @@ def make_button(row, col, label, width=1, color=WHITE, text_color=BLACK):
8484make_button (2 , 4 , "." )
8585make_button (3 , 4 , "=" , 1 , ORANGE , WHITE )
8686
87+ def find_button (label ):
88+ result = None
89+ for i , b in enumerate (buttons ):
90+ if b .label == label :
91+ result = b
92+ return result
93+
8794# Add the display and buttons to the main calc group
8895calc_group .append (border )
8996calc_group .append (calc_display )
@@ -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 :
106+ # Button Down Events
99107 for i , 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