Skip to content

Instantly share code, notes, and snippets.

@eliasdorneles
Created September 11, 2019 08:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eliasdorneles/7bb74d2b25d0d385e09976c1b70f878c to your computer and use it in GitHub Desktop.
Save eliasdorneles/7bb74d2b25d0d385e09976c1b70f878c to your computer and use it in GitHub Desktop.
Minimal example handling click in the terminal with Urwid
#!/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