This answer quotes ChatGPT
You can add a checkbox column to the table container, with one checkbox for each row. Users can check one or more checkboxes to select data that needs to be submitted to another database. Then, during the commit operation, it iterates through all the rows in the table container, checking that the check box for each row is checked, and if it is checked, it commits the data for that row to another database.
The exact implementation ofmay vary from case to case, but here is a simple example code to help you understand the process:
import sqlite3
import tkinter as tk
from tkinter import ttk
# 连接源数据库
conn_src = sqlite3.connect('source.db')
c_src = conn_src.cursor()
# 连接目标数据库
conn_dest = sqlite3.connect('destination.db')
c_dest = conn_dest.cursor()
# 创建表格容器和复选框列
root = tk.Tk()
table = ttk.Treeview(root)
table['columns'] = ('name', 'age', 'selected')
table.column('name', width=100)
table.column('age', width=50)
table.column('selected', width=50)
table.heading('name', text='Name')
table.heading('age', text='Age')
table.heading('selected', text='Selected')
for i in range(5):
table.insert('', i, text=str(i), values=('Name {}'.format(i), i+20, False))
# 提交选中的数据到目标数据库
def submit_selected_data():
for row in table.get_children():
values = table.item(row)['values']
if values[2]:
c_dest.execute("INSERT INTO people (name, age) VALUES (?, ?)", (values[0], values[1]))
conn_dest.commit()
print('Selected data submitted')
# 勾选复选框时更新数据
def update_selected_data(event):
item = table.focus()
values = table.item(item)['values']
values = (values[0], values[1], not values[2])
table.item(item, values=values)
table.bind('' , update_selected_data)
# 显示表格和提交按钮
table.pack()
submit_button = tk.Button(root, text='Submit selected data', command=submit_selected_data)
submit_button.pack()
root.mainloop()