Tkinter:更新文本框中的值并在每次打开主文件时显示它们时出现问题

问题描述 投票:0回答:1

我有一些文本框,我必须不断更新它们的值。这个想法是写入文本框并保存它们的值,但总是“覆盖”数据库中的相同字段(而不创建新字段)。所以我想每次打开应用程序时自动显示文本框的内容。

问题是单击按钮时文本框值不会保存到数据库中。我认为

save

函数有问题(可能是sql的情况)

我做错了什么?我该如何修复我的代码?我是Python新手。谢谢你

import sqlite3 from tkinter import ttk import tkinter as tk from tkinter import messagebox root = tk.Tk() root.geometry("130x100") #Create database conn = sqlite3.connect("/home/Qi_Yao/Desktop/my_db.db") c = conn.cursor() c.execute("CREATE TABLE IF NOT EXISTS valuedb(all_value text)") conn.commit() conn.close() #Textbox textbox1 = ttk.Entry(root, width=7) textbox1.place(x=1, y=1) textbox2 = ttk.Entry(root, width=7) textbox2.place(x=1, y=30) #Save def save(): conn = sqlite3.connect("/home/Qi_Yao/Desktop/my_db.db") c = conn.cursor() if c: c.execute("UPDATE valuedb SET all_value=? WHERE id=?;") else: c.execute("INSERT INTO valuedb VALUES (?);") conn.commit() conn.close() messagebox.showinfo('Success', 'Saved and Update') #LOAD WHEN OPEN WINDOWS def load(): conn = sqlite3.connect("/home/Qi_Yao/Desktop/my_db.db") c = conn.cursor() c.execute("SELECT * FROM valuedb") vals = c.fetchall() conn.close() #SAVE BUTTON save = tk.Button(root, text="Save & Update", bg='#b40909', foreground='white', command= save) save.place(x=1, y=70) load() root.mainloop()


python sql python-3.x sqlite tkinter
1个回答
0
投票

#Save def save(): conn = sqlite3.connect("/home/Qi_Yao/Desktop/my_db.db") c = conn.cursor() if c: c.execute("UPDATE valuedb SET all_value=? WHERE id=?;", (textbox1.get(), textbox2.get())) else: c.execute("INSERT INTO valuedb VALUES (?);", (textbox1.get(),)) # Should be (?, ?) because the table has two fields as seen in the update query? conn.commit() conn.close() messagebox.showinfo('Success', 'Saved and Update')

© www.soinside.com 2019 - 2024. All rights reserved.