lundi 14 novembre 2022

tkinter long list of checkbox to display in many columns [duplicate]

I did this short code to display à long (200+) list of variable to make a selection via checkbox.


def gui_selection(list_depart):

    import tkinter
    root = tkinter.Tk()
    selection = []

    nb = 1
    r = 0
    c = 0
    for x in range(len(list_depart)):
        # this part is for grid positionning
        if nb > 20:
            r = 0
            c = c + 1
            nb = 1
        nb = nb + 1
        # end of grid positionning 

        l = tkinter.Checkbutton(root, text=list_depart[x], variable=list_depart[x],
                                command=lambda x=list_depart[x]: selection.append(x)).grid(row=r, column=c)
        l.pack(anchor='w')
    tkinter.Button(root, text="Ok", command=lambda: [print(selection), root.destroy()]).pack()
    root.mainloop()

    return selection

I try to limit to 20 the number of row and start a new columns each time this value is reach. It was working great until I try to add the grid() like an an exemple I follow using grid.

error: File "E:/Python/Projet/AI_hydro/modelisation/load_data.py", line 145, in gui_selection l.pack(anchor='w') AttributeError: 'NoneType' object has no attribute 'pack'

This is my first interface user, thank for your help




Aucun commentaire:

Enregistrer un commentaire