mercredi 9 novembre 2016

tkinter: dynamically add checkboxes to grid and read values individually

I would like to dynamically add checkboxes to a tkinter grid and then have the user check some of them and afterwards see, which checkboxes were checked.

Here's the code:

    top = tkinter.Tk()
    var2 = tkinter.IntVar()
    main()
    top.mainloop()

# in main
 for feat in feats:
                counter += 1

                tkinter.Checkbutton(top, text=str(merkmal) + " ist " + 

str(label), command=lambda: testResult(),
                                             variable=var2).grid(row=counter, sticky="W")

   # callback
    def testResult():
        print(var2)

Two problems:

a) I need a vertical and horizontal scrollbar on my grid

I have looked at other posts and seen that I need to add a canvas and then add the scrollbar to the canvas but I havent gotten it to work. Here's my attempt:

w = tkinter.Canvas(top)
    scrollbar = tkinter.Scrollbar(top, orient="vertical",command=w.yview)
    scrollbar.grid(row=0,column=1,sticky='ns')

    w.config(yscrollcommand=scrollbar.set, xscrollcommand=scrollbar.set)
    w.grid(row=0,column=0,sticky='nsew')

and then addind the checkboxes to w instead of top. The vertical scrollbar shows, but I cant scroll.

b) When I check one of the boxes, all of them get checked. How can I make it so that each checkbox can be checked individually?

I am new to tkinter and really don't have to learn too much into it, just need this to work somehow.

Thanks for any help!

Aucun commentaire:

Enregistrer un commentaire