def __init__(self, got_focus=None):
urwid.Edit.__init__(self)
self.history = deque(maxlen=1000)
self._history_index = -1
self._got_focus = got_focus
python类Edit()的实例源码
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 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 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 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))
def __init__(self, got_focus=None):
urwid.Edit.__init__(self)
self.history = deque(maxlen=1000)
self._history_index = -1
self._got_focus = got_focus