Python 3.x 更改屏幕上的按钮标签重新绘制urwid

Python 3.x 更改屏幕上的按钮标签重新绘制urwid,python-3.x,button,urwid,Python 3.x,Button,Urwid,在urwid中,子窗口小部件是否有一种基于屏幕重画操作更改其属性的方法 具体来说,如果我有一个按钮小部件,带有一些很长的字符串: mybutton = urwid.Button(<some very long string>) mybutton=urwid.Button() 当终端重新调整到列宽小于字符串长度时,按钮是否有办法仅显示字符串的一部分?相当于: mybutton = urwid.Button(<some very long...>) mybutton=u

在urwid中,子窗口小部件是否有一种基于屏幕重画操作更改其属性的方法

具体来说,如果我有一个按钮小部件,带有一些很长的字符串:

mybutton = urwid.Button(<some very long string>)
mybutton=urwid.Button()
当终端重新调整到列宽小于字符串长度时,按钮是否有办法仅显示字符串的一部分?相当于:

mybutton = urwid.Button(<some very long...>)
mybutton=urwid.Button()

现在,urwid试图包装按钮标签,这在我的界面上下文中看起来很难看。

我认为实现这一点的最简单方法是将此行为实现为一个,它包装了普通的
urwid.button
,但强制它始终只在一行中渲染——下面是一个完全有效的示例:

from __future__ import print_function, absolute_import, division
import urwid    

class MyButton(urwid.WidgetWrap):
    def __init__(self, *args, **kw):
        self.button = urwid.Button(*args, **kw)
        super(MyButton, self).__init__(self.button)

    def rows(self, *args, **kw):
        return 1

    def render(self, size, *args, **kw):
        cols = size[0]
        maxsize = cols - 4  # buttons use 4 chars to draw < > borders
        if len(self.button.label) > maxsize:
            new_label = self.button.label[:maxsize - 3] + '...'
            self.button.set_label(new_label)
        return super(MyButton, self).render(size, *args, **kw)


def show_or_exit(key):
    if key in ('q', 'Q', 'esc'):
        raise urwid.ExitMainLoop()    


if __name__ == '__main__':
    # demo:
    widget = urwid.Pile([
        urwid.Text('Some buttons using urwid vanilla urwid.Button widget:\n'),
        urwid.Button('Here is a little button with really a really long string for label, such that it can be used to try out a problem described in Stackoverflow question'),
        urwid.Button('And here is another button, also with a really really long string for label, for the same reason as the previous one'),
        urwid.Button('And a short one'),
        urwid.Text('\n\n\nSame buttons, with a custom button widget:\n'),
        MyButton('Here is a little button with really a really long string for label, such that it can be used to try out a problem described in Stackoverflow question'),
        MyButton('And here is another button, also with a really really long string for label, for the same reason as the previous one'),
        MyButton('And a short one'),
        urwid.Columns([
            MyButton('Here is a little button with really a really long string for label, such that it can be used to try out a problem described in Stackoverflow question'),
            MyButton('And here is another button, also with a really really long string for label, for the same reason as the previous one'),
        ]),
    ])
    widget = urwid.Filler(widget, 'top')
    loop = urwid.MainLoop(widget, unhandled_input=show_or_exit)
    loop.run()
来自未来导入打印功能,绝对导入,分割
导入urwid
类MyButton(urwid.WidgetWrap):
定义初始值(自,*args,**kw):
self.button=urwid.button(*参数,**千瓦)
超级(MyButton,self)。\uuuu初始化(self.button)
def行(自身,*参数,**千瓦):
返回1
def渲染(自身、大小、*args、**kw):
cols=大小[0]
maxsize=cols-4#按钮使用4个字符绘制<>边框
如果len(self.button.label)>maxsize:
new_label=self.button.label[:maxsize-3]+'…'
self.button.set_标签(新_标签)
返回超级(MyButton,self).渲染(大小,*args,**kw)
def显示或退出(键):
如果输入('q','q','esc'):
raise urwid.ExitMainLoop()
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
#演示:
widget=urwid.Pile([
Text('一些使用urwid.urwid.Button小部件的按钮:\n'),
Button('这里有一个小按钮,标签的字符串非常长,可以用来尝试Stackoverflow问题中描述的问题'),
Button('这是另一个按钮,标签也有一个很长的字符串,原因与前一个相同'),
urwid.按钮(“和一个短按钮”),
Text('\n\n\n名称按钮,带有自定义按钮小部件:\n'),
MyButton('这里有一个小按钮,标签的字符串非常长,可以用来尝试Stackoverflow问题中描述的问题'),
MyButton('这是另一个按钮,标签也有很长的字符串,原因与前一个相同'),
MyButton(“和一个短按钮”),
urwid.列([
MyButton('这里有一个小按钮,标签的字符串非常长,可以用来尝试Stackoverflow问题中描述的问题'),
MyButton('这是另一个按钮,标签也有很长的字符串,原因与前一个相同'),
]),
])
widget=urwid.Filler(小部件“顶部”)
loop=urwid.MainLoop(小部件,未处理的输入=显示或退出)
loop.run()
了解更多
  • 说明如何使用WidgetWrap类扩展和自定义现有小部件
  • 解释了该方法的目的和方法