代码之家  ›  专栏  ›  技术社区  ›  AFoeee

如何使python库“urwid”的按钮看起来漂亮?

  •  3
  • AFoeee  · 技术社区  · 6 年前

    当几个按钮并排排列在一起时,也会让人恼火。

    我如何才能实现一个按钮显示一个框架和居中的文本,并改变其颜色时,它得到的重点?

    enter image description here

    1 回复  |  直到 6 年前
        1
  •  8
  •   Elias Dorneles    4 年前

    您可以使用Urwid绘制任何可以在终端中使用纯Unicode文本绘制的内容,并为每个字符指定前景和背景颜色。

    考虑到这一点,它是不可能画完全一样,因为它是在您的模型,因为绘制边界,您需要使用 the Unicode box drawing characters ,这将占用更多空间。

    我在这里分享它的未完成状态(工作,但选择的外观是小车),希望你可能会发现它有用,也许可以作为一个足够好的起点,让你摆弄。

    截图:

    enter image description here

    enter image description here

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    from __future__ import print_function, absolute_import, division
    import urwid
    
    
    PALETTE = [
        ('normal', '', ''),
        ('bold', 'bold', ''),
        ('blue', 'bold', 'dark blue'),
        ('highlight', 'black', 'dark blue'),
    ]
    
    
    def show_or_exit(key):
        if key in ('q', 'Q', 'esc'):
            raise urwid.ExitMainLoop()
    
    
    class CustomButton(urwid.Button):
        button_left = urwid.Text('[')
        button_right = urwid.Text(']')
    
    
    def custom_button(*args, **kwargs):
        b = CustomButton(*args, **kwargs)
        b = urwid.AttrMap(b, '', 'highlight')
        b = urwid.Padding(b, left=4, right=4)
        return b
    
    
    class BoxButton(urwid.WidgetWrap):
        _border_char = u'─'
        def __init__(self, label, on_press=None, user_data=None):
            padding_size = 2
            border = self._border_char * (len(label) + padding_size * 2)
            cursor_position = len(border) + padding_size
    
            self.top = u'┌' + border + u'┐\n'
            self.middle = u'│  ' + label + u'  │\n'
            self.bottom = u'└' + border + u'┘'
    
            # self.widget = urwid.Text([self.top, self.middle, self.bottom])
            self.widget = urwid.Pile([
                urwid.Text(self.top[:-1]),
                urwid.Text(self.middle[:-1]),
                urwid.Text(self.bottom),
            ])
    
            self.widget = urwid.AttrMap(self.widget, '', 'highlight')
    
            # self.widget = urwid.Padding(self.widget, 'center')
            # self.widget = urwid.Filler(self.widget)
    
            # here is a lil hack: use a hidden button for evt handling
            self._hidden_btn = urwid.Button('hidden %s' % label, on_press, user_data)
    
            super(BoxButton, self).__init__(self.widget)
    
        def selectable(self):
            return True
    
        def keypress(self, *args, **kw):
            return self._hidden_btn.keypress(*args, **kw)
    
        def mouse_event(self, *args, **kw):
            return self._hidden_btn.mouse_event(*args, **kw)
    
    
    if __name__ == '__main__':
        header = urwid.Text('Header')
        footer = urwid.Text('Footer')
        onclick = lambda w: footer.set_text('clicked: %r' % w)
        widget = urwid.Pile([
            header,
            urwid.Text('Simple custom buttons:'),
            urwid.Columns([
                custom_button('OK', on_press=onclick),
                custom_button('Cancel', on_press=onclick),
            ]),
            urwid.Text('Box bordered buttons:'),
            urwid.Columns([
                urwid.Padding(BoxButton('OK', on_press=onclick), left=4, right=4),
                BoxButton('Cancel', on_press=onclick),
            ]),
            footer,
        ])
        widget = urwid.Filler(widget, 'top')
        loop = urwid.MainLoop(widget, PALETTE, unhandled_input=show_or_exit)
        loop.run()
    
        2
  •  1
  •   Lucio Paiva    4 年前

    在埃利亚斯的出色回答的基础上,我接受了他的回答 BoxButton 通过使用 LineBox 而不是手动绘制边框:

    class BoxButton(urwid.WidgetWrap):
        """ Taken from https://stackoverflow.com/a/65871001/778272
        """
        def __init__(self, label, on_click):
            label_widget = urwid.Text(label, align='center')
            self.widget = urwid.LineBox(label_widget)
            self.hidden_button = urwid.Button('hidden button', on_click)
            super(BoxButton, self).__init__(self.widget)
    
        def selectable(self):
            return True
    
        def keypress(self, *args, **kwargs):
            return self.hidden_button.keypress(*args, **kwargs)
    
        def mouse_event(self, *args, **kwargs):
            return self.hidden_button.mouse_event(*args, **kwargs)
    
    

    urwid.Columns([
        (14, BoxButton('Deploy', on_deploy_clicked)),
        (1, urwid.Text(' ')),  # quick hack to add gap
        (14, BoxButton('Restart', on_restart_clicked)),
    ])
    

    enter image description here

    故意不装饰自己。要装饰它:

    urwid.AttrMap(BoxButton('Deploy', on_clicked), 'normal', 'highlight')
    
    推荐文章