pythonpython-3.xcheckboxlistboxurwid

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


Hi everyone I'm new with the urwid. I want to send the values of checkboxes which are selected from checkbox list it will add into the list and send this list to the next page. Here below is my code in that i am getting empty value while printing the List box value.

    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()

Solution

  • we are getting the solution of above question

     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()