0

我想弄清楚如何从列表框和 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
4

1 回答 1

0

工作代码:

(当有人尝试再次添加同名时,您必须捕获错误)

(我在http://effbot.org/tkinterbook/listbox.htm上找到了关于 Listbox 的所有信息)

#!/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("<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()

        # 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()
于 2013-11-02T04:23:20.227 回答