リストボックスとsqlite3の両方から行を削除する方法を見つけようとしています. 一番下に「delButton」関数がありますが、リストボックスと sql から行を削除する必要がありますか? ここで何をしているのかわかりませんが、削除部分に関しては、さまざまな例からコピーして貼り付けてその機能をまとめました。
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("<FocusIn>", 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("<FocusIn>", 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("<FocusIn>", 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("<FocusIn>", 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()
これがエラーです。
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