问题描述
我有一个 Tkinter 应用程序,在该应用程序中我有一个 OptionMenu
,它为我提供了位于列表 id's
中的所有 vehicleid
。请注意,此列表可以变大或变小。
现在我希望我的按钮根据用户的选择将 owner
和 vehicleid
的数据发送到数据库。因此,如果我有例如 2 vehicleid's
,我首先需要选择一个特定的 vehicleid
,并且对于每个 vehicleid
,我需要选择一个特定的 owner
。
所以在 2 vehicleid
的情况下,我的数据库应该是这样的:
vehicleid owner
C161 --- Spain
C162 --- United Kingdom
应用如下所示:
这是我的代码:
owner = ['Spain','United Kingdom','Malaysia']
vehicleid = ['C161','C162']
window = Tk()
window.title("Running Python Script") # Create window
window.geometry('550x300') # Geo of the window
##These are the option menus
dd_owner = StringVar(window)
dd_owner.set(owner[0]) # the first value
w = OptionMenu(window,dd_owner,*owner)
w.grid(row=1,column=1)
dd_id = StringVar(window)
dd_id.set(vehicleid[0])
w0 = OptionMenu(window,dd_id,*vehicleid)
w0.grid(row=0,column=1)
##The run button
run_list_button =Button(window,text="Send data of ID's to database!")
run_list_button.grid(column=0,row=3)
##These are the titles
l1 = Label(window,text='Select Owner',width=15)
l1.grid(row=1,column=0)
l0 = Label(window,text='Select vehicle id:',width = 30)
l0.grid(row=0,column=0)
mainloop()
解决方法
虽然你的问题令人困惑。看了你的讨论后,我明白你想在用户确认他们的选择后才将所有数据发送到数据库。
在这种情况下,您可能需要一个字典来存储车辆 ID 和所有者 {"vehicle_id": [],"owner": []}
,直到用户点击更新数据库按钮。更新数据库后,请确保清空字典,以免先前选择的项目再次插入数据库。
注意:您仍然需要多次按下另一个按钮才能将数据插入字典。可以通过控制变量的trace方法选择不带按钮
这是一个例子
from tkinter import *
import sqlite3
CREATE_QUERY = "CREATE TABLE IF NOT EXISTS vehicle(vehicle_id VARCHAR(5),owner VARCHAR(100));"
INSERT_QUERY = "INSERT INTO vehicle(vehicle_id,owner) VALUES(?,?);"
SELECT_QUERY = "SELECT * FROM vehicle;"
sql_file = "sample.db"
id_dict = {"vehicle_id": [],"owner": []}
def create_data_base():
with sqlite3.connect(sql_file) as conn:
conn.execute(CREATE_QUERY)
conn.commit()
def insert_to_db():
global id_dict
with sqlite3.connect(sql_file) as conn:
for value in zip(*id_dict.values()):
conn.execute(INSERT_QUERY,value)
conn.commit()
id_dict = {"vehicle_id": [],"owner": []} # empty the list once you insert the data
display_data()
def display_data():
with sqlite3.connect(sql_file) as conn:
curr = conn.cursor()
curr.execute(SELECT_QUERY)
items = curr.fetchall()
print(items)
def add():
id_dict["vehicle_id"].append(dd_id.get())
id_dict["owner"].append(dd_owner.get())
print(id_dict)
owner = ['Spain','United Kingdom','Malaysia']
vehicleid = ['C161','C162']
window = Tk()
window.title("Running Python Script") # Create window
window.geometry('550x300') # Geo of the window
create_data_base()
##These are the option menus
dd_owner = StringVar(window)
dd_owner.set(owner[0]) # the first value
w = OptionMenu(window,dd_owner,*owner)
w.grid(row=1,column=1)
dd_id = StringVar(window)
dd_id.set(vehicleid[0])
w0 = OptionMenu(window,dd_id,*vehicleid)
w0.grid(row=0,column=1)
Button(window,text='Add',command=add).grid(column=1,row=3)
##The run button
run_list_button =Button(window,text="Send data of ID's to database!",command=insert_to_db)
run_list_button.grid(column=0,row=3)
##These are the titles
l1 = Label(window,text='Select Owner',width=15)
l1.grid(row=1,column=0)
l0 = Label(window,text='Select vehicle id:',width = 30)
l0.grid(row=0,column=0)
window.mainloop()
以上代码会将字典中的所有数据插入到数据库中。
,首先,您应该将数据存储在某处(字典或文件..),然后在用户按下按钮时读取数据。
import mysql.connector as mysql
....
mydb = mysql.connect(host = 'localhost',user = 'root',passwd = '****.',database = 'table_data')
data = {}
def store():
if dd_id.get() not in data:
data[dd_id.get()] = dd_owner.get()
print(data)
def upload():
cur = mydb.cursor()
for item in data.items():
sql = 'INSERT INTO table_data VALUES (%s,%s)'
params = (item[0],item[1])
cur.execute(sql,params)
mydb.commit()
print('Done')
....
# The store button
Button(window,text="Store data!",command=store).grid(column=0,row=3)
# The database button
Button(window,text="Send to database",command=upload).grid(column=0,row=4)
当点击相应的按钮时,这会将数据存储在数据库中,也不允许重复条目或条目更新。