Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- #
- # Copyright (C) 2011 by Yu-Jie Lin
- #
- # Permission is hereby granted, free of charge, to any person obtaining a copy
- # of this software and associated documentation files (the "Software"), to deal
- # in the Software without restriction, including without limitation the rights
- # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- # copies of the Software, and to permit persons to whom the Software is
- # furnished to do so, subject to the following conditions:
- #
- # The above copyright notice and this permission notice shall be included in
- # all copies or substantial portions of the Software.
- #
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- # THE SOFTWARE.
- import urwid
- class MyBigText(urwid.BigText):
- _selectable = True
- signals = ['keypress', 'mouse_event']
- def keypress(self, size, key):
- # self._emit(), i.e. Widget._emit(), does not handle/return return value of
- # event handler.
- if urwid.emit_signal(self, 'keypress', self, size, key):
- return key
- def mouse_event(self, *args):
- self._emit('mouse_event', *args)
- return True
- def main():
- TEXT = 'MyBigText [F10 to Quit]'
- palette = [
- ('bigtext', 'white', 'dark gray'),
- ('bigtext focus', 'light red', 'white'),
- ('listbox header', 'light blue', 'dark green'),
- ('listbox', 'white', 'dark gray'),
- ('listbox focus', 'light red', 'white'),
- ]
- def event_handler(w, *args):
- if args[1] == 'f10':
- return True
- btevts.append(urwid.Text(repr(args)))
- btevts.set_focus(len(btevts) - 1)
- def unhandled_input(inp):
- if inp == 'f10':
- raise urwid.ExitMainLoop
- uhevts.append(urwid.Text(repr(inp)))
- uhevts.set_focus(len(uhevts) - 1)
- font = urwid.HalfBlock5x4Font()
- btxt = MyBigText(TEXT, font)
- urwid.connect_signal(btxt, 'keypress', event_handler)
- urwid.connect_signal(btxt, 'mouse_event', event_handler)
- try:
- if urwid.AttrMap.pack.__func__ is urwid.Widget.pack.__func__:
- urwid.AttrMap.pack = property(lambda self:self._original_widget.pack)
- except:
- pass
- btxt = urwid.AttrMap(btxt, 'bigtext', 'bigtext focus')
- btxt_p = urwid.Padding(btxt, width='clip')
- btxt_f = urwid.Filler(btxt_p, height=font.height)
- btevts = urwid.SimpleListWalker([])
- uhevts = urwid.SimpleListWalker([])
- pile = urwid.Pile([
- ('fixed', font.height, btxt_f),
- ('fixed', 1, urwid.AttrMap(urwid.Filler(urwid.Text('MyBigText Events')), 'listbox header')),
- ('weight', 1, urwid.AttrMap(urwid.ListBox(btevts), 'listbox', 'listbox focus')),
- ('fixed', 1, urwid.AttrMap(urwid.Filler(urwid.Text('Unhandled Events')), 'listbox header')),
- ('weight', 1, urwid.AttrMap(urwid.ListBox(uhevts), 'listbox', 'listbox focus')),
- ])
- loop = urwid.MainLoop(pile, palette=palette, unhandled_input=unhandled_input)
- loop.run()
- if __name__ == '__main__':
- main()
Add Comment
Please, Sign In to add comment