[elbe-devel] [PATCH v2 03/13] Debianize - Button widget

dion at linutronix.de dion at linutronix.de
Thu Aug 1 17:49:24 CEST 2019


From: Olivier Dion <dion at linutronix.de>

A simple and clean button widget.

Signed-off-by: Olivier Dion <dion at linutronix.de>
---
 elbepack/debianize/widgets/button.py | 52 ++++++++++++++++++++++++++++++++++++
 1 file changed, 52 insertions(+)
 create mode 100644 elbepack/debianize/widgets/button.py

diff --git a/elbepack/debianize/widgets/button.py b/elbepack/debianize/widgets/button.py
new file mode 100644
index 00000000..6e9663a9
--- /dev/null
+++ b/elbepack/debianize/widgets/button.py
@@ -0,0 +1,52 @@
+# ELBE - Debian Based Embedded Rootfilesystem Builder
+# Copyright (c) 2019 Olivier Dion <dion at linutronix.de>
+#
+# SPDX-License-Identifier: GPL-3.0-or-later
+
+
+from urwid import (
+    AttrMap,
+    LineBox,
+    Text,
+    WidgetWrap,
+    connect_signal,
+    emit_signal
+)
+
+from elbepack.debianize.base.tui import TUISignal
+
+
+class Button(WidgetWrap):
+    """
+    A simple and clean button widget
+    """
+
+    signals = [TUISignal.CLICK]
+
+    def __init__(self, text, palette, callback):
+        """
+        @text:  The text of show in the button.
+        @palette:  A valid palette name to apply on the button.  See @TUI::palette
+        @callback:  The callback to call when the button is clicked.
+        """
+
+        widget = LineBox(AttrMap(Text("[{}]".format(text), align="center"),
+                                 "default", palette))
+        super(Button, self).__init__(widget)
+        connect_signal(self, TUISignal.CLICK, callback)
+
+        # Very important!  This is not documented in urwid's
+        # documentation!  Without this, the button would only text.
+        self._w.base_widget._selectable = True
+
+    def keypress(self, size, key):
+        if key == "enter":
+            emit_signal(self, TUISignal.CLICK)
+            return None
+        return key
+
+    def mouse_event(self, size, event, button, col, row, focus):
+        if focus and event == "mouse release":
+            emit_signal(self, TUISignal.CLICK)
+            return True
+        return False
-- 
2.11.0




More information about the elbe-devel mailing list