root/bigtext.py

Revision 332:08f97445caca, 5.4 KB (checked in by Ian Ward <ian@…>, 2 years ago)

update examples for unhandled_input change

  • Property exe set to *
Line 
1#!/usr/bin/python
2#
3# Urwid BigText example program
4#    Copyright (C) 2004-2009  Ian Ward
5#
6#    This library is free software; you can redistribute it and/or
7#    modify it under the terms of the GNU Lesser General Public
8#    License as published by the Free Software Foundation; either
9#    version 2.1 of the License, or (at your option) any later version.
10#
11#    This library is distributed in the hope that it will be useful,
12#    but WITHOUT ANY WARRANTY; without even the implied warranty of
13#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14#    Lesser General Public License for more details.
15#
16#    You should have received a copy of the GNU Lesser General Public
17#    License along with this library; if not, write to the Free Software
18#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19#
20# Urwid web site: http://excess.org/urwid/
21
22"""
23Urwid example demonstrating use of the BigText widget.
24"""
25
26import urwid
27import urwid.raw_display
28
29
30class SwitchingPadding(urwid.Padding):
31    def padding_values(self, size, focus):
32        maxcol = size[0]
33        width, ignore = self.original_widget.pack(size, focus=focus)
34        if maxcol > width:
35            self.align = "left"
36        else:
37            self.align = "right"
38        return urwid.Padding.padding_values(self, size, focus)
39
40
41class BigTextDisplay:
42    palette = [
43        ('body',         'black',      'light gray', 'standout'),
44        ('header',       'white',      'dark red',   'bold'),
45        ('button normal','light gray', 'dark blue', 'standout'),
46        ('button select','white',      'dark green'),
47        ('button disabled','dark gray','dark blue'),
48        ('edit',         'light gray', 'dark blue'),
49        ('bigtext',      'white',      'black'),
50        ('chars',        'light gray', 'black'),
51        ('exit',         'white',      'dark cyan'),
52        ]
53       
54    def create_radio_button(self, g, name, font, fn):
55        w = urwid.RadioButton(g, name, False, on_state_change=fn)
56        w.font = font
57        w = urwid.AttrWrap(w, 'button normal', 'button select')
58        return w
59
60    def create_disabled_radio_button(self, name):
61        w = urwid.Text("    " + name + " (UTF-8 mode required)")
62        w = urwid.AttrWrap(w, 'button disabled')
63        return w
64   
65    def create_edit(self, label, text, fn):
66        w = urwid.Edit(label, text)
67        urwid.connect_signal(w, 'change', fn)
68        fn(w, text)
69        w = urwid.AttrWrap(w, 'edit')
70        return w
71
72    def set_font_event(self, w, state):
73        if state:
74            self.bigtext.set_font(w.font)
75            self.chars_avail.set_text(w.font.characters())
76
77    def edit_change_event(self, widget, text):
78        self.bigtext.set_text(text)
79
80    def setup_view(self):
81        fonts = urwid.get_all_fonts()
82        # setup mode radio buttons
83        self.font_buttons = []
84        group = []
85        utf8 = urwid.get_encoding_mode() == "utf8"
86        for name, fontcls in fonts:
87            font = fontcls()
88            if font.utf8_required and not utf8:
89                rb = self.create_disabled_radio_button(name)
90            else:
91                rb = self.create_radio_button(group, name, font,
92                    self.set_font_event)
93                if fontcls == urwid.Thin6x6Font:
94                    chosen_font_rb = rb
95                    exit_font = font
96            self.font_buttons.append( rb )
97       
98        # Create BigText
99        self.bigtext = urwid.BigText("", None)
100        bt = SwitchingPadding(self.bigtext, 'left', None)
101        bt = urwid.AttrWrap(bt, 'bigtext')
102        bt = urwid.Filler(bt, 'bottom', None, 7)
103        bt = urwid.BoxAdapter(bt, 7)
104       
105        # Create chars_avail
106        cah = urwid.Text("Characters Available:")
107        self.chars_avail = urwid.Text("", wrap='any')
108        ca = urwid.AttrWrap(self.chars_avail, 'chars')
109       
110        chosen_font_rb.set_state(True) # causes set_font_event call
111   
112        # Create Edit widget
113        edit = self.create_edit("", "Urwid "+urwid.__version__,
114            self.edit_change_event)
115       
116        # ListBox
117        chars = urwid.Pile([cah, ca])
118        fonts = urwid.Pile([urwid.Text("Fonts:")] + self.font_buttons,
119            focus_item=1)
120        col = urwid.Columns([('fixed',16,chars), fonts], 3, 
121            focus_column=1)
122        bt = urwid.Pile([bt, edit], focus_item=1)
123        l = [bt, urwid.Divider(), col]
124        w = urwid.ListBox(urwid.SimpleListWalker(l))
125       
126        # Frame
127        w = urwid.AttrWrap(w, 'body')
128        hdr = urwid.Text("Urwid BigText example program - F8 exits.")
129        hdr = urwid.AttrWrap(hdr, 'header')
130        w = urwid.Frame(header=hdr, body=w)
131
132        # Exit message
133        exit = urwid.BigText(('exit'," Quit? "), exit_font)
134        exit = urwid.Overlay(exit, w, 'center', None, 'middle', None)
135        return w, exit
136
137
138    def main(self):
139        self.view, self.exit_view = self.setup_view()
140        self.loop = urwid.MainLoop(self.view, self.palette, 
141            unhandled_input=self.unhandled_input)
142        self.loop.run()
143   
144    def unhandled_input(self, key):
145        if key == 'f8':
146            self.loop.widget = self.exit_view
147            return True
148        if self.loop.widget != self.exit_view:
149            return
150        if key in ('y', 'Y'):
151            raise urwid.ExitMainLoop()
152        if key in ('n', 'N'):
153            self.loop.widget = self.view
154            return True
155   
156
157def main():
158    BigTextDisplay().main()
159   
160if '__main__'==__name__:
161    main()
Note: See TracBrowser for help on using the browser.