我有一系列条目小部件文本框(姓名、号码、电子邮件等),用于收集或显示联系信息。如果我填充这些并将它们“添加”到我的 sqlite,这将正常工作。但是,如果我选择一个现有联系人并用现有数据填充这些框,然后修改一个条目并尝试将此修改后的条目添加到 sqlite 数据库,则 Entry.get(x) 命令只会提取之前文本框中的数据我从现有条目中填充了它。
例如,我创建了一个新联系人“Bob”,这样就可以保存了。我将“鲍勃”更改为“弗兰克”,这也可以节省。然后,我从现有条目“Michael”中填充字段,无论我现在做什么,我只检索“Frank”作为此框中的条目。插入似乎锁定了条目。
def CurSelet(event=None):
i_contact = populate(int((listbox1.curselection()[0])))
def populate(index):
site_id = (Entry.get(x))
conn = sqlite3.connect('example.db')
c = conn.cursor()
c.execute('SELECT * FROM sites WHERE site_id=?', (site_id,))
site_info = c.fetchall()
pop_name(site_info,index)
pop_num(site_info,index)
pop_email(site_info,index)
pop_mob(site_info,index)
conn.close()
def pop_name(site_info,index):
cne = Entry(root)
cne.grid(row=1, column=8, columnspan=2)
cne.focus()
cne.delete(0, tk.END)
cne.insert(0, (site_info[index])[1])
def pop_num(site_info,index):
cnbre = Entry(root)
cnbre.grid(row=2, column=8, columnspan=2)
cnbre.focus()
cnbre.delete(0, tk.END)
cnbre.insert(0, (site_info[index])[2])
def pop_email(site_info,index):
cee = Entry(root)
cee.grid(row=3, column=8, columnspan=2)
cee.focus()
cee.delete(0, tk.END)
cee.insert(0, (site_info[index])[3])
def pop_mob(site_info,index):
cme = Entry(root)
cme.grid(row=4, column=8, columnspan=2)
cme.focus()
cme.delete(0, tk.END)
cme.insert(0, (site_info[index])[4])
def add():
conn = sqlite3.connect('example.db')
c = conn.cursor()
c.execute('SELECT * FROM sites')
#print c.fetchall()
site_id = (Entry.get(x))
contact_name = (Entry.get(cne))
print(contact_name)
contact_number = (Entry.get(cnbre))
contact_email = (Entry.get(cee))
contact_mobile = (Entry.get(cme))
contact_alt = (Entry.get(cae))
c.execute("insert into sites values (?,?,?,?,?,?)",(site_id, contact_name, contact_number, contact_email, contact_mobile, contact_alt))
#c.execute("INSERT INTO sites VALUES('S900000', 'contact_name', '12345', 'contact_email', '12345', '12345')")
conn.commit()
conn.close()
#Site_ID entry box
text = Label(root, text="Please enter the Site ID.")
text.grid(row=0, column=1)
x = Entry(root)
x.grid(row=1, column=1, columnspan=1)
Label(root, text="Site ID").grid(row=1, sticky=W)
# button to Populate from existing contact
button3 = tk.Button(root, text='Select Contact.', command=CurSelet)
button3.grid(row=4, column=3, sticky=tk.E)
#Contact detail entry
text = Label(root, text="Contact Name.")
text.grid(row=1, column=7)
cne = Entry(root)
cne.grid(row=1, column=8, columnspan=2)
text = Label(root, text="Contact Number.")
text.grid(row=2, column=7)
cnbre = Entry(root)
cnbre.grid(row=2, column=8, columnspan=2)
text = Label(root, text="Contact Email.")
text.grid(row=3, column=7)
cee = Entry(root)
cee.grid(row=3, column=8, columnspan=2)
text = Label(root, text="Mobile Number.")
text.grid(row=4, column=7)
cme = Entry(root)
cme.grid(row=4, column=8, columnspan=2)
text = Label(root, text="Alternate Number.")
text.grid(row=5, column=7)
cae = Entry(root)
cae.grid(row=5, column=8, columnspan=2)
button5 = tk.Button(root, text='Delete', command=delete)
button5.grid(row=9, column=7, sticky=tk.E)
button6 = tk.Button(root, text='Add', command=add)
button6.grid(row=9, column=8, sticky=tk.E)
root.mainloop()
我根本看不到是什么将数据锁定到这些测试框中。