2

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

Reference image

I know Python Wx GUI development have this support but I am looking for support in Tk.

If anyone have idea please share link for details or way to do it ?

  • 1
    What do you mean by "I DON'T want list of Checkbox"? Why not? How is "checkbox list" different than "list of Checkbox" in your mind? – Bryan Oakley May 17 '18 at 19:17

2 Answers2

8

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

enter image description here

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
0

Would this be something you could use?
I wanted to get that functionality as well.
So I tried my hand at creating one.

Image for checkbox [0,1,-1]
 chk = -1 Separator text

Optional: Image for data item. User data assigned to the item.

CheckListBox image

        leftframe = tk.Frame( topframe)
    leftframe.pack(side='left')
    # create listbox object
    example = CheckListBox(leftframe,height=60,width=20)
    example.pack(side="left", fill="both", expand=True)
    for i in range( 0,10):
        data = []
        example.setvalue( data , "altData","Some data here."+str( i +1) )
        example.setvalue( data ,"tip", "Item Tool Tip here")
        pic = folderImg if i%2==0 else fileImg
        example.append(  "First text " + str( i +1) , bg='lightgray' , chk=i%2 , image=pic, appdata=data)
MD Mushfirat Mohaimin
  • 1,966
  • 3
  • 10
  • 22
DrHoule
  • 1
  • 1