Cómo eliminar una fila de un cuadro de lista en TKinter y SQLite3

Estoy tratando de averiguar cómo eliminar una fila tanto del cuadro de lista como de sqlite3. Tengo una función ‘delButton’ en la parte inferior, ¿necesito eliminar la línea del cuadro de lista y sql? No estoy seguro de lo que estoy haciendo aquí, en lo que respecta a la parte de eliminación, he juntado esa función copiando y pegando de varios ejemplos.

from Tkinter import * import sqlite3 class Hello(): def __init__(self, master): frame=Frame(master, width=80, height=50) frame.pack() self.text = Label(frame, text=" ") self.text.pack() self.text.grid(row=0, sticky=W) self.text["text"] = " Address Book" self.lab = Label(frame, text="name") self.lab.pack(side=LEFT) self.lab.grid(row=1) self.nameField = Entry(frame, text = "name", width=30) self.nameField.insert(0, "Enter Name") self.nameField.pack() self.nameField.grid(row=1) self.nameField.bind("", self.clearNameField) self.ageField = Entry(frame, text = "age", width=30) self.ageField.insert(0, "Enter Age") self.ageField.pack() self.ageField.grid(row=2) self.ageField.bind("", self.clearAgeField) self.phoneField = Entry(frame, text = "phone", width=30) self.phoneField.insert(0, "Enter Phone #") self.phoneField.pack() self.phoneField.grid(row=3) self.phoneField.bind("", self.clearPhoneField) self.fbField = Entry(frame, text = "fblink", width=30) self.fbField.insert(0, "Enter Facebook Link") self.fbField.pack() self.fbField.grid(row=4) self.fbField.bind("", self.clearFbField) self.btn=Button(frame, text='Add', command=self.add_note) self.btn.pack() self.btn.grid(row=5, rowspan=1, sticky="w") self.showbtn = Button(frame, text='Update', command=self.show_notes) self.showbtn.pack() self.showbtn.grid(row=5, rowspan=1, sticky="") self.delbtn = Button(frame, text='Delete', command=self.del_notes) self.delbtn.pack() self.delbtn.grid(row=5, rowspan=1, sticky="e") self.content=Listbox(master, width=50) self.content.pack() def clearNameField(self, event): self.nameField.delete(0,END) def clearAgeField(self, event): self.ageField.delete(0,END) def clearPhoneField(self, event): self.phoneField.delete(0,END) def clearFbField(self, event): self.fbField.delete(0,END) def add_note(self): if self.nameField.get() == "": self.text["text"] = "Please type sumting" else: item = self.nameField.get() item2 = self.ageField.get() item3 = self.phoneField.get() item4 = self.fbField.get() self.nameField.delete(0, END) self.ageField.delete(0, END) self.phoneField.delete(0, END) self.fbField.delete(0, END) conn = sqlite3.connect('phonebook1.db') c = conn.cursor() conn.execute(''' CREATE TABLE IF NOT EXISTS people(name TEXT primary key, age TEXT, phone TEXT, fblink TEXT)''') c.execute("insert into people values (?, ?, ?, ?)", (item, item2, item3, item4)) conn.commit() list=c.execute("SELECT * FROM people") conn.commit() for row in list: self.content.insert(END, row) c.close() def del_notes(self): conn = sqlite3.connect('phonebook1.db') c =conn.cursor() list=c.execute("SELECT * FROM people") conn.commit() for row in list: self.content.delete(END, row) c.close() 

Aquí está el error.

 File "phonebookapp.py", line 106, in del_notes self.content.delete(END, row) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 2565, in delete self.tk.call(self._w, 'delete', first, last) TclError: bad listbox index "ben ben ben benn": must be active, anchor, end, @x,y, or a number 

Código de trabajo:

(tienes que detectar el error cuando alguien intenta volver a agregar el mismo nombre)

(toda la información sobre Listbox que encontré en http://effbot.org/tkinterbook/listbox.htm )

 #!/usr/bin/python from Tkinter import * import sqlite3 class Hello(): def __init__(self, master): frame=Frame(master, width=80, height=50) frame.pack() self.text = Label(frame, text=" ") self.text.pack() self.text.grid(row=0, sticky=W) self.text["text"] = " Address Book" self.lab = Label(frame, text="name") self.lab.pack(side=LEFT) self.lab.grid(row=1) self.nameField = Entry(frame, text = "name", width=30) self.nameField.insert(0, "Enter Name") self.nameField.pack() self.nameField.grid(row=1) self.nameField.bind("", self.clearNameField) self.ageField = Entry(frame, text = "age", width=30) self.ageField.insert(0, "Enter Age") self.ageField.pack() self.ageField.grid(row=2) self.ageField.bind("", self.clearAgeField) self.phoneField = Entry(frame, text = "phone", width=30) self.phoneField.insert(0, "Enter Phone #") self.phoneField.pack() self.phoneField.grid(row=3) self.phoneField.bind("", self.clearPhoneField) self.fbField = Entry(frame, text = "fblink", width=30) self.fbField.insert(0, "Enter Facebook Link") self.fbField.pack() self.fbField.grid(row=4) self.fbField.bind("", self.clearFbField) self.btn=Button(frame, text='Add', command=self.add_note) self.btn.pack() self.btn.grid(row=5, rowspan=1, sticky="w") # self.showbtn = Button(frame, text='Update', command=self.show_notes) # self.showbtn.pack() # self.showbtn.grid(row=5, rowspan=1, sticky="") self.delbtn = Button(frame, text='Delete', command=self.del_notes) self.delbtn.pack() self.delbtn.grid(row=5, rowspan=1, sticky="e") self.content=Listbox(master, width=50) self.content.pack() # open database self.conn = sqlite3.connect('phonebook1.db') c = self.conn.cursor() # create table c.execute('''CREATE TABLE IF NOT EXISTS people(name TEXT primary key, age TEXT, phone TEXT, fblink TEXT)''') self.conn.commit() # read people people = c.execute("SELECT * FROM people") self.conn.commit() # add to list for person in people: self.content.insert(END, person) c.close() def clearNameField(self, event): self.nameField.delete(0,END) def clearAgeField(self, event): self.ageField.delete(0,END) def clearPhoneField(self, event): self.phoneField.delete(0,END) def clearFbField(self, event): self.fbField.delete(0,END) def add_note(self): if self.nameField.get() == "": self.text["text"] = "Please type something" else: item1 = self.nameField.get() item2 = self.ageField.get() item3 = self.phoneField.get() item4 = self.fbField.get() self.nameField.delete(0, END) self.ageField.delete(0, END) self.phoneField.delete(0, END) self.fbField.delete(0, END) # add to database c = self.conn.cursor() c.execute("INSERT INTO people VALUES (?, ?, ?, ?)", (item1, item2, item3, item4)) self.conn.commit() c.close() # add to list self.content.insert(END, (item1, item2, item3, item4)) def del_notes(self): # get selected person person = self.content.get(ACTIVE) name = person[0] # delete in database c = self.conn.cursor() c.execute("DELETE FROM people WHERE name=?", (name)) self.conn.commit() c.close() # delete on list self.content.delete(ANCHOR) def __del__(self): # close database self.conn.close() root = Tk() Hello(root) root.mainloop()