diff options
author | Andrew Dunai <andunai@gmail.com> | 2018-01-03 16:50:30 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-01-03 16:50:30 +0200 |
commit | 2920475c0068d40cadd31d58650d9cfd7a1c2394 (patch) | |
tree | 77500d96f2a010ebec9fcf4e804a88ae6ddcfeca | |
parent | 6f94735e10ad10f5c750566382be97c4c9c11738 (diff) | |
parent | 2dcc541c0130d3ed3854c60ff30381fcc3406349 (diff) | |
download | urwid-2920475c0068d40cadd31d58650d9cfd7a1c2394.tar.gz |
Merge pull request #214 from abadger/fix-edit-change-signal
Emit postchange after text is updated
-rw-r--r-- | urwid/widget.py | 20 | ||||
-rwxr-xr-x | urwid/wimp.py | 11 |
2 files changed, 24 insertions, 7 deletions
diff --git a/urwid/widget.py b/urwid/widget.py index 9a732e7..98c1905 100644 --- a/urwid/widget.py +++ b/urwid/widget.py @@ -1101,11 +1101,22 @@ class Edit(Text): deletion. A caption may prefix the editing area. Uses text class for text layout. - Users of this class to listen for ``"change"`` events - sent when the value of edit_text changes. See :func:``connect_signal``. + Users of this class may listen for ``"change"`` or ``"postchange"`` + events. See :func:``connect_signal``. + + * ``"change"`` is sent just before the value of edit_text changes. + It receives the new text as an argument. Note that ``"change"`` cannot + change the text in question as edit_text changes the text afterwards. + * ``"postchange"`` is sent after the value of edit_text changes. + It receives the old value of the text as an argument and thus is + appropriate for changing the text. It is possible for a ``"postchange"`` + event handler to get into a loop of changing the text and then being + called when the event is re-emitted. It is up to the event + handler to guard against this case (for instance, by not changing the + text if it is signaled for for text that it has already changed once). """ # (this variable is picked up by the MetaSignals metaclass) - signals = ["change"] + signals = ["change", "postchange"] def valid_char(self, ch): """ @@ -1160,6 +1171,7 @@ class Edit(Text): self.allow_tab = allow_tab self._edit_pos = 0 self.set_caption(caption) + self._edit_text = '' self.set_edit_text(edit_text) if edit_pos is None: edit_pos = len(edit_text) @@ -1355,9 +1367,11 @@ class Edit(Text): text = self._normalize_to_caption(text) self.highlight = None self._emit("change", text) + old_text = self._edit_text self._edit_text = text if self.edit_pos > len(text): self.edit_pos = len(text) + self._emit("postchange", old_text) self._invalidate() def get_edit_text(self): diff --git a/urwid/wimp.py b/urwid/wimp.py index 25e70c1..ea73fb4 100755 --- a/urwid/wimp.py +++ b/urwid/wimp.py @@ -109,7 +109,7 @@ class CheckBox(WidgetWrap): # allow users of this class to listen for change events # sent when the state of this widget is modified # (this variable is picked up by the MetaSignals metaclass) - signals = ["change"] + signals = ["change", 'postchange'] def __init__(self, label, state=False, has_mixed=False, on_state_change=None, user_data=None): @@ -121,7 +121,7 @@ class CheckBox(WidgetWrap): function call for a single callback :param user_data: user_data for on_state_change - Signals supported: ``'change'`` + Signals supported: ``'change'``, ``"postchange"`` Register signal handler with:: @@ -233,7 +233,8 @@ class CheckBox(WidgetWrap): # self._state is None is a special case when the CheckBox # has just been created - if do_callback and self._state is not None: + old_state = self._state + if do_callback and old_state is not None: self._emit('change', state) self._state = state # rebuild the display widget with the new state @@ -241,6 +242,8 @@ class CheckBox(WidgetWrap): ('fixed', self.reserve_columns, self.states[state] ), self._label ] ) self._w.focus_col = 0 + if do_callback and old_state is not None: + self._emit('postchange', old_state) def get_state(self): """Return the state of the checkbox.""" @@ -335,7 +338,7 @@ class RadioButton(CheckBox): This function will append the new radio button to group. "first True" will set to True if group is empty. - Signals supported: ``'change'`` + Signals supported: ``'change'``, ``"postchange"`` Register signal handler with:: |