[Urwid] [Fwd: Find focused Edit]

cantabile cantabile.03 at wanadoo.fr
Tue Jul 19 22:23:49 EDT 2005


Ian Ward a écrit :

> cantabile wrote:
>
> >Hi all,
> >
> >I'm trying end write a urwid based gui for server tools install.
> >I have several Edit's piled (with Pile() of course) in Columns.
> >Now, I'd like to display some help for each Edit in the frame footer.
> >I'm aware that get_focus() exists, but in that case, it gives back a
> >Column instead of the Edit.
> >How could I do to get the highlighted / focused Edit to display its
> >help, or the app. to retrieve the highlighted Edit when it changes ?
> >
> >Thanks for your help.
> >
>
> Columns and Pile objects also have get_focus() methods that you can 
> use to drill down to the Edit widget.  To make your code generic with 
> respect to the containing Columns and Pile widgets you could use a 
> "while hasattr( w, 'get_focus' ): w = w.get_focus()" loop to drill down.
>
> Ian
>
>
> _______________________________________________
> Urwid mailing list
> Urwid at lists.excess.org
> http://lists.excess.org/mailman/listinfo/urwid
>
>
Well, doesn't work. I think it may come from the way I'm using the widgets.
I wrap them in this class

class It:
    """ Wrapper class for widgets
   
        Items are stored as objects in order to get their
        value by using getval() below.
    """
    def __init__(self, title, value, help):
        self.widget = urwid.Edit(('editcp', title + " : "), value)
        self.help = help
        self.padd = urwid.Padding(urwid.AttrWrap(self.widget,     
'editbx','editfc'),
                                      ('fixed left',3), ('fixed right', 
1), MIDCOL)
    def get(self):
        return self.padd
       
    def getval(self):
        return self.widget.get_edit_text()

    def gettitle(self):
        return self.widget.caption

    def gethelp(self):
        return help

For example :

name = It(dict['name'][0], dict['name'][1], dict['name'][2])
context = ...

and so on

Then I write content like this :
CONTENT = [
 urwid.Columns([
    urwid.Divider(""),
    urwid.Columns( [
            urwid.Pile([ name.get(), blank, context.get() ]),
            urwid.Pile([ method.get(), blank, flags.get() ]),
        ], 5 ),

## lots of other edits piled in two columns
]

Then in run():
 while not keys:
                keys= self.ui.get_input()
                for k in keys:
                    if k == 'window resize':
                        size = self.ui.get_cols_rows()
                    #other tests cut here
                    else:
                        w = self.listbox.get_focus()
                        while hasattr(w, 'get_focus'):
                            w= w.get_focus()
                            # For testing purposes
                            # The idea would be to retrieve the It.gethelp
                            # message once the Edit is identified
                            help.settext(w.get_edit_text())
                    self.view.keypress(size, k)

What do you think ?

Another idea : is it possible that wrapping the edits in urwid.Padding 
and urwid.AttrWrap stops the loop before it reaches the urwid.Edit itself ?




More information about the Urwid mailing list