我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用urwid.Edit()。
def reply_box(self, button, thr_id): subject = ur.Edit(("blue", "Subject: "), wrap="clip") name = ur.Edit(("blue", "Name: "), "Anonymous", wrap="clip") if thr_id == -1: text = ur.Edit(multiline=True) else: text = ur.Edit(edit_text=">>" + thr_id, multiline=True) post_btn = ur.AttrMap(ur.Button( "Post", self.add_post, (subject, name, text, thr_id)), "green", "b_green") box = ur.LineBox(ur.ListBox(ur.SimpleFocusListWalker( [name, subject, ur.LineBox(text, "Post text"), self.parent.div, post_btn]))) self.loop.Widget = MyOverlay( box, self.loop.baseWidget, "center", ("relative", 100), "top", ("relative", 100), None, None, self.parent.margin, self.parent.margin, int(self.loop.dimensions[1] * 0.3), int(self.loop.dimensions[1] * 0.3), self.parent)
def init_main_screen(self): self.w_filter_edit = urwid.Edit('Filter ') aw_filter_edit = urwid.AttrMap(self.w_filter_edit, 'edit', 'edit_focus') self.w_status_line = urwid.AttrMap(StatusLine(self.store.get_test_stats), 'statusline', '') urwid.connect_signal(self.w_filter_edit, 'change', self.on_filter_change) self.init_test_listbox() self.w_main = urwid.Padding( urwid.Pile([ ('pack', urwid.Text(u'Python Urwid Test Runner', align='center')), ('pack', urwid.Divider()), ('pack', aw_filter_edit), ('pack', urwid.Divider()), self.w_test_listbox, ('pack', urwid.Divider()), ('pack', self.w_status_line), ]), left=2, right=2 )
def __init__(self, Telegram_ui): self.Telegram_ui = Telegram_ui self.updateLockedauto = False self.username_list = [] self.buffer_writing_text = {} # handeling navigation in history self.history_own_message = {} self.history_pos = 0 self.cur_text = "" self.status_bar = urwid.Text(('status_bar', ' '), align='left') self.attr = urwid.AttrMap(self.status_bar, 'status_bar') self.widgetEdit = urwid.Edit(TEXT_CAPTION, "", multiline=False) self.pile = urwid.Pile([self.attr, self.widgetEdit]) super().__init__(self.pile) self.update_send_widget()
def setUp(self): self.size = (80, 20) self.content = [ urwid.ListBox(urwid.SimpleFocusListWalker([ urwid.Edit('Field 1'), urwid.Edit('Field 2'), ])), urwid.ListBox(urwid.SimpleFocusListWalker([ SelectableText('Row 1'), urwid.Edit('', 'Edit field 2'), SelectableText('Row 3'), ])) ] self.editbox = self.content[1].body[1] self.editbox.edit_pos = 0 self.tabs = Tabs((urwid.Text('Edit fields'), self.content[0]), (urwid.Text('empty'),), (urwid.Text('Text rows'), self.content[1]))
def test_focusable_widget_gets_keypress_only_if_visible(self): w = Scrollable( urwid.Pile([urwid.Text('t1'), urwid.Text('t2'), urwid.Edit('', 'e3')]) ) size = (5, 2) def press_keys(): for key in ('backspace', 'backspace', 'f', 'o', 'o'): w.keypress(size, key) self.check(w, size, text=('t1'.ljust(size[0]), 't2'.ljust(size[0]))) press_keys() self.check(w, size, text=('t1'.ljust(size[0]), 't2'.ljust(size[0]))) w.set_scrollpos(1) self.check(w, size, text=('t2'.ljust(size[0]), 'e3'.ljust(size[0]))) press_keys() self.check(w, size, text=('t2'.ljust(size[0]), 'foo'.ljust(size[0])))
def test_abort_chain_with_builtin_key(self): self.km.bind('alt-1 alt-2 alt-3', 'foo') self.widget.keypress((80,), 'alt-1') self.assert_status(keys_given=('alt-1',), widget_text='Original Text', active_keychains=((('alt-1', 'alt-2', 'alt-3'), 'foo'),)) # This would usually append 'x' to the Edit widget text, but we want it # to abort the started chain instead. self.widget.keypress((80,), 'x') self.assert_status(keys_given=(), widget_text='Original Text', active_keychains=()) # Now we can change the text again self.widget.keypress((80,), 'x') self.assert_status(keys_given=(), widget_text='Original Textx', active_keychains=())
def keypress(self, size, key): if key=='enter': line=self.edit_text.strip() if line: urwid.emit_signal(self,'line_entered', line) self.history.append(line) self._history_index=len(self.history) self.edit_text=u'' if key=='up': self._history_index-=1 if self._history_index< 0: self._history_index= 0 else: self.edit_text=self.history[self._history_index] if key=='down': self._history_index+=1 if self._history_index>=len(self.history): self._history_index=len(self.history) self.edit_text=u'' else: self.edit_text=self.history[self._history_index] else: urwid.Edit.keypress(self, size, key)
def keypress(self, size, key): keyl = key.lower() if key == "enter": app.remove_overlays() self.callback(self.value(), *self.args) elif keyl in ("q", "esc", "ctrl g", "ctrl c"): app.remove_overlays() elif keyl in ("down", "ctrl n", "n", "j"): self.incr("down") elif keyl in ("up", "ctrl p", "p", "k"): self.incr("up") else: # dont use super because we want to allow zeros in this box urwid.Edit.keypress(self, (size[0],), key)
def keypress(self, size, key): if key == 'enter': line = self.edit_text.strip() if line: urwid.emit_signal(self, 'line_entered', line) self.history.append(line) self._history_index = len(self.history) self.edit_text = u'' if key == 'up': self._history_index -= 1 if self._history_index < 0: self._history_index = 0 else: self.edit_text = self.history[self._history_index] if key == 'down': self._history_index += 1 if self._history_index >= len(self.history): self._history_index = len(self.history) self.edit_text = u'' else: self.edit_text = self.history[self._history_index] else: urwid.Edit.keypress(self, size, key)
def read_next_line(self): """Read another line from the file.""" next_line = self.file.readline() if not next_line or next_line[-1:] != '\n': # no newline on last line of file self.file = None else: # trim newline characters next_line = next_line[:-1] expanded = next_line.expandtabs() edit = urwid.Edit("", expanded, allow_tab=True) edit.set_edit_pos(0) edit.original_text = next_line self.lines.append(edit) return next_line
def build_widgets(): input1 = urwid.Edit('What is your name? ') input2 = urwid.Edit('What is your quest? ') input3 = urwid.Edit('What is the capital of Assyria? ') inputs = [input1, input2, input3] def update_clock(widget_ref): widget = widget_ref() if not widget: # widget is dead; the main loop must've been destroyed return widget.set_text(datetime.now().isoformat()) # Schedule us to update the clock again in one second loop.call_later(1, update_clock, widget_ref) clock = urwid.Text('') update_clock(weakref.ref(clock)) return urwid.Filler(urwid.Pile([clock] + inputs), 'top')
def test2_cursor(self): T, E = urwid.Text, urwid.Edit l1 = [T(""),T(""),T("\n"),E("","\n\nX"),T("\n"),T(""),T("")] l2 = [T(""),T(""),T("\n"),E("","YY\n\n"),T("\n"),T(""),T("")] l2[3].set_edit_pos(2) self.cvtest( "plain cursor in view", l1, 3, 1, (0,1), 1, (1,3) ) self.cvtest( "cursor off top", l2, 3, 0, (1,3), 0, (2, 0) ) self.cvtest( "cursor further off top", l2, 3, 0, (2,3), 0, (2, 0) ) self.cvtest( "cursor off bottom", l1, 3, 3, (0,1), 2, (1, 4) ) self.cvtest( "cursor way off bottom", l1, 3, 100, (0,1), 2, (1, 4) )
def test_move_cursor(self): e, s, x = urwid.Edit("",""),SelectableText(""), urwid.Text("") self.mctest("nothing selectbl",[x,x,x],1,(20,),9,0,False,0,None) self.mctest("dead on",[x,s,x],1,(20,),9,0,True,1,9) self.mctest("l edge",[x,s,x],1,(20,),6,0,True,1,6) self.mctest("r edge",[x,s,x],1,(20,),13,0,True,1,13) self.mctest("l off",[x,s,x],1,(20,),2,0,True,1,2) self.mctest("r off",[x,s,x],1,(20,),17,0,True,1,17) self.mctest("l off 2",[x,x,s],1,(20,),2,0,True,2,2) self.mctest("r off 2",[s,x,x],1,(20,),17,0,True,0,17) self.mctest("l between",[s,s,x],1,(20,),6,0,True,0,6) self.mctest("r between",[x,s,s],1,(20,),13,0,True,1,13) self.mctest("l between 2l",[s,s,x],2,(22,),6,0,True,0,6) self.mctest("r between 2l",[x,s,s],2,(22,),14,0,True,1,14) self.mctest("l between 2r",[s,s,x],2,(22,),7,0,True,1,7) self.mctest("r between 2r",[x,s,s],2,(22,),15,0,True,2,15) # unfortunate pref_col shifting self.mctest("l e edge",[x,e,x],1,(20,),6,0,True,1,7) self.mctest("r e edge",[x,e,x],1,(20,),13,0,True,1,12) # 'left'/'right' special cases self.mctest("right", [e, e, e], 0, (12,), 'right', 0, True, 2, 'right') self.mctest("left", [e, e, e], 0, (12,), 'left', 0, True, 0, 'left')
def test_focus_path(self): # big tree of containers t = urwid.Text(u'x') e = urwid.Edit(u'?') c = urwid.Columns([t, e, t, t]) p = urwid.Pile([t, t, c, t]) a = urwid.AttrMap(p, 'gets ignored') s = urwid.SolidFill(u'/') o = urwid.Overlay(e, s, 'center', 'pack', 'middle', 'pack') lb = urwid.ListBox(urwid.SimpleFocusListWalker([t, a, o, t])) lb.focus_position = 1 g = urwid.GridFlow([t, t, t, t, e, t], 10, 0, 0, 'left') g.focus_position = 4 f = urwid.Frame(lb, header=t, footer=g) self.assertEqual(f.get_focus_path(), ['body', 1, 2, 1]) f.set_focus_path(['footer']) # same as f.focus_position = 'footer' self.assertEqual(f.get_focus_path(), ['footer', 4]) f.set_focus_path(['body', 1, 2, 2]) self.assertEqual(f.get_focus_path(), ['body', 1, 2, 2]) self.assertRaises(IndexError, lambda: f.set_focus_path([0, 1, 2])) self.assertRaises(IndexError, lambda: f.set_focus_path(['body', 2, 2])) f.set_focus_path(['body', 2]) # focus the overlay self.assertEqual(f.get_focus_path(), ['body', 2, 1])
def keypress(self, size, key): if key == 'enter': urwid.emit_signal(self, 'done', self.get_edit_text()) return elif key == 'esc': urwid.emit_signal(self, 'done', None) return elif key == 'tab': urwid.emit_signal(self, 'toggle_case_modifier') urwid.emit_signal(self, 'change', self, self.get_edit_text()) return elif key == 'ctrl r': urwid.emit_signal(self, 'toggle_regexp_modifier') urwid.emit_signal(self, 'change', self, self.get_edit_text()) return elif key == 'down': urwid.emit_signal(self, 'done', None) return urwid.Edit.keypress(self, size, key)
def __init__(self, *args, **kwargs): self.start = urwid.Edit('Start: ') self.end = urwid.Edit('End: ') self.server = kwargs["server"] self.calendar = kwargs["calendar"] del(kwargs['server']) del(kwargs["calendar"]) print (self.server, self.calendar) div = urwid.Divider() self.msg = urwid.Edit(caption="Event message: ", edit_text='', multiline=True) button_save = urwid.Button('save') urwid.connect_signal(button_save, 'click', self.on_save) self.pile = urwid.Pile([self.start, self.end, div, self.msg, div, button_save]) super(EventWidget, self).__init__(self.pile, *args, **kwargs)
def __init__(self, pubpen): self.pubpen = pubpen username_label = urwid.Text('Username: ', align='right') password_label = urwid.Text('Password: ', align='right') self.username = urwid.Edit() self.password = urwid.Edit() login_button = urwid.Button('Login') decorated_login_button = urwid.AttrMap(login_button, None, focus_map='reversed') quit_button = urwid.Button('Quit') decorated_quit_button = urwid.AttrMap(quit_button, None, focus_map='reversed') buttons = urwid.Columns(( (len('Login') + 4, decorated_login_button), (len('Quit') + 4, decorated_quit_button), ), focus_column=1) labels = urwid.Pile([username_label, password_label]) self.fields = urwid.Pile([self.username, self.password, buttons], focus_item=0) entry_box = urwid.Columns([labels, self.fields]) self.status_message = urwid.Text(' ', align='center') entry_with_status = urwid.Pile([entry_box, self.status_message]) padded_entry_with_status = urwid.Padding(entry_with_status, align='center') self.display = urwid.Filler(padded_entry_with_status, valign='middle') decorate = urwid.LineBox(self.display) super().__init__(decorate) self.focusable_widgets = (w for w in itertools.cycle(( ((self.fields, 1),), ((self.fields, 2), (buttons, 0)), ((self.fields, 2), (buttons, 1)), ((self.fields, 0),) ))) urwid.connect_signal(login_button, 'click', self.attempt_login) urwid.connect_signal(quit_button, 'click', self.quit) self.pubpen.subscribe('user.login_success', self.handle_login_success) self.pubpen.subscribe('user.login_failure', self.handle_login_failure)
def __init__(self, callback, content=""): self.callback = callback self.w_edit = urwid.Edit(edit_text=content) self.w_lb = urwid.LineBox(self.w_edit) self.__super.__init__(self.w_lb)
def __init__(self, caption=u"", edit_text=u"", multiline=False, align=urwid.LEFT, wrap=urwid.SPACE, allow_tab=False, edit_pos=None, layout=None, mask=None): self.overlays = ["Hi, my name is ", TextOverlay("<@!41378434659029>", "@SlimShady", "message_mention")] self.cur_overlay = len(self.overlays) - 1 urwid.Edit.__init__(self, "") self.edit_text = self.get_text()[0] self.set_edit_pos(len(edit_text))
def keypress(self, size, key): if key == "backspace": self.delete_char() else: return urwid.Edit.keypress(self, size, key) return None
def __init__(self, discord_client, chat_widget): self.discord = discord_client self.ui = self.discord.ui self.chat_widget = chat_widget self.editing = None self.edit = urwid.Edit(multiline=True, allow_tab=True) self.edit.keypress = self._edit_keypress self.w_lb = urwid.LineBox(urwid.Padding(self.edit, left=1, right=1)) self.w_text = urwid.Text("") self.w_typing = TypingList(self) self.pile = urwid.Pile([]) self.hide_channel_selector() self.__super.__init__(self.pile)
def _edit_keypress(self, size, key): if key == "enter": return key key = urwid.Edit.keypress(self.edit, size, key) if key is None: if self.editing is None: self.discord.async( self.discord.send_typing(self.chat_widget.send_channel)) return key
def test_moving_focus_up_down(self): w = Scrollable( urwid.Pile([urwid.Text('t1'), urwid.Text('t2'), urwid.Edit('', 'e3'), urwid.Text('t4'), urwid.Text('t5'), urwid.Edit('', 'e6'), urwid.Text('t7'), urwid.Text('t8')]) ) size = (10, 2) self.check(w, size, text=('t1'.ljust(size[0]), 't2'.ljust(size[0]))) w.keypress(size, 'down') self.check(w, size, text=('t2'.ljust(size[0]), 'e3'.ljust(size[0]))) w.keypress(size, 'down') self.check(w, size, text=('t5'.ljust(size[0]), 'e6'.ljust(size[0]))) w.keypress(size, 'down') self.check(w, size, text=('e6'.ljust(size[0]), 't7'.ljust(size[0]))) w.keypress(size, 'down') self.check(w, size, text=('t7'.ljust(size[0]), 't8'.ljust(size[0])))
def test_mouse_event(self): scrl = Scrollable( urwid.Pile([urwid.Text('t1'), urwid.Text('t2'), urwid.Edit('', 'eXXX'), urwid.Text('t3'), urwid.Edit('', 'eYYY'), urwid.Text('t4'), urwid.Text('t5')]) ) sb = ScrollBar(scrl, thumb_char='#', trough_char='|') size = (10, 5) self.check(sb, size, cursor_pos=(4, 2), text=('t1'.ljust(size[0]-1) + '#', 't2'.ljust(size[0]-1) + '#', 'eXXX'.ljust(size[0]-1) + '#', 't3'.ljust(size[0]-1) + '#', 'eYYY'.ljust(size[0]-1) + '|')) sb.mouse_event(size, 'mouse press', button=1, col=1, row=4, focus=True) self.check(sb, size, cursor_pos=(1, 4), text=('t1'.ljust(size[0]-1) + '#', 't2'.ljust(size[0]-1) + '#', 'eXXX'.ljust(size[0]-1) + '#', 't3'.ljust(size[0]-1) + '#', 'eYYY'.ljust(size[0]-1) + '|')) scrl.set_scrollpos(2) self.check(sb, size, cursor_pos=(1, 2), text=('eXXX'.ljust(size[0]-1) + '|', 't3'.ljust(size[0]-1) + '#', 'eYYY'.ljust(size[0]-1) + '#', 't4'.ljust(size[0]-1) + '#', 't5'.ljust(size[0]-1) + '#')) sb.mouse_event(size, 'mouse press', button=1, col=3, row=0, focus=True) self.check(sb, size, cursor_pos=(3, 0), text=('eXXX'.ljust(size[0]-1) + '|', 't3'.ljust(size[0]-1) + '#', 'eYYY'.ljust(size[0]-1) + '#', 't4'.ljust(size[0]-1) + '#', 't5'.ljust(size[0]-1) + '#'))
def test_builtin_keys_have_precedence_over_keychain_if_no_keychain_started(self): self.km.bind('z', 'impossible action', context='list2') # This should go to an Edit widget self.mainw.focus_position = 1 # Focus list2 self.press_key('z') self.assert_current_keychain() self.assert_action(exp_action=None, exp_count=0, exp_widget=None) self.assert_active_keychains() self.assertEqual(self.listw2.focus.edit_text, 'z')
def __init__(self, got_focus=None): urwid.Edit.__init__(self) self.history=deque(maxlen=1000) self._history_index=-1 self._got_focus=got_focus
def keypress(self, size, key): if key == 'enter': urwid.emit_signal(self, 'done', self.get_edit_text()) return elif key == 'esc': urwid.emit_signal(self, 'done', None) return urwid.Edit.keypress(self, size, key)
def __init__(self, got_focus=None): urwid.Edit.__init__(self) self.history = deque(maxlen=1000) self._history_index = -1 self._got_focus = got_focus
def keypress(self, size, key): if key == 'enter': # if you dont need a reference to the CustomEdit instance you can drop the 3rd argument self.on_finish(self) return elif key == 'esc': super(CustomEdit, self).set_edit_text('') self.on_finish(self) return return urwid.Edit.keypress(self, size, key)
def question(): return urwid.Pile([urwid.Edit(('I say', u"What is your name?\n"))])
def create_edit(self, label, text, fn): w = urwid.Edit(label, text) urwid.connect_signal(w, 'change', fn) fn(w, text) w = urwid.AttrWrap(w, 'edit') return w
def split_focus(self): """Divide the focus edit widget at the cursor location.""" focus = self.lines[self.focus] pos = focus.edit_pos edit = urwid.Edit("",focus.edit_text[pos:], allow_tab=True) edit.original_text = "" focus.set_edit_text(focus.edit_text[:pos]) edit.set_edit_pos(0) self.lines.insert(self.focus+1, edit)
def __init__(self, op, letter): """Use the operator and letter of the child column as caption op -- operator or None letter -- letter of child column remove_fn -- function to call when user wants to remove child function takes no parameters """ urwid.Edit.__init__(self, layout=CALC_LAYOUT) self.op = op self.set_letter( letter )
def test4cursor(self): T,E = urwid.Text, urwid.Edit #...
def test5set_focus_valign(self): T,E = urwid.Text, urwid.Edit lbox = urwid.ListBox(urwid.SimpleFocusListWalker([ T(''), T('')])) lbox.set_focus_valign('middle') # TODO: actually test the result
def test3_shift(self): T,E = urwid.Text, urwid.Edit self.ltest( "shift up one fit", [T("1\n2"),T("3"),T("4"),T("5"),T("6")], 4, 5, ["2 ","3 ","4 ","5 ","6 "],None) e = E("","ab\nc",1) e.set_edit_pos( 2 ) self.ltest( "shift down one cursor over edge", [e,T("3"),T("4"),T("5\n6")], 0, -1, ["ab ","c ","3 ","4 ","5 "], (2,0)) self.ltest( "shift up one cursor over edge", [T("1\n2"),T("3"),T("4"),E("","d\ne")], 3, 4, ["2 ","3 ","4 ","d ","e "], (1,4)) self.ltest( "shift none cursor top focus over edge", [E("","ab\n"),T("3"),T("4"),T("5\n6")], 0, -1, [" ","3 ","4 ","5 ","6 "], (0,0)) e = E("","abc\nd") e.set_edit_pos( 3 ) self.ltest( "shift none cursor bottom focus over edge", [T("1\n2"),T("3"),T("4"),e], 3, 4, ["1 ","2 ","3 ","4 ","abc "], (3,4))
def test4_really_large_contents(self): T,E = urwid.Text, urwid.Edit self.ltest("really large edit", [T(u"hello"*100)], 0, 0, ["hell","ohel","lohe","lloh","ello"], None) self.ltest("really large edit", [E(u"", u"hello"*100)], 0, 0, ["hell","ohel","lohe","lloh","llo "], (3,4))
def test_change_focus_with_mouse(self): p = urwid.Pile([urwid.Edit(), urwid.Edit()]) self.assertEqual(p.focus_position, 0) p.mouse_event((10,), 'button press', 1, 1, 1, True) self.assertEqual(p.focus_position, 1)
def test_box_column(self): c = urwid.Columns([urwid.Filler(urwid.Edit()),urwid.Text('')], box_columns=[0]) c.keypress((10,), 'x') c.get_cursor_coords((10,)) c.move_cursor_to_coords((10,), 0, 0) c.mouse_event((10,), 'foo', 1, 0, 0, True) c.get_pref_col((10,))
def test_get_cursor_coords(self): self.assertEqual(urwid.Overlay(urwid.Filler(urwid.Edit()), urwid.SolidFill(u'B'), 'right', 1, 'bottom', 1).get_cursor_coords((2,2)), (1,1))
def test1_SpaceWrap(self): w = urwid.Edit("","blah blah") w.set_edit_pos(0) self.rtest(w,["blah","blah"],(0,0)) w.set_edit_pos(4) self.rtest(w,["lah ","blah"],(3,0)) w.set_edit_pos(5) self.rtest(w,["blah","blah"],(0,1)) w.set_edit_pos(9) self.rtest(w,["blah","lah "],(3,1))
def test2_ClipWrap(self): w = urwid.Edit("","blah\nblargh",1) w.set_wrap_mode('clip') w.set_edit_pos(0) self.rtest(w,["blah","blar"],(0,0)) w.set_edit_pos(10) self.rtest(w,["blah","argh"],(3,1)) w.set_align_mode('right') w.set_edit_pos(6) self.rtest(w,["blah","larg"],(0,1))
def test3_AnyWrap(self): w = urwid.Edit("","blah blah") w.set_wrap_mode('any') self.rtest(w,["blah"," bla","h "],(1,2))
def test4_CursorNudge(self): w = urwid.Edit("","hi",align='right') w.keypress((4,),'end') self.rtest(w,[" hi "],(3,0)) w.keypress((4,),'left') self.rtest(w,[" hi"],(3,0))