@@ -64,10 +64,11 @@ def done(value):
64
64
if button_pushed == "B" : return done (False )
65
65
if button_pushed == "A" : return done (True )
66
66
67
- def prompt_text (description , init_text = "" , true_text = "OK" , false_text = "Back" , width = 300 , height = 200 , font = "Roboto_BlackItalic24" ):
67
+ def prompt_text (description , init_text = "" , true_text = "OK" , false_text = "Back" , width = 300 , height = 200 , font = "Roboto_BlackItalic24" , cb = None ):
68
68
"""Shows a dialog and keyboard that allows the user to input/change a string
69
69
70
- Returns None if user aborts with button B
70
+ Calls the 'cb' callback or return None if user aborts with button B. Using a callback is highly recommended as it's not
71
+ possible to process events inside an event callback.
71
72
72
73
The caller is responsible for flushing the display after processing the response.
73
74
"""
@@ -86,13 +87,33 @@ def prompt_text(description, init_text = "", true_text="OK", false_text="Back",
86
87
ugfx .set_default_font ("Roboto_Regular12" )
87
88
button_height = 25
88
89
89
- def okay (evt ):
90
- # We'd like promises here, but for now this should do
91
- global wait_for_interrupt
92
- button_pushed = "A"
93
- wait_for_interrupt = False
94
-
95
- button_yes = ugfx .Button (int (width * 4 / 5 ), height - kb_height - button_height , int (width * 1 / 5 )- 3 , button_height , true_text , parent = window , cb = okay )
90
+ def done (result ):
91
+ window .destroy ()
92
+ if cb :
93
+ cb (result )
94
+ return result
95
+
96
+ def syncSuccess (evt ):
97
+ if evt :
98
+ # We'd like promises here, but for now this should do
99
+ global wait_for_interrupt
100
+ button_pushed = "A"
101
+ wait_for_interrupt = False
102
+ def syncCancel (evt ):
103
+ if evt :
104
+ # We'd like promises here, but for now this should do
105
+ global wait_for_interrupt
106
+ button_pushed = "B"
107
+ wait_for_interrupt = False
108
+
109
+ def asyncSuccess (evt ):
110
+ if evt :
111
+ done (edit .text ())
112
+ def asyncCancel (evt ):
113
+ if evt :
114
+ done (False )
115
+
116
+ button_yes = ugfx .Button (int (width * 4 / 5 ), height - kb_height - button_height , int (width * 1 / 5 )- 3 , button_height , true_text , parent = window , cb = asyncSuccess if cb else syncSuccess )
96
117
button_no = ugfx .Button (int (width * 4 / 5 ), height - kb_height - button_height - button_height , int (width / 5 )- 3 , button_height , false_text , parent = window ) if false_text else None
97
118
ugfx .set_default_font (font )
98
119
label = ugfx .Label (5 , 1 , int (width * 4 / 5 ), height - kb_height - 5 - edit_height - 5 , description , parent = window )
@@ -114,14 +135,14 @@ def toggle_focus(pressed):
114
135
elif focus == 1 or not button_no :
115
136
button_yes .set_focus ()
116
137
kb .enabled (0 )
117
- ugfx .input_attach (ugfx .BTN_A , pressed_a )
118
- ugfx .input_attach (ugfx .BTN_B , pressed_b )
138
+ ugfx .input_attach (ugfx .BTN_A , asyncSuccess if cb else syncSuccess )
139
+ ugfx .input_attach (ugfx .BTN_B , asyncCancel if cb else syncCancel )
119
140
focus = (2 if button_no else 0 )
120
141
else :
121
142
button_no .set_focus ()
122
143
kb .enabled (0 )
123
- ugfx .input_attach (ugfx .BTN_A , pressed_a )
124
- ugfx .input_attach (ugfx .BTN_B , pressed_b )
144
+ ugfx .input_attach (ugfx .BTN_A , asyncCancel if cb else syncCancel )
145
+ ugfx .input_attach (ugfx .BTN_B , asyncCancel if cb else syncCancel )
125
146
focus = 0
126
147
ugfx .flush ()
127
148
@@ -138,21 +159,14 @@ def toggle_focus(pressed):
138
159
ugfx .flush ()
139
160
140
161
wait_for_interrupt = True
141
- while wait_for_interrupt :
142
- time .sleep (0.2 )
143
-
144
- def done (value ):
145
- window .hide ()
146
- window .destroy ()
147
- button_yes .destroy ()
148
- if button_no : button_no .destroy ()
149
- label .destroy ()
150
- kb .destroy ()
151
- edit .destroy ()
152
- return value
162
+ if cb :
163
+ return
164
+ else :
165
+ while wait_for_interrupt :
166
+ time .sleep (0.2 )
153
167
154
- if (focus == 0 and no_button ) or button_pushed == "B" : return done (False )
155
- return done (edit .text ())
168
+ if (focus == 0 and no_button ) or button_pushed == "B" : return done (False )
169
+ return done (edit .text ())
156
170
157
171
def prompt_option (options , index = 0 , text = "Please select one of the following:" , title = None , select_text = "OK" , none_text = None ):
158
172
"""Shows a dialog prompting for one of multiple options
0 commit comments