检查时如何删除复选按钮

时间:2014-12-11 02:50:16

标签: python tkinter

我已经构建了一个简单的待办事项列表,我试图让复选框在检查时自行删除(表示任务已经完成)

我不确定如何实现该功能以便自行删除。任何人都可以帮我解决这个问题。我已经梳理了一系列页面,但没有一个真正表明你是如何做到这一点的。

class App(object):

def __init__(self, master):
    self.master = master
    self.frame = Frame(master)
    self.frame.grid()
    self.addFrame = Frame(master)
    self.addFrame.grid(row=0, column=0, columnspan=2, sticky='N')
    self.listFrame = Frame(master)
    self.listFrame.grid(row=1, column=0, columnspan=2, sticky='NW')
    self.todoList = []
    self.initUI()

def initUI(self):

    self.entryBox = Entry(self.frame, width = 15)
    self.entryBox.grid(row=0, column=0, sticky='N')

    self.addButton = Button(self.frame, text="<-ADD->", command=self.add)
    self.addButton.grid(row=0, column=1, sticky='N')


def removeCheckButton(self):
    # - CONFUSED HOW TO REMOVE THE SPECIFIC CHECKBUTTON
    pass

def add(self):
    entry = self.entryBox.get()
    self.entryBox.delete(0, END)
    self.todoList.append(entry)
    print self.todoList
    var1 = IntVar()
    self.buttonList = []
    for n in range(len(self.todoList)):
        lx = Checkbutton(self.listFrame, text=self.todoList[n], variable=self.todoList[n], command=removeCheckButton)
        lx.grid(row=n, column=0, sticky='NW')
        self.buttonList.append(lx)
        print self.buttonList

1 个答案:

答案 0 :(得分:1)

看看这个。你的add设计有点奇怪(而且IMO不正确),所以我稍微修改了它以及其他部分。

from tkinter import *


class App(object):

    def __init__(self, master):
        self.master = master
        self.frame = Frame(master)
        self.frame.grid()
        self.addFrame = Frame(master)
        self.addFrame.grid(row=0, column=0, columnspan=2, sticky='N')
        self.listFrame = Frame(master)
        self.listFrame.grid(row=1, column=0, columnspan=2, sticky='NW')
        self.todoList = []
        self.buttonList = []  #<--- button list is here now
        self.initUI()

    def initUI(self):

        self.entryBox = Entry(self.frame, width = 15)
        self.entryBox.grid(row=0, column=0, sticky='N')

        self.addButton = Button(self.frame, text="<-ADD->", command=self.add)
        self.addButton.grid(row=0, column=1, sticky='N')


    def removeCheckButton(self, button_no):
        # - CONFUSED HOW TO REMOVE THE SPECIFIC CHECKBUTTON
       # print(button_no, self.buttonList[button_no])
        #self.buttonList[button_no].grid_forget()
        self.buttonList[button_no].destroy()
       # del self.buttonList[button_no]
       # del self.todoList[button_no]


    def add(self):
        entry = self.entryBox.get()
        self.entryBox.delete(0, END)
        self.todoList.append(entry)
        print(self.todoList)
        var1 = IntVar()
        #self.buttonList = [] #<--- not sense having this here
      #  for n in range(len(self.todoList)): #<-- this for also very strange here.
        n = len(self.buttonList)
        lx = Checkbutton(self.listFrame,
                         text=self.todoList[n],
                         variable=self.todoList[n],
                         command=lambda ni=n: self.removeCheckButton(ni))
        lx.grid(row=n, column=0, sticky='NW')
        self.buttonList.append(lx)
         #   print(self.buttonList)


root = Tk()
app = App(root)
root.mainloop()

P.S。 我使用python 3,但除了import part之外,代码应该为你执行。可能它需要更多的修复,但复选框现在应该被破坏了。