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

时间:2013-10-03 04:14:55

标签: python tkinter

我有一系列Entry小部件文本框(名称,号码,电子邮件等)我用它来收集或显示联系信息。如果我填充这些并将它们“添加”到我的sqlite,这可以正常工作。但是,如果我选择现有联系人并使用现有数据填充这些框,然后修改条目并尝试将此修改后的条目添加到sqlite数据库,则Entry.get(x)命令只会拉取文本框中的数据我从现有条目填充它。

例如,我创建了一个新的联系人'Bob',这样可以保存。我将'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() 

我根本无法看到将数据锁定到这些测试框中的内容。

1 个答案:

答案 0 :(得分:1)

所有pop_xxx函数都会创建新的Entry小部件,并将它们放入局部变量中,其他函数无法看到它们。

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

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

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

def pop_name(...):
    cne.delete(...)
    ...
相关问题