使用“Insert”后,条目小部件“get”调用不会更新

2024-10-02 16:24:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一系列的条目小部件文本框(姓名、号码、电子邮件等),用于收集或显示联系信息。如果我填充这些内容并将它们“添加”到sqlite中,这将正常工作。但是,如果我选择了一个现有联系人并用现有数据填充这些框,那么修改一个条目并尝试将这个修改后的条目添加到sqlite数据库中入口。获取(x) 命令只提取文本框中的数据,然后我从现有条目填充它。你知道吗

例如,我创建了一个新的联系人“Bob”,这样可以保存OK。我把“Bob”改成“Frank”,这样也省钱了。 然后,我从现有条目“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() 

我根本看不出是什么将数据锁定到这些测试框中。你知道吗


Tags: textinfoindexsitecontactcolumnrootpop
1条回答
网友
1楼 · 发布于 2024-10-02 16:24:44

所有的pop_xxx函数都会创建新的条目小部件,并将它们放入其他函数看不到的局部变量中。你知道吗

要写入全局变量,必须使用global statement

def pop_name(...):
    global cne
    cne = Entry(root)
    ...

但是,实际上不需要重新创建小部件。 只需重用现有的:

def pop_name(...):
    cne.delete(...)
    ...

相关问题 更多 >