pythonuser-interfaceuitextfieldcommand-line-interfaceurwid

Is there an equivalent of a GUI text field in urwid?


I'm wondering if the python libary urwid includes an input option that behaves like a GUI text field.

By this I mean that,

Here is a simple example of Swing's JTextField:

enter image description here


Solution

  • The input text equivalent in urwid is the Edit widget, here is an example of how you can use it:

    #!/usr/bin/env python
    
    from __future__ import print_function, absolute_import, division
    import urwid
    
    
    def show_or_exit(key):
        if key in ('q', 'Q', 'esc'):
            raise urwid.ExitMainLoop()
    
    def name_changed(w, x):
        header.set_text('Hello % s!' % x)
    
    
    if __name__ == '__main__':
        name_edit = urwid.Edit("Name: ")
        header = urwid.Text('Fill your details')
        widget = urwid.Pile([
            urwid.Padding(header, 'center', width=('relative', 6)),
            name_edit,
            urwid.Edit('Address: '),
        ])
        urwid.connect_signal(name_edit, 'change', name_changed)
    
        widget = urwid.Filler(widget, 'top')
        loop = urwid.MainLoop(widget, unhandled_input=show_or_exit)
        loop.run()
    

    If you try it out, you can see that both fields remain editable and can be filled in any order.