# HG changeset patch # User Goffi # Date 1447262235 -3600 # Node ID 2b7eafea8bdb133ad86922a042d55db968a3ada9 # Parent bf38c1c0db3b6bbde37cbf9247e7d77a7f49e9d1 GenericDialog: buttons callbacks can now be set after widget creation diff -r bf38c1c0db3b -r 2b7eafea8bdb urwid_satext/sat_widgets.py --- a/urwid_satext/sat_widgets.py Wed Nov 11 18:16:25 2015 +0100 +++ b/urwid_satext/sat_widgets.py Wed Nov 11 18:17:15 2015 +0100 @@ -1122,29 +1122,35 @@ style = [] frame_header = urwid.AttrMap(urwid.Text(title,'center'),'title') - buttons = None + self.buttons = collections.OrderedDict() if "OK/CANCEL" in style: - cancel_arg = [kwargs['cancel_value']] if kwargs.has_key('cancel_value') else [] - ok_arg = [kwargs['ok_value']] if kwargs.has_key('ok_value') else [] - buttons = [urwid.Button(_("Cancel"), kwargs['cancel_cb'], *cancel_arg), - urwid.Button(_("Ok"), kwargs['ok_cb'], *ok_arg)] + self.buttons['cancel'] = urwid.Button(_("Cancel"), kwargs.get('cancel_cb'), kwargs.get('cancel_value')) + self.buttons['ok'] = urwid.Button(_("Ok"), kwargs.get('ok_cb'), kwargs.get('ok_value')) elif "YES/NO" in style: - yes_arg = [kwargs['yes_value']] if kwargs.has_key('yes_value') else [] - no_arg = [kwargs['no_value']] if kwargs.has_key('no_value') else [] - buttons = [urwid.Button(_("Yes"), kwargs['yes_cb'], *yes_arg), - urwid.Button(_("No"), kwargs['no_cb'], *no_arg)] + self.buttons['yes'] = urwid.Button(_("Yes"), kwargs.get('yes_cb'), kwargs.get('yes_value')) + self.buttons['no'] = urwid.Button(_("No"), kwargs.get('no_cb'), kwargs.get('no_value')) if "OK" in style: - ok_arg = [kwargs['ok_value']] if kwargs.has_key('ok_value') else [] - buttons = [urwid.Button(_("Ok"), kwargs['ok_cb'], *ok_arg)] - if buttons: - buttons_flow = urwid.GridFlow(buttons, max([len(button.get_label()) for button in buttons])+4, 1, 1, 'center') + self.buttons['ok'] = urwid.Button(_("Ok"), kwargs.get('ok_cb'), kwargs.get('ok_value')) + if self.buttons: + buttons_flow = urwid.GridFlow(self.buttons.values(), max([len(button.get_label()) for button in self.buttons.itervalues()])+4, 1, 1, 'center') body_content = urwid.SimpleListWalker(widgets_lst) frame_body = UnselectableListBox(body_content) - frame = FocusFrame(frame_body, frame_header, buttons_flow if buttons else None, 'footer' if buttons else 'body') + frame = FocusFrame(frame_body, frame_header, buttons_flow if self.buttons else None, 'footer' if self.buttons else 'body') decorated_frame = urwid.LineBox(frame) urwid.WidgetWrap.__init__(self, decorated_frame) + def setCallback(self, name, callback, data=None): + """Set the callback associated with a button press + + @param name: one of "ok", "cancel", "yest", "no" + @aram callback(callable): method to call on requested action + @param args*: argument to send to the callback (first one will be the button widget + @raise KeyError if name is invalid + """ + button = self.buttons[name] + urwid.connect_signal(button, 'click', callback, data) + class InputDialog(GenericDialog): """Dialog with an edit box""" @@ -1174,7 +1180,9 @@ class Alert(GenericDialog): """Dialog with just a message and a OK button""" - def __init__(self, title, message, style=['OK'], **kwargs): + def __init__(self, title, message, style=None, **kwargs): + if style is None: + style= ['OK'] GenericDialog.__init__(self, [urwid.Text(message, 'center')], title, style, ok_value=None, **kwargs) ## CONTAINERS ##