0

連絡先情報を収集または表示するために使用する一連のエントリ ウィジェット テキスト ボックス (名前、番号、電子メールなど) があります。これらを入力して 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() 

これらのテスト ボックスに何がデータをロックしているのか、まったくわかりません。

4

1 に答える 1

1

すべてのpop_xxx関数は新しい Entry ウィジェットを作成し、それらを他の関数から見えないローカル変数に入れます。

グローバル変数に書き込むには、global ステートメントを使用する必要があります。

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

ただし、実際にウィジェットを再作成する必要はありません。既存のものを再利用するだけです:

def pop_name(...):
    cne.delete(...)
    ...
于 2013-10-03T06:58:48.717 に答える