-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathInputAlert.py
137 lines (122 loc) · 4.58 KB
/
InputAlert.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import ui
class InputAlert(ui.View):
'''a ui that is added to the root view of a class, as a subview.
call input_alert() to simulate a modal dialog.
if cancel is selected, returns None.
note that input_alert must be called in a background thread, since it does not return until the dialog is closed.
also, note if view is closed while dialog is open, the dialog still returns.'''
def __init__(self):
from threading import Event
self.e=Event()
self.width=1
self.height=1
def input_alert(self,title='',
message='',
input='',
ok_button_title='ok',
hide_cancel_button=False):
#first, cover up view
#then
from threading import Timer
print threading.enumerate()
from thread import interrupt_main
self.e.clear()
for s in self.subviews:
self.remove_subview(s)
if self.superview:
dim=ui.View(frame=(0,0,self.superview.width, self.superview.height), bg_color=(0.5, 0.5, 0.5, 0.75))
inputbox=ui.View()
inputbox.width=300
inputbox.corner_radius=10
inputbox.bg_color='white'
inputbox.border_color='black'
inputbox.center = (dim.width/2,dim.height/2)
titlelabel = ui.Label()
titlelabel.text=title
titlelabel.alignment=ui.ALIGN_CENTER
titlelabel.font=('<system-bold>',14)
titlelabel.size_to_fit()
titlelabel.y=10
titlelabel.x=0
messagelabel = ui.Label()
messagelabel.text=message
messagelabel.alignment=ui.ALIGN_CENTER
messagelabel.font=('<system>',12)
messagelabel.x=0
messagelabel.y=titlelabel.height+titlelabel.y
messagelabel.size_to_fit()
inputfield = ui.TextField()
inputfield.text=input
inputfield.font=('<system>',12)
inputfield.height=titlelabel.height*1.5
inputfield.x=0.1*inputbox.width
inputfield.y=messagelabel.height + messagelabel.y
inputfield.width = .8*inputbox.width
inputfield.corner_radius=0
inputfield.border_width=1
inputfield.action=self.input_action
buttons=ui.SegmentedControl()
buttons.corner_radius=0
buttons.height=30
buttons.segments=('Cancel','Ok')
buttons.width=titlelabel.width=messagelabel.width=inputbox.width
buttons.width+=10
buttons.x-=5
buttons.y=inputfield.y+inputfield.height+10
buttons.action = self.button_action
inputbox.height = buttons.y+buttons.height-1
dim.add_subview(inputbox)
inputbox.add_subview(titlelabel)
inputbox.add_subview(messagelabel)
inputbox.add_subview(inputfield)
inputbox.add_subview(buttons)
self.superview.add_subview(dim)
self.bring_to_front()
dim.bring_to_front()
self.width=dim.width
self.height=dim.height
def check_if_onscreen():
if not self.on_screen:
self.e.set()
else:
Timer(0.25,check_if_onscreen).start()
check_if_onscreen()
self.e.wait()
self.superview.remove_subview(dim)
if not hide_cancel_button and buttons.selected_index==0:
rtnvalue=None
self.width=1
self.height=1
interrupt_main()
self.e.set()
else:
rtnvalue=inputfield.text
self.width=1
self.height=1
return rtnvalue
def button_action(self,sender):
self.e.set()
def input_action(self,sender):
self.e.set()
def get_top_view(self):
'''find root window'''
#find root window
root=self
while root.superview:
root=root.superview
return root
if __name__=='__main__':
v=ui.View(frame=(0,0, 500, 600))
b=ui.Button(frame=(20,20,100,100),bg_color='blue')
v.add_subview(b)
v.present('sheet')
i=InputAlert()
v.add_subview(i)
#b.bring_to_front()
@ui.in_background
def ia(sender):
#print threading.current_thread()
#print 'push'
b.bg_color= i.input_alert('enter a color','valid coloname','green')
b.action=ia
b.title='push'