Tkinter:Checkbuttons和list

时间:2016-12-13 14:49:20

标签: python-3.x checkbox tkinter

嘿伙计我在tkinter中遇到了checkboxen的问题。有人可以说我的错吗?

def edit_contact_gui(self):
    """GUI to edit the created contacts."""
    self.edit_contact_wd = tk.Tk()
    self.edit_contact_wd.title('Edit Contacts of the Phonebook:"%s"'\
                               % self.book)
    self.button_edit = tk.Button(self.edit_contact_wd, text = 'Edit',\
                                 command = self.edit_contact)


    try:
        with open('%s.txt' % self.book, 'rb') as file:
            book = pickle.load(file)
            x = 1
            self.var_lst = []
            for i in book:
                var = tk.IntVar()
                tk.Label(self.edit_contact_wd, text = i).grid(row = x, \
                                                                column = 0)

                tk.Checkbutton(self.edit_contact_wd, text = 'edit', \
                               variable = var).grid(row = x, column = 1)
                self.var_lst.append(var.get())

                x += 1

            self.button_edit.grid(row = x+1, column = 1)

    except FileNotFoundError:
        tk.Label(self.edit_contact_wd, text = 'The phonebook has no entrys!', fg = 'red').grid(row = 1, column = 0)

    self.edit_contact_wd.mainloop()

def edit_contact(self):
    print(self.var_lst)

我的GUI输出有效,但程序返回一个全部为零的List [0,0,0,0,0]。在我看来,标记的复选框已返回1,但它没有。为什么?你能救我吗?

1 个答案:

答案 0 :(得分:0)

您必须将IntVarvar)保留在列表中,而不是来自IntVarvar.get())的值

 self.var_lst.append(var) # without .get()

并且在edit_contact()中您必须使用get()

for var in self.var_lst:
    print(var.get())