如何更新urwid中显示的文本?
作者:互联网
与urwid混淆了.通过调整其中一个urwid示例,我得到初始屏幕,并希望在按下F5键时执行run_stuff()函数,并显示指示run_stuff()正在运行并最终完成的消息(理想情况下有进度条,但只显示消息将做).
到目前为止,我的代码在下面,有些东西告诉我运行第二个主循环不是一个好主意,但无法找出用新文本更新屏幕的其他方法.
import urwid
import urwid.raw_display
import urwid.web_display
import os, time
def run_stuff():
counter = 0
for x in range(10):
time.sleep(1)
counter += 1
# update progress bar: processing $counter out of 10
def main():
text_header = (u"Some header here! "
u"UP / DOWN / PAGE UP / PAGE DOWN scroll. F5 Run F8 exits.")
initial_text = [u"Here is ",('important', u"Initial Text."), ]
secondary_text = [u"Please wait while stuff is running" ]
options_list = [u"option1", u"option2", u"option3" ]
radio_button_group = []
blank = urwid.Divider()
listbox_content = [
blank,
urwid.Padding(urwid.Text(initial_text), left=2, right=2, min_width=20),
blank,
urwid.Padding(urwid.GridFlow(
[urwid.AttrWrap(urwid.RadioButton(radio_button_group,
txt), 'buttn','buttnf')
for txt in options_list],
28, 7, 1, 'left') ,
left=3, right=3, min_width=20),
blank,
blank,
]
header = urwid.AttrWrap(urwid.Text(text_header), 'header')
listbox = urwid.ListBox(urwid.SimpleListWalker(listbox_content))
frame = urwid.Frame(urwid.AttrWrap(listbox, 'body'), header=header)
palette = [
('body','black','light gray', 'standout'),
('reverse','light gray','black'),
('header','white','dark red', 'bold'),
('important','dark blue','light gray',('standout','underline')),
('editfc','white', 'dark blue', 'bold'),
('editbx','light gray', 'dark blue'),
('editcp','black','light gray', 'standout'),
('bright','dark gray','light gray', ('bold','standout')),
('buttn','black','dark cyan'),
('buttnf','white','dark blue','bold'),
]
# use appropriate Screen class
if urwid.web_display.is_web_request():
screen = urwid.web_display.Screen()
else:
screen = urwid.raw_display.Screen()
def unhandled(key):
if key == 'f8':
raise urwid.ExitMainLoop()
elif key == 'f5':
listbox_content = [
blank,
urwid.Padding(urwid.Text(secondary_text), left=2, right=2, min_width=20),
blank,
blank
]
header = urwid.AttrWrap(urwid.Text(text_header), 'header')
listbox = urwid.ListBox(urwid.SimpleListWalker(listbox_content))
frame = urwid.Frame(urwid.AttrWrap(listbox, 'body'), header=header)
urwid.MainLoop(frame, palette, screen, unhandled_input=unhandled).run()
run_stuff()
urwid.MainLoop(frame, palette, screen, unhandled_input=unhandled).run()
def setup():
urwid.web_display.set_preferences("Urwid Tour")
# try to handle short web requests quickly
if urwid.web_display.handle_short_request():
return
main()
if '__main__'==__name__ or urwid.web_display.is_web_request():
setup()
解决方法:
一种解决方案是将secondary_text定义为全局范围中的urwid.Text对象,然后从run_stuff()中调用其set_text方法.
标签:python,urwid 来源: https://codeday.me/bug/20190623/1270544.html