Guest User

Untitled

a guest
Feb 18th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. #!/usr/bin/env python
  2. #
  3. # Copyright (C) 2011 by Yu-Jie Lin
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a copy
  6. # of this software and associated documentation files (the "Software"), to deal
  7. # in the Software without restriction, including without limitation the rights
  8. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. # copies of the Software, and to permit persons to whom the Software is
  10. # furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. # THE SOFTWARE.
  22.  
  23.  
  24. import urwid
  25.  
  26.  
  27. class MyBigText(urwid.BigText):
  28.  
  29. _selectable = True
  30. signals = ['keypress', 'mouse_event']
  31.  
  32. def keypress(self, size, key):
  33.  
  34. # self._emit(), i.e. Widget._emit(), does not handle/return return value of
  35. # event handler.
  36. if urwid.emit_signal(self, 'keypress', self, size, key):
  37. return key
  38.  
  39. def mouse_event(self, *args):
  40.  
  41. self._emit('mouse_event', *args)
  42. return True
  43.  
  44.  
  45. def main():
  46.  
  47. TEXT = 'MyBigText [F10 to Quit]'
  48.  
  49. palette = [
  50. ('bigtext', 'white', 'dark gray'),
  51. ('bigtext focus', 'light red', 'white'),
  52. ('listbox header', 'light blue', 'dark green'),
  53. ('listbox', 'white', 'dark gray'),
  54. ('listbox focus', 'light red', 'white'),
  55. ]
  56.  
  57. def event_handler(w, *args):
  58.  
  59. if args[1] == 'f10':
  60. return True
  61. btevts.append(urwid.Text(repr(args)))
  62. btevts.set_focus(len(btevts) - 1)
  63.  
  64. def unhandled_input(inp):
  65.  
  66. if inp == 'f10':
  67. raise urwid.ExitMainLoop
  68. uhevts.append(urwid.Text(repr(inp)))
  69. uhevts.set_focus(len(uhevts) - 1)
  70.  
  71. font = urwid.HalfBlock5x4Font()
  72. btxt = MyBigText(TEXT, font)
  73. urwid.connect_signal(btxt, 'keypress', event_handler)
  74. urwid.connect_signal(btxt, 'mouse_event', event_handler)
  75.  
  76. try:
  77. if urwid.AttrMap.pack.__func__ is urwid.Widget.pack.__func__:
  78. urwid.AttrMap.pack = property(lambda self:self._original_widget.pack)
  79. except:
  80. pass
  81.  
  82. btxt = urwid.AttrMap(btxt, 'bigtext', 'bigtext focus')
  83.  
  84. btxt_p = urwid.Padding(btxt, width='clip')
  85. btxt_f = urwid.Filler(btxt_p, height=font.height)
  86.  
  87. btevts = urwid.SimpleListWalker([])
  88. uhevts = urwid.SimpleListWalker([])
  89.  
  90. pile = urwid.Pile([
  91. ('fixed', font.height, btxt_f),
  92. ('fixed', 1, urwid.AttrMap(urwid.Filler(urwid.Text('MyBigText Events')), 'listbox header')),
  93. ('weight', 1, urwid.AttrMap(urwid.ListBox(btevts), 'listbox', 'listbox focus')),
  94. ('fixed', 1, urwid.AttrMap(urwid.Filler(urwid.Text('Unhandled Events')), 'listbox header')),
  95. ('weight', 1, urwid.AttrMap(urwid.ListBox(uhevts), 'listbox', 'listbox focus')),
  96. ])
  97.  
  98. loop = urwid.MainLoop(pile, palette=palette, unhandled_input=unhandled_input)
  99. loop.run()
  100.  
  101.  
  102. if __name__ == '__main__':
  103. main()
Add Comment
Please, Sign In to add comment