Discussion:
[urwid] switch focus from ListBox to Edit
c***@posteo.jp
2016-03-26 03:08:40 UTC
Permalink
How do I set the focus to an Edit?

In the example you see a urwid.Text and a urwid.ListBox.
When pressing 'N' the Text is replace with an urwid.Edit.

But this Edit doesn't have the focus, so I can not start to write in
that Edit. I found no "set_focus()" in the Edit class.
--
You received this message because you are subscribed to the Google Groups "Urwid Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to urwid+***@excess.org.
To post to this group, send email to ***@excess.org.
For more options, visit https://groups.google.com/a/excess.org/d/optout.
Robert Urban
2016-03-26 15:18:35 UTC
Permalink
try adding "frame.focus_position = 'header'" after "placeholder.original_widget
= edit"
Post by c***@posteo.jp
How do I set the focus to an Edit?
In the example you see a urwid.Text and a urwid.ListBox.
When pressing 'N' the Text is replace with an urwid.Edit.
But this Edit doesn't have the focus, so I can not start to write in
that Edit. I found no "set_focus()" in the Edit class.
--
You received this message because you are subscribed to the Google Groups "Urwid Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to urwid+***@excess.org.
To post to this group, send email to ***@excess.org.
For more options, visit https://groups.google.com/a/excess.org/d/optout.
c***@posteo.jp
2016-03-26 15:36:44 UTC
Permalink
Hi Robert,

thank you very much.
Post by Robert Urban
try adding "frame.focus_position = 'header'" after
"placeholder.original_widget = edit"
This helps for the example code. And I think I understand why. ;)

But it doesn't help me to understand the uwrid-focus-thing in a more
generalized way.

Why do I have to use a string ("header") here? Where in the docu can I
read about it? The referecen docu about 'focus_postion' doesn't help me
here.

What part of the documentation didn't I read here because I didn't knew?

How would I solve this, when frame is another type of container widget?
e.g. Columsn() or whatever.
--
You received this message because you are subscribed to the Google Groups "Urwid Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to urwid+***@excess.org.
To post to this group, send email to ***@excess.org.
For more options, visit https://groups.google.com/a/excess.org/d/optout.
Robert Urban
2016-03-26 16:22:19 UTC
Permalink
Hi c.buhtz,
Post by c***@posteo.jp
Hi Robert,
thank you very much.
you are welcome.
Post by c***@posteo.jp
Post by Robert Urban
try adding "frame.focus_position = 'header'" after
"placeholder.original_widget = edit"
This helps for the example code. And I think I understand why. ;)
But it doesn't help me to understand the uwrid-focus-thing in a more
generalized way.
Why do I have to use a string ("header") here? Where in the docu can I
read about it? The referecen docu about 'focus_postion' doesn't help me
here.
uh, it is there. If you go to:

http://urwid.org/reference/widget.html#frame
Post by c***@posteo.jp
focus_position
writeable property containing an indicator which part of the frame that is
in focus: ‘body’, ‘header’ or ‘footer’.
What part of the documentation didn't I read here because I didn't knew?
How would I solve this, when frame is another type of container widget?
e.g. Columsn() or whatever.
I think it's mostly documented, if perhaps slightly cryptic. For example in the
Post by c***@posteo.jp
focus_position
index of child widget in focus. Raises IndexError if read when Columns is
empty, or when set to an invalid index.
which tells you that focus_position is a writeable property (yes, it could be
explicitly mentioned) and can be set to an "index", which is the column-number
you wish to give focus:
col.focus_position = 1

cheers,
rob
--
You received this message because you are subscribed to the Google Groups "Urwid Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to urwid+***@excess.org.
To post to this group, send email to ***@excess.org.
For more options, visit https://groups.google.com/a/excess.org/d/optout.
Robert Nesius
2016-03-26 16:43:27 UTC
Permalink
widget focus management was something I struggled with too. I did explore
focus maps but they didn't work as expected for me. If anyone had tips to
share on those I'd like to hear them.

