Python Tkinter Tk support checklist box?

前端 未结 1 1681
既然无缘
既然无缘 2021-01-16 05:53

I am trying to create checklist box in GUI . Is possible to do Tkinter ?( i DON\'T want list of Check box)

I know Python Wx GUI development have this suppor

相关标签:
1条回答
  • 2021-01-16 06:47

    Tkinter doesn't have a widget like wxPython's ChecklistBox. However, one is trivial to create as a group of checkbuttons inside a frame.

    Example:

    class ChecklistBox(tk.Frame):
        def __init__(self, parent, choices, **kwargs):
            tk.Frame.__init__(self, parent, **kwargs)
    
            self.vars = []
            bg = self.cget("background")
            for choice in choices:
                var = tk.StringVar(value=choice)
                self.vars.append(var)
                cb = tk.Checkbutton(self, var=var, text=choice,
                                    onvalue=choice, offvalue="",
                                    anchor="w", width=20, background=bg,
                                    relief="flat", highlightthickness=0
                )
                cb.pack(side="top", fill="x", anchor="w")
    
    
        def getCheckedItems(self):
            values = []
            for var in self.vars:
                value =  var.get()
                if value:
                    values.append(value)
            return values
    

    Example of usage:

    choices = ("Author", "John", "Mohan", "James", "Ankur", "Robert")
    checklist = ChecklistBox(root, choices, bd=1, relief="sunken", background="white")
    ...
    print("choices:", checklist.getCheckedItems())
    

    0 讨论(0)
提交回复
热议问题