mercredi 6 février 2019

Show selected checkboxes names in TKinter

I have this Python script that, given a list, create a checkbox and show me the items I checked.

example = ["Windows", "Mac", "Ubuntu"]

class Checkbar(Frame):
    def __init__(self, parent=None, picks=[], side=LEFT, anchor=W):
        Frame.__init__(self, parent)
        self.vars = []
        for pick in picks:
            var = IntVar()
            chk = Checkbutton(self, text=pick, variable=var)
            chk.pack(side=side, anchor=anchor, expand=YES)
            self.vars.append(var)

    def state(self):
        return map((lambda var: var.get()), self.vars)


master = Tk()

lng = Checkbar(master, example)
lng.pack(side=TOP, fill=X)
lng.config(relief=GROOVE, bd=2)


def allstates():
    print(list(lng.state()))


Button(master, text='Quit', command=master.quit).pack(side=RIGHT)
Button(master, text='Peek', command=allstates).pack(side=RIGHT)
master.mainloop()

The result is this:

checkboxExample

I see that allstates function return me a list with the select box but in a binary form (0-1). What I would like to know is if there is any option to show the selected variable instead of 0,1 in the console output.

Thanks




Aucun commentaire:

Enregistrer un commentaire