What I ultimately ended up doing is writing my own "meta-container" classes
that handled focus management for me. I inherited from an Urwid container
class and overrode the keypress method and handled tab/shift-tab myself.
For example I would have a "FormBody" whose child was a "FormController".
The "Form Controller" had two children, a FormBody and a NavBar. The
FormBody inherited from Urwid.ListBox, and NavBar inherited from
urwid.Columns (each column had a button). The FormBody widget new how to
iterate over the contents of the list box, and if it "tabbed off the end"
it would return the tab to the FormController. The FormController knew when
a tab returned to it unhandled that the FormBody was what had the focus, so
it would call the keypress method on the NavBar. One interesting edge-case
I ran into is that when the FormBody had no selectable widgets inside, the
ListBox itself was given the focus, so in that special case I had to set
the selectable attribute on the FormBody to false.

In summary: You can set the focus position of a container widget by
set_focus. You can see if a widget is selectable by querying the
selectable() method (or attribute, or something along those lines). You
can extend and augment Urwid classes by inheriting form them and overriding
the keypress method (the tutorial documentation covers this). You might
need "orchestration" classes that help pass the keypress events around. If
you figure out focus maps or find an easier/nicer way to achieve all of
this, I'd love to know about it. :)

I was part of the group that wrote the installer UI for Clear Linux. You
can download our installer image from
https://download.clearlinux.org/image/. /usr/bin/ister_gui.py is the
application we wrote (still a number of tweaks/enhancements to make) but
you can look it over and get the idea. I'm not saying it's pretty or
perfect but if you run the installer a few times and then look at the
source for the GUI you might get some ideas.

-Rob
Post by c***@posteo.jp
Hi Robert,
thank you very much.
Post by Robert Urban
try adding "frame.focus_position = 'header'" after
"placeholder.original_widget = edit"
This helps for the example code. And I think I understand why. ;)
But it doesn't help me to understand the uwrid-focus-thing in a more
generalized way.
Why do I have to use a string ("header") here? Where in the docu can I
read about it? The referecen docu about 'focus_postion' doesn't help me
here.
What part of the documentation didn't I read here because I didn't knew?
How would I solve this, when frame is another type of container widget?
e.g. Columsn() or whatever.
--
You received this message because you are subscribed to the Google Groups
"Urwid Users" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/a/excess.org/d/optout.
--
You received this message because you are subscribed to the Google Groups "Urwid Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to urwid+***@excess.org.
To post to this group, send email to ***@excess.org.
For more options, visit https://groups.google.com/a/excess.org/d/optout.
c***@posteo.jp
2016-03-27 02:59:07 UTC
Permalink
Post by Robert Urban
try adding "frame.focus_position = 'header'" after
"placeholder.original_widget = edit"
My first example code worked fine with that solution. But bringing this
into my productive code failed.
So I updated my example (see attachment) with the widget structure of
my productive code to show you my problem. Setting focus doesn't work
here. This is the structure scheme:

Frame
|-(header)
|-Pile
|-Text
|-Columns
|-Text
|-Text or Edit (switching with 'N')
|-(body)
|-ListBox
|-some Text items
--
You received this message because you are subscribed to the Google Groups "Urwid Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to urwid+***@excess.org.
To post to this group, send email to ***@excess.org.
For more options, visit https://groups.google.com/a/excess.org/d/optout.
Robert Urban
2016-03-27 22:28:28 UTC
Permalink
Hello c.buhtz,

I think your script should behave as expected, but it doesn't. I think urwid is
caching the non-selectability of your Text() widget. When you swap it out for an
Edit() widget, urwid refuses to set the focus of the Columns() to position 1.

Might be a bug...

You can work around this by using an Edit() widget from the beginning.

cheers,
rob
Post by c***@posteo.jp
Post by Robert Urban
try adding "frame.focus_position = 'header'" after
"placeholder.original_widget = edit"
My first example code worked fine with that solution. But bringing this
into my productive code failed.
So I updated my example (see attachment) with the widget structure of
my productive code to show you my problem. Setting focus doesn't work
Frame
|-(header)
|-Pile
|-Text
|-Columns
|-Text
|-Text or Edit (switching with 'N')
|-(body)
|-ListBox
|-some Text items
--
You received this message because you are subscribed to the Google Groups "Urwid Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to urwid+***@excess.org.
To post to this group, send email to ***@excess.org.
For more options, visit https://groups.google.com/a/excess.org/d/optout.
Loading...