Created
September 11, 2019 08:29
-
-
Save eliasdorneles/7bb74d2b25d0d385e09976c1b70f878c to your computer and use it in GitHub Desktop.
Minimal example handling click in the terminal with Urwid
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
from __future__ import print_function, absolute_import, division | |
import urwid | |
PALETTE = [ | |
('bold', 'bold', ''), | |
] | |
def show_or_exit(key): | |
if key in ('q', 'Q', 'esc'): | |
raise urwid.ExitMainLoop() | |
class ClickableTextWidget(urwid.WidgetWrap): | |
def __init__(self): | |
self.text_widget = urwid.Text('[ Click me ]') | |
self.count = 0 | |
super(ClickableTextWidget, self).__init__(self.text_widget) | |
def mouse_event(self, size, event, button, col, row, focus): | |
if event == 'mouse press': | |
self.text_widget.set_text('[ You clicked me %d times ]' % self.count) | |
self.count += 1 | |
if __name__ == '__main__': | |
header = urwid.Text('Header') | |
footer = urwid.Text('Footer') | |
widget = urwid.Pile([ | |
header, | |
ClickableTextWidget(), | |
footer, | |
]) | |
widget = urwid.Filler(widget, 'top') | |
loop = urwid.MainLoop(widget, PALETTE, unhandled_input=show_or_exit) | |
loop.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment