import urwid
palette = [('header', 'white', 'black'),
('reveal focus', 'black', 'dark cyan', 'standout'),]
content = urwid.SimpleListWalker([
urwid.AttrMap(w, None, 'reveal focus') for w in [
urwid.Text("This is a text string that is fairly long"),
urwid.Divider("-"),
urwid.Text("Short one"),
urwid.Text("Another"),
urwid.Divider("-"),
urwid.Text("What could be after this?"),
urwid.Text("The end."),]
])
listbox = urwid.ListBox(content)
show_key = urwid.Text("", wrap='clip')
head = urwid.AttrMap(show_key, 'header')
top = urwid.Frame(listbox, head)
def show_all_input(input, raw):
show_key.set_text("Pressed: " + " ".join([
unicode(i) for i in input]))
return input
def exit_on_cr(input):
if input == 'enter':
raise urwid.ExitMainLoop()
loop = urwid.MainLoop(top, palette, input_filter=show_all_input, unhandled_input=exit_on_cr)
loop.run()
上または下を押すとリストがスクロールするはずですが、私の場合はそうではありません。何か問題がありますか?
編集1
コードは完全に機能します。実際、スクロール可能なリストが何であるかを理解していませんでした。上または下を押すとリスト内の項目が選択されると思っていましたが、そうではありません。これが行うことは、一度にすべてのアイテムを表示するのに十分なスペースがない場合に端末をスクロールすることです。このコードの動作を理解するには、端末のサイズを非常に小さいサイズに変更する必要があります。
編集2
上または下を押したときにフォーカスを変更するには、奇妙な API を使用する必要があります。listbox.focus_next()
/のような APIlistbox.focus_previous()
があればよかったのですが、自分で位置を処理する必要があります。もちろん、独自の関数 (または ListBox のサブクラス) を作成して、より優れた API を提供することもできます。
def handle_input(input):
if input == "esc":
raise urwid.ExitMainLoop()
head.original_widget.set_text("key pressed: %s" % input)
lw = listbox.body
if input == "up":
# this can raise an error if we scroll past first position
try:
lw.set_focus(lw.get_prev(lw.get_focus()[1])[1])
except:
pass
elif input == "down":
# this can raise an error if we scroll past last position
try:
lw.set_focus(lw.get_next(lw.get_focus()[1])[1])
except:
pass
編集3
より良い API :
def urwid_test():
"""
'black', 'dark red', 'dark green', 'brown', 'dark blue',
'dark magenta', 'dark cyan', 'light gray', 'dark gray',
'light red', 'light green', 'yellow', 'light blue',
'light magenta', 'light cyan', 'white'
"""
class MyListBox(urwid.ListBox):
def focus_next(self):
try:
self.body.set_focus(self.body.get_next(self.body.get_focus()[1])[1])
except:
pass
def focus_previous(self):
try:
self.body.set_focus(self.body.get_prev(self.body.get_focus()[1])[1])
except:
pass
def handle_input(input):
if input == "esc":
raise urwid.ExitMainLoop()
head.original_widget.set_text("key pressed: %s" % input)
if input == "up":
listbox.focus_previous()
elif input == "down":
listbox.focus_next()
palette = [("top","white","black"),
("line","light green","dark green","standout"),
("frame","dark magenta","white"),
]
widgets = [urwid.AttrMap(widget,None,"line") for widget in
[
urwid.Text("Chemma!"),
urwid.Divider("-"),
urwid.Text("Another text widget!"),
urwid.Divider("-"),
urwid.Text("What is your name"),
urwid.Divider("-"),
urwid.Text("Boy ?"),
]
]
head = urwid.AttrMap(urwid.Text("key pressed :",wrap="clip"),"top")
L = urwid.SimpleListWalker(widgets)
listbox = MyListBox(L)
top = urwid.AttrMap(urwid.Frame(listbox,head),"frame")
loop = urwid.MainLoop(top,palette,unhandled_input=handle_input)
loop.screen.set_terminal_properties(colors=256)
loop.run()
if __name__ == "__main__":
urwid_test()