gpt4 book ai didi

python - 如何使 python 库 "urwid"的按钮看起来漂亮?

转载 作者:太空宇宙 更新时间:2023-11-03 13:58:31 25 4
gpt4 key购买 nike

urwid 中按钮的默认外观非常实用,但在我看来不是很漂亮。当几个按钮并排排成一排时,它也可能会令人恼火。

如何实现按钮显示有框架和居中文本,并在获得焦点时改变颜色?

enter image description here

最佳答案

您可以使用 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()

关于python - 如何使 python 库 "urwid"的按钮看起来漂亮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52252730/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com