如何设置列表框(urwid)中的项目样式?

2024-09-28 17:22:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用文本项创建一个列表框,其中每个项的第一行是一个粗体的白色标题(前两个单词是绿色的),接下来的两行是灰色的正文文本。在

以下是我目前的情况:

class SelectableText(urwid.Text):
    def selectable(self):
        return True

    def keypress(self, size, key):
        return key

palette = [('reveal focus', 'black', 'dark cyan', 'standout')]

items = ["This part should be green and is the bold title!\nThese are the two lines of grey body text... Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc facilisis at mi eu efficitur.", "This part should be green and is the bold title!\nThese are the two lines of grey body text... Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc facilisis at mi eu efficitur."]

item_list  = list(map(lambda item: urwid.AttrMap(SelectableText(item), None, "reveal focus"), items))
item_list_walker= urwid.SimpleListWalker(item_list)
listbox = urwid.ListBox(item_list_walker)

layout = urwid.Filler(listbox, valign="top", top=1, bottom=1)
main_loop = urwid.MainLoop(layout, palette)
main_loop.run()

我的问题是如何在不更改“显示焦点”属性的情况下向项目添加显示属性?i、 当项目文本被选中时,它仍然应该以深青色的背景变成黑色,但是当它没有被选中时,它的样式会有所不同。在


Tags: thekey文本selfreturndef情况items