我们从Python开源项目中,提取了以下3个代码示例,用于说明如何使用ui.in_background()。
def init_size(self): # initialize with correct size when landscape orientation = ui.WebView(frame=(0,0,100,200)).eval_js('window.orientation') if orientation in (-90, 90): self.frame = (0, 0, self.height, self.width) def did_load(self): self.init_buttons() self.init_webbrowser() self.init_addressbar() self.init_size() self.flex = 'WH' self.bookmarks = self.load_bookmarks() self.history = self.load_history() self.addressbar_is_editing = False self.webpage_has_loaded = False self.favourite_images = {True :ui.Image.named('ionicons-ios7-star-32'), False:ui.Image.named('ionicons-ios7-star-outline-32')} def save_history(self, filename=filename_history): with open(filename, 'w') as f: url = self.get_url() if url in self.history: self.history.remove(url) self.history.append(url) f.seek(0) pickle.dump(self.history, f) def clear_history(self, sender, filename=filename_history): with open(filename, 'w') as f: self.history = [] f.seek(0) pickle.dump(self.history, f) sender.superview.superview['history'].data_source.items = self.history sender.superview.superview['history'].reload() def save_bookmark(self, filename=filename_bookmarks): with open(filename, 'w') as f: url = self.get_url() title = self.get_title() or self.parse_url(url) self.bookmarks[title] = url f.seek(0) json.dump(self.bookmarks, f, indent=4) self['controlpanel']['favourite'].image = self.favourite_images[True] def remove_bookmark(self, title=None, filename=filename_bookmarks): with open(filename, 'w') as f: title = title or self.get_title() del self.bookmarks[title] f.seek(0) json.dump(self.bookmarks, f, indent=4) self['controlpanel']['favourite'].image = self.favourite_images[False] def popup_menu(self): popup = ui.View(name='menu', frame=(0, 0, 320, 500)) toolbar = ui.View(frame=(-5, 0, 330, 100), name='toolbar') toolbar.border_width = 0.5 toolbar.border_color = '#B2B2B2' label = ui.Label() label.text = 'Bookmarks' label.alignment = ui.ALIGN_CENTER label.frame = (0, 0, 320, 50) label.name = 'title' segment_ctrl = ui.SegmentedControl(name='segctrl') segment_ctrl.segments = ['Bookmarks', 'History'] segment_ctrl.width = 170 segment_ctrl.center = popup.center segment_ctrl.y = label.height segment_ctrl.selected_index = 0 segment_ctrl.action = self.bookmarks_or_history button = ui.Button() button.frame = (segment_ctrl.x*3.5, segment_ctrl.y, 60, 30) button.font = ('<system>', 15) button.title= 'Clear' button.name = 'clear' button.action = self.clear_history button.hidden = True toolbar.add_subview(label) toolbar.add_subview(segment_ctrl) toolbar.add_subview(button) popup.add_subview(toolbar) data_source = ui.ListDataSource(sorted(self.bookmarks.keys())) popup.add_subview(self.list_bookmarks_and_history(data_source, width=320,height=toolbar.superview.height-toolbar.height, y=toolbar.height, name='bookmarks')) x, y = self['controlpanel']['bookmarks'].center popup.present('popover', popover_location=(x, y), hide_title_bar=True) def bookmarks_or_history(self, sender): toolbar = sender.superview if sender.selected_index == 0: toolbar['clear'].hidden = True toolbar['title'].text = 'Bookmarks' data_source = ui.ListDataSource(sorted(self.bookmarks.keys())) tv = self.list_bookmarks_and_history(data_source, width=320, height=toolbar.superview.height-toolbar.height, y=toolbar.height, name='bookmarks') toolbar.superview.remove_subview(toolbar.superview['history']) else: toolbar['clear'].hidden = False toolbar['title'].text = 'History' data_source = ui.ListDataSource(self.history[::-1]) tv = self.list_bookmarks_and_history(data_source, width=320, height=toolbar.superview.height-toolbar.height, y=toolbar.height, name='history') toolbar.superview['bookmarks'].hidden=True toolbar.superview.remove_subview(toolbar.superview['bookmarks']) sender.superview.superview.add_subview(tv) def list_bookmarks_and_history(self, data_source, **kwargs): tv = ui.TableView() tv.data_source = data_source tv.delegate = self for k, v in kwargs.items(): setattr(tv, k, v) return tv def show_more_menu(self): popup = ui.TableView() popup.width = 250 popup.height = 500 popup.name = 'More' popup.data_source = popup.delegate = self button = self['controlpanel']['more'] popup.present('popover', popover_location=(button.x, button.y+button.height)) def button_tapped(self, sender): if sender.name == 'favourite': if self.get_url() in self.bookmarks.values(): self.remove_bookmark() else: self.save_bookmark() elif sender.name == 'bookmarks': self.popup_menu() elif sender.name == 'more': self.show_more_menu() else: eval("self['webview'].{}()".format(sender.name)) def tableview_number_of_rows(self, tableview, section): if tableview.name == 'Bookmarks': return len(self.bookmarks) elif tableview.name == 'More': return 1 def tableview_cell_for_row(self, tableview, section, row): if tableview.name == 'Bookmarks': cell = ui.TableViewCell() cell.text_label.text = sorted(self.bookmarks.keys())[row] cell.image_view.image = ui.Image.named('ionicons-ios7-bookmarks-outline-32') cell.image_view.tint_color = '#66CCFF' return cell elif tableview.name == 'More': cell = ui.TableViewCell() cell.text_label.text = 'Settings' cell.image_view.image = ui.Image.named('ionicons-wrench-32') return cell @ui.in_background def tableview_did_select(self, tableview, section, row): if tableview.name == 'bookmarks': url = self.bookmarks[sorted(self.bookmarks.keys())[row]] self.load_url(url) tableview.superview.close() elif tableview.name == 'history': url = tableview.data_source.items[row] tableview.superview.close() self.load_url(url) elif tableview.name == 'More': tableview.close() console.hud_alert('No settings yet...', 'error', 1) def tableview_can_delete(self, tableview, section, row): return True def tableview_delete(self, tableview, section, row): item = sorted(self.bookmarks.keys())[row] self.remove_bookmark(item) tableview.reload() def textfield_did_begin_editing(self, textfield): self.addressbar_is_editing = True self.set_url() self['controlpanel']['reload'].hidden = True def textfield_did_end_editing(self, textfield): self.addressbar_is_editing = False self['controlpanel']['reload'].hidden = False self.set_url() def textfield_should_return(self, textfield): url = self['controlpanel']['addressbar'].text self.load_url(url) textfield.end_editing() return True def webview_did_start_load(self, webview): self.webpage_has_loaded = False def webview_did_finish_load(self, webview): if not self.addressbar_is_editing: self.set_url() self.webpage_has_loaded = True page_is_bookmarked = unicode(self.get_url()) in self.bookmarks.values() self['controlpanel']['favourite'].image = self.favourite_images[page_is_bookmarked] self.save_history()
def getFile(setter=None,base_dir='.'): fv = FileViewer(setter,base_dir) fv.height=700 nv = ui.NavigationView(fv) def openDocuments(sender,path): def setme(fv,value): # set and bubble up setters fv.src.sel[0]=value if fv.src.setter is not None: fv.src.setter(value) newfv = FileViewer(setter=lambda value:setme(fv,value),base_dir=path) nv.push_view(newfv) nv.right_button_items=[ ui.ButtonItem(title='Documents', action=lambda sender:openDocuments(sender,os.path.expanduser('~/Documents'))), ui.ButtonItem(title='Library', action=lambda sender:openDocuments(sender,os.path.split(os.__file__)[0]))] nv.height=800 nv.width=500 nv.name = 'File Selector' nv.present('popover') ui.in_background(nv.wait_modal) nv.wait_modal() return fv.src.sel[0]
def printLog(): ### Pull the contents back into a string and close the stream global log_capture_string log_contents = log_capture_string.getvalue() log_capture_string.close() log_capture_string = StringIO() logger.handlers[0].stream=log_capture_string print(log_contents.lower()) ## run a runction in an async thread. better than ui.in_background for this application, because it is not queued up