def __init__(self, root_node, allow_multi=False, async_mode=False):
self.async_mode = async_mode
self.allow_multi = allow_multi
self.selected_entries = None
self.table_view = ui.TableView()
self.table_view.frame = (0, 0, 500, 500)
self.table_view.data_source = self
self.table_view.delegate = self
self.table_view.flex = 'WH'
self.table_view.allows_multiple_selection = True
self.table_view.tint_color = 'gray'
self.view = ui.View(frame=self.table_view.frame)
self.view.add_subview(self.table_view)
self.view.name = root_node.title
self.busy_view = ui.View(frame=self.view.bounds, flex='WH', background_color=(0, 0, 0, 0.35))
hud = ui.View(frame=(self.view.center.x - 50, self.view.center.y - 50, 100, 100))
hud.background_color = (0, 0, 0, 0.7)
hud.corner_radius = 8.0
hud.flex = 'TLRB'
spinner = ui.ActivityIndicator()
spinner.style = ui.ACTIVITY_INDICATOR_STYLE_WHITE_LARGE
spinner.center = (50, 50)
spinner.start_animating()
hud.add_subview(spinner)
self.busy_view.add_subview(hud)
self.busy_view.alpha = 0.0
self.view.add_subview(self.busy_view)
self.done_btn = ui.ButtonItem(title='Done', action=self.done_action)
if self.allow_multi:
self.view.right_button_items = [self.done_btn]
self.done_btn.enabled = False
self.root_node = root_node
self.entries = []
self.flat_entries = []
if self.async_mode:
self.set_busy(True)
t = threading.Thread(target=self.expand_root)
t.start()
else:
self.expand_root()
python类View()的实例源码
def __init__(self, *args, **kwargs):
ui.View.__init__(self, *args, **kwargs)
self.pinchgesture_recognizer_target = ui.Button()
self.pinchgesture_recognizer_target.action = self.did_pinch
self.pangesture_recognizer_target = ui.Button()
self.pangesture_recognizer_target.action = self.did_pan
self.gr_delegate=GRDelegate.alloc().init().autorelease()
self.recognizers={}
self_objc = ObjCInstance(self)
pinchobjctarget=ObjCInstance(self.pinchgesture_recognizer_target)
panobjctarget=ObjCInstance(self.pangesture_recognizer_target)
pinchrecognizer = ObjCClass('UIPinchGestureRecognizer').alloc()
self.recognizers['pinch'] = pinchrecognizer.initWithTarget_action_( pinchobjctarget, sel('invokeAction:')).autorelease()
panrecognizer = ObjCClass('UIPanGestureRecognizer').alloc()
self.recognizers['pan'] = panrecognizer.initWithTarget_action_( panobjctarget, sel('invokeAction:')).autorelease()
self.recognizers['pan'].setMinimumNumberOfTouches_(2)
for r in self.recognizers.values():
self_objc.addGestureRecognizer_(r)
r.setDelegate_(self.gr_delegate)
self.panx,self.pany,self.sx,self.sy=0,0,1,1
self.panx0,self.pany0,self.sx0,self.sy0=0,0,1,1
def ns(py_obj):
'''Convert common Python objects to their ObjC equivalents, i.e. str => NSString, int/float => NSNumber, list => NSMutableArray, dict => NSMutableDictionary, bytearray => NSData, set => NSMutableSet. Nested structures (list/dict/set) are supported. If an object is already an instance of ObjCInstance, it is left untouched.
'''
if isinstance(py_obj, ObjCInstance):
return py_obj
if isinstance(py_obj, c_void_p):
return ObjCInstance(py_obj)
if isinstance(py_obj, ui.View):
return ObjCInstance(py_obj)
if isinstance(py_obj, str):
return NSString.stringWithUTF8String_(py_obj)
if isinstance(py_obj, unicode):
return NSString.stringWithUTF8String_(py_obj.encode('utf-8'))
elif isinstance(py_obj, bytearray):
return NSData.dataWithBytes_length_(str(py_obj), len(py_obj))
elif isinstance(py_obj, int):
return NSNumber.numberWithInt_(py_obj)
elif isinstance(py_obj, float):
return NSNumber.numberWithDouble_(py_obj)
elif isinstance(py_obj, bool):
return NSNumber.numberWithBool_(py_obj)
elif isinstance(py_obj, list):
arr = NSMutableArray.array()
for obj in py_obj:
arr.addObject_(ns(obj))
return arr
elif isinstance(py_obj, set):
s = NSMutableSet.set()
for obj in py_obj:
s.addObject_(ns(obj))
return s
elif isinstance(py_obj, dict):
dct = NSMutableDictionary.dictionary()
for key, value in py_obj.iteritems():
dct.setObject_forKey_(ns(value), ns(key))
return dct
def __init__(self):
ui.View.__init__(self)
self.tv = None
self.difficulty = 1
self.starting = True
def make_board_scene(self, center_frame):
board_scene = SkChessBoardScene(self.game, center_frame)
scene_view = sk.View(board_scene)
scene_view.frame = center_frame # center_square()
scene_view.shows_fps = True
scene_view.shows_node_count = True
scene_view.shows_physics = True
return scene_view
#@classmethod
def __init__(self):
self.view = ui.View(background_color='lightyellow')
self.view.name = 'MiniPhotoView'
scrollview1 = ui.ScrollView()
scrollview1.name = 'scrollview1'
scrollview1.flex = 'WH'
scrollview1.content_size = (2000,2000)
self.view.add_subview(scrollview1)
self.view.present('full_screen')
self.sv1 = self.view['scrollview1']
width, height = self.sv1.content_size
self.sv1.add_subview(MyPictureView(width,height))
def popover(self, msg):
textview = ui.TextView()
textview.editable = False
textview.font = ('AmericanTypewriter', 24)
textview.alignment = ui.ALIGN_CENTER
textview.text = msg
pov = ui.View()
pov.width = textview.width = 222
pov.add_subview(textview)
pov.present('popover')
# in the .pyui file, the "Custom View Class" must be set to AreYouEnabledView
def __init__(self, *args,**kwargs):
ui.View.__init__(self,*args,**kwargs)
self.history=[] #future...keep track of recent colors
self.current=(.3,0.2,0.5)
self.N=16
self.Nb=32
def __init__(self,
frame=(0, 0, 600, 400),
**kwargs):
self._undo = UndoStack()
self._tv = ui.TextView(frame=(0,0,self.width,self.height),bg_color=(1.0,1.0,1.0))
self._tv.flex='WH'
self.add_subview(self._tv)
ui.View.__init__(self,**kwargs)
#ui props
self.frame=frame
self._tv.delegate = self
def add_subview(self,subviews):
if not hasattr(subviews,'__contains__'):
subviews=[subviews]
for s in subviews:
self.originalSizes.append(s.frame[2:3])
ui.View.add_subview(self,s)
self.layout()
def add_subview(self,subviews):
if not hasattr(subviews,'__contains__'):
subviews=[subviews]
_hidden=self.hidden
self.hidden=True
for s in subviews:
self.originalSizes.append(s.frame[2:])
print 'bf add'
ui.View.add_subview(self,s)
self.hidden=_hidden
#self.originalSizes.append(s.frame[2:])
print 'bf lay'
self.layout()
def __init__(self, N_onscreen=700,*args, **kwargs):
ui.View.__init__(self,*args,**kwargs)
# ready lock is used to protect calls to matplotlib
self.ready=threading.Lock()
#set up zoomable sliders
self.hslider=ZoomSlider(frame=(self.width*0.08,0,self.width*0.84,self.height*0.08),vert=0,flex='wt')
self.vslider=ZoomSlider(frame=(0,self.height*0.08,self.width*0.08,self.height*0.84),vert=1,flex='hr')
self.add_subview(self.hslider)
self.add_subview(self.vslider)
self.hslider.barvalue=0.125
self.hslider.barwidth=0.25
self.vslider.barvalue=0.5
self.vslider.barwidth=1.0
self.hslider.action=self.did_slide
self.vslider.action=self.did_slide
#matplotlib image output
self.img_view = ui.ImageView(frame=[self.width*0.08,self.height*0.08,self.width*0.84,self.height*0.84],flex='WH',bg_color=(1,1,1))
self.add_subview(self.img_view)
# image buffer
self.b = io.BytesIO()
#store base xlim and ylim, only update when drag ends
self.xlim=plt.xlim()
self.ylim=plt.ylim()
self.N_onscreen=N_onscreen # number of points onscreen
# fast and slow dpi.. set low_dpi to lower number for snappier response
self.high_dpi=92
self.low_dpi=16.
self.device_dpi=72
# set output image size to match view size. this probably should be modified to use actual device dpi and size. fonts and line width are based on pts, not pixels
plt.gcf().set_size_inches(self.img_view.width/self.device_dpi,self.img_view.height/self.device_dpi)
#update plot, ensuring update really happens
#self.update_plt(dpi=self.high_dpi, waitForLock=True)
#ObjCInstance(self).becomeFirstResponder()
def add_subview(self,subview):
#override add_subview to add to longtapbox
ui.View.add_subview(self.flow,subview)
def layout(self):
'''force internal slider to follow this Views width/height'''
s=self.slider
s.height,s.width=self.height,self.width
# s.x=s.y=0 # i thought this might be needed, but it seems not to be.
# we want various View parameters to masquerade as the internal slider's.
# the importand ones seem to be
# tint_color, bg_color, action, continuous, value
# though a few others like alpha, borderparameters, etc might be needed
# if you plan on modifying from within the action
def setupkb(self):
#define keys
redokey=key(title='redo',action=self.redoaction)
undokey=key(title='undo',subkeys=[redokey], action=self.undoaction)
hidekey=key(title='hide',action=self.hideaction)
keymap=[key('\t',title='TAB'),key('_'),key('#',['@']),key('<',['<=']),key('>',['>=']),
key('{'),key('}'),key('['),key(']'),key("'",['"']),key('('),key(')'),
key(':',[';']), undokey]+[key(str(n)) for n in range(1,9)]+[key('0'),key('+',['%']),key('-'),key('/',['\\n','\\t','\\','/']),key('*'),key('=',['!=']), hidekey]
#customkb component
customkb=FlowContainer(frame=(0,self.height-100,self.width,100),flex='')
customkb.name='customkb'
self.add_subview(customkb)
minimizedkb=ui.Button(frame=(0,self.height-15,self.width,15),flex='',bg_color=(.7, .7, .7))
minimizedkb.action=self.showaction
minimizedkb.title=u'\u2550'*10
minimizedkb.name='minimizedkb'
self.add_subview(minimizedkb)
customkb.bring_to_front()
customkb.hidden=True
for k in keymap:
customkb.add_subview(k.makeButton())
customkb.flex='WT'
customkb.y=self.height-customkb.height
#contentframe
content=ui.View(frame=(0,0,self.width,self.height-15))
content.name='content'
self.add_subview(content)
content.send_to_back()
content.border_color=(0,0,1)
content.border_width=3
self.content=content
def __init__(self, *args, **kwargs):
ui.View.__init__(self, *args, **kwargs)
self.header = None
self.border_color = 'black'
self.border_width = 2
self.corner_radius = 6
def __init__(self, *args, **kwargs):
ui.View.__init__(self, *args, **kwargs)
self.panels = []
self.updates_running = False
self.make_view()
def display_toast(view, help_text, width = 220, height = 110, show_duration=2, fade_duration=0.5, background_colour=(.42, .42, .42), text_colour= (.96, .96, .96), corner_radius=10):
w, h = ui.get_screen_size()
help_view = ui.View(frame=((w/2)-(width/2),(h/2)-height, width, height))
help_view.background_color = background_colour
help_view.corner_radius = corner_radius
label = ui.Label()
label.text = help_text
label.flex = 'H'
label.width = help_view.width * 0.9
label.alignment = ui.ALIGN_CENTER
label.x = (help_view.width / 2) - (label.width / 2)
label.y = (help_view.height / 2) - (label.height / 2)
label.number_of_lines = 3
label.text_color = text_colour
help_view.add_subview(label)
def animation_fade_in():
help_view.alpha = 1.0
def animation_fade_out():
help_view.alpha = 0.0
help_view.alpha = 0.0
view.add_subview(help_view)
ui.animate(animation_fade_in, duration=fade_duration)
time.sleep(show_duration+fade_duration)
ui.animate(animation_fade_out, duration=fade_duration)
time.sleep(fade_duration)
view.remove_subview(help_view)
def __init__(self, *args, **kwargs):
ui.View.__init__(self, *args, **kwargs)
MKMapView = ObjCClass('MKMapView')
frame = CGRect(CGPoint(0, 0), CGSize(self.width, self.height))
self.mk_map_view = MKMapView.alloc().initWithFrame_(frame)
flex_width, flex_height = (1<<1), (1<<4)
self.mk_map_view.setAutoresizingMask_(flex_width|flex_height)
self_objc = ObjCInstance(self)
self_objc.addSubview_(self.mk_map_view)
self.mk_map_view.release()
def __init__(self):
# Init
self.views = []
self.curview = None
self.root = ui.View(name="Multipanel")
self.close = ui.Button()
self.close.name = "close"
self.close.enabled = False
self.close.image = ui.Image.named("ionicons-close-round-32")
self.close.action = self.close_tapped
self.root.add_subview(self.close)
self.close.frame = self.root.width - 32, 0, 32, 32
self.close.flex = "LB"
self.tabs = ui.SegmentedControl()
self.tabs.name = "tabs"
self.tabs.enabled = False
self.tabs.selected_index = -1
self.tabs.segments = [PLACEHOLDER_TEXT]
self.tabs.action = self.segment_changed
self.root.add_subview(self.tabs)
self.tabs.frame = 0, 0, self.root.width - self.close.width, self.tabs.height
self.tabs.flex = "WB"
self.placeholder = ui.View()
self.placeholder.background_color = "lightgray"
self.ph_label = ui.Label()
self.ph_label.font = ("<system-bold>", 24)
self.ph_label.text_color = "gray"
self.ph_label.text = "No View Selected"
self.placeholder.add_subview(self.ph_label)
self.ph_label.size_to_fit()
self.ph_label.center = self.placeholder.center
self.ph_label.flex = "TBLR"
self.update_view()