lundi 17 juillet 2017

Checkbutton "Get" issue

I am trying to allow a user to select from a table the words s/he would like to see presented in a cvs. I am having an issue because when I expect the "checked" words to have a "1" for being true, they all appear to have "0"s no matter what.

Here is the code I have for now:

class Example(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)
        b = tk.Button(self, text="Done!", command=self.upload_cor)
        b.grid()

        canvas=Canvas(self,bg='#FFFFFF',width=300,height=10000,scrollregion=(0,0,500,15000))
        vbar=Scrollbar(self,orient=VERTICAL)
        vbar.grid(row=1,column=1,sticky='nsw')
        vbar.config(command=canvas.yview)
        canvas.config(width=300,height=300)
        canvas.config(yscrollcommand=vbar.set)
        canvas.grid(row=1,column=0,sticky='nsew')
        table = tk.Frame(canvas, width=300, height=10000)
        canvas.create_window(0,0,anchor='nw',height=10000,width=300,window=table)

        data = (word_freq)

        self.widgets = {}
        row = 0

        for word, freq in (data):
            row += 1
            self.widgets[word] = {
                "Word": tk.Label(table, text=word),
                "Freq": tk.Label(table, text=freq),

                "checkbox_var": tk.IntVar(),
                "checkbox": tk.Checkbutton(table)
            }

            self.widgets[word]["Word"].grid(row=row, column=0, sticky="nsew")
            self.widgets[word]["Freq"].grid(row=row, column=1, sticky="nsew")

            self.widgets[word]["checkbox"].config(variable=(self.widgets[word]["checkbox_var"]))
            self.widgets[word]["checkbox"].grid(row=row, column=2, sticky="nsew")

            self.widgets[word]["checkbox"].select()

        table.grid_columnconfigure(1, weight=1)
        table.grid_columnconfigure(2, weight=1)

    def upload_cor(self):
        for word in sorted(self.widgets.keys()):
            check_var = self.widgets[word]["checkbox_var"]
            print(check_var.get())

if __name__ == "__main__":
    root = tk.Tk()
    Example(root).grid(sticky='nsew')
    root.mainloop()

Some values of "word_freq" are:

[('hamlet', 469), ('lord', 310), ("'s", 230), ('king', 201), ("'d", 169), ('horatio', 157), ('--', 136), ('claudius', 120), ('queen', 119), ('polonius', 119), ('shall', 114), ('good', 107)]

Not sure if that is where the problem lies, but if so, how can I fix those values to make it work?




Aucun commentaire:

Enregistrer un commentaire