如何从 Urwid 的单个列表中的复选框传递值?

how to pass the value from checkboxes in single list from Urwid?

大家好,我是 urwid 的新手。我想发送从复选框列表中选择的复选框的值,它将添加到列表中并将该列表发送到下一页。下面是我的代码,我在打印列表框值时得到的是空值。

    from urwid import (CheckBox, SimpleListWalker, ListBox, Text, Frame,
                   MainLoop, ExitMainLoop, connect_signal)

words = ["obelus", "bumfuzzle", "rubaboo", "ballyhoo", "everywhen",
         "octothorpe", "meldrop", "sozzled", "pizazz", "pronk"]
palette = [
        ('reverse','light gray','black'),
        ('important','dark blue','light gray',('standout','underline')),
        ('buttn','white','default'),
        ('buttnf','white','dark blue','bold'),
        ('h2', 'white,underline,bold', 'dark red'),
        ('header','white','dark red', 'bold'),
        ('body', 'default', 'default'),]

def update_footer(widget, state):

  footer.set_text(", ".join([checkbox.label
                            for checkbox in checkboxes
                            if checkbox.state is True]))

checkboxes = []

for word in words:
  checkbox = CheckBox(label=word)
  connect_signal(checkbox, "postchange", update_footer)
  checkboxes.append(checkbox)

list_walker = SimpleListWalker(contents=checkboxes)

listbox = ListBox(list_walker)

footer = Text(markup="")


print(listbox)
frame = Frame(header=header , body=listbox)

MainLoop(widget=frame).run()

我们正在得到上述问题的解决方案

 from urwid import (CheckBox, SimpleListWalker, ListBox, Text, Frame,
                   MainLoop, ExitMainLoop, connect_signal)

import urwid

choices = ["obelus", "bumfuzzle", "rubaboo", "ballyhoo", "everywhen",
         "octothorpe", "meldrop", "sozzled", "pizazz", "pronk"]



def item_chosen(widget,state):
    list1 = [checkbox.label
                            for checkbox in checkboxes
                            if checkbox.state is True]

def exit_program(button):
    list1 = [checkbox.label
                            for checkbox in checkboxes
                            if checkbox.state is True]



    txt = urwid.Text(u"Selected Items are "+str(list1))
    fill = urwid.Filler(txt, 'top')

    frame2 = Frame(body=fill)

    MainLoop(widget=frame2).run()

    #raise ExitMainLoop()


checkboxes = []

for c in choices:
    checkbox = CheckBox(label=c)
    connect_signal(checkbox, "postchange",item_chosen)
    checkboxes.append(checkbox)



main = urwid.Padding(ListBox(SimpleListWalker(checkboxes)), left=2, right=2)

top = urwid.Overlay(main, urwid.SolidFill(u'\N{MEDIUM SHADE}'),
    align='center', width=('relative', 60),
    valign='middle', height=('relative', 60),
    min_width=20, min_height=9)


footer = urwid.Button(u'submit')
urwid.connect_signal(footer, 'click', exit_program)


frame = Frame(body=top,footer = footer)


urwid.MainLoop(widget=frame, palette=[('reversed', 'standout', '')]).run()