从 tkinter 树视图中的数据库中提取。数据填充正确,并且编码为位于正确的标头下。但是,当我去更新记录时,正确的字段会更新,但新信息和其余信息会移动/显示到不正确的标题下。有没有办法让它留在正确的标题中/下,或者我的代码在某个地方不正确?下面的示例代码供审查。我已经尝试了很多迭代,但继续回到下面的粗体部分,它似乎正在切换显示更新信息的位置。任何和所有帮助、想法和/或建议表示赞赏。谢谢。
my_tree = ttk.Treeview(tree_frame, yscrollcommand=tree_scroll.set, selectmode="extended")
my_tree.pack()
my_tree['columns'] = ("User", "Full Name", "Location", "Drawer 1", "Drawer 2", "Drawer 3", "Supervisor")
my_tree.column("#0", width=0, stretch=NO)
my_tree.column("User", anchor=W, width=140)
my_tree.column("Full Name", anchor=W, width=140)
my_tree.column("Location", anchor=W, width=175)
my_tree.column("Drawer 1", anchor=W, width=140)
my_tree.column("Drawer 2", anchor=W, width=140)
my_tree.column("Drawer 3", anchor=W, width=140)
my_tree.column("Supervisor", anchor=W, width=140)
my_tree.heading("#0", text="", anchor=W)
my_tree.heading("User", text="User", anchor=W)
my_tree.heading("Full Name", text="Full Name", anchor=W)
my_tree.heading("Location", text="Location", anchor=W)
my_tree.heading("Drawer 1", text="Drawer 1", anchor=W)
my_tree.heading("Drawer 2", text="Drawer 2", anchor=W)
my_tree.heading("Drawer 3", text="Drawer 3", anchor=W)
my_tree.heading("Supervisor", text="Supervisor", anchor=W)
for record in records:
supervisor_value = 'Y' if record[5] else 'N' # Convert True to 'Y' and False to 'N'
active_value = 'Yes' if record[2] else 'No'
create_value = 'Yes' if record[4] else 'No'
void_value = 'Yes' if record[6] else 'No'
reconcile_value = 'Yes' if record[7] else 'No'
drawer_value = 'Yes' if record[8] else 'No'
my_tree.insert(parent='', index='end', iid=record[0], text='', values=(record[0], record[3], record[1], record[9], record[10], record[11], supervisor_value, create_value, void_value, active_value, reconcile_value, drawer_value), tags=(my_tag))
#CLEAR ENTRY BOXES
def clear_entries():
loc_entry.delete(0, END)
active_check.delete(0, END)
casr_entry.delete(0, END)
fn_entry.delete(0, END)
create_check.delete(0, END)
superv_check.delete(0, END)
void_check.delete(0, END)
recon_check.delete(0, END)
drwer_check.delete(0, END)
draw1_entry.delete(0, END)
draw2_entry.delete(0, END)
draw3_entry.delete(0, END)
#GRAB RECORD NUMBER
selected = my_tree.focus()
#GRAB RECORD VALUES
values = my_tree.item(selected, 'values')
#OUTPUT TO BOXES
loc_entry.insert(0, values[2])
active_check.insert(0, values[9])
casr_entry.insert(0, values[0])
fn_entry.insert(0, values[1])
create_check.insert(0, values[7])
superv_check.insert(0, values[6])
void_check.insert(0, values[8])
recon_check.insert(0, values[10])
drwer_check.insert(0, values[11])
draw1_entry.insert(0, values[3])
draw2_entry.insert(0, values[4])
draw3_entry.insert(0, values[5])
def update_record():
#GRAB THE RECORD NUMBER
selected = my_tree.focus()
#UPDATE RECORD
my_tree.item(selected, text="", values=(loc_entry.get(), active_check.get(), casr_entry.get(), fn_entry.get(), create_check.get(), superv_check.get(), void_check.get(), recon_check.get(), drwer_check.get(), draw1_entry.get(), draw2_entry.get(), draw3_entry.get(), ))
#CLEAR ENTRY BOXES
loc_entry.delete(0, END)
active_check.delete(0, END)
casr_entry.delete(0, END)
fn_entry.delete(0, END)
create_check.delete(0, END)
superv_check.delete(0, END)
void_check.delete(0, END)
recon_check.delete(0, END)
drwer_check.delete(0, END)
draw1_entry.delete(0, END)
draw2_entry.delete(0, END)
draw3_entry.delete(0, END)
updt_button = Button(edit_frame, text = "Update Cashier", font = 'Helvetica 12 bold', background ='light gray', command=update_record)
updt_button.grid(row=0, column=1, padx=10, pady=10)
slt_button = Button(edit_frame, text = "Clear Boxes", font = 'Helvetica 12 bold', background ='light gray', command = clear_entries)
slt_button.grid(row=0, column=2, padx=10, pady=10)
#BIND THE TREEVIEW
my_tree.bind("<ButtonRelease-1>", select)
下面的图片是用 Excel 制作的示例:
曾尝试重新查询数据库,在更新后再次提取信息,但结果加倍。更新的信息转到正确的字段,它正在更改显示的位置,因此尝试移动字段,但没有成功。
请注意,
values
内my_tree.item(...)
的update_record()
选项的值的顺序应如下所示:
def update_record():
#GRAB THE RECORD NUMBER
selected = my_tree.focus()
#UPDATE RECORD
my_tree.item(selected, text="",
values=(
casr_entry.get(),
fn_entry.get(),
loc_entry.get(),
draw1_entry.get(),
draw2_entry.get(),
draw3_entry.get(),
superv_check.get(),
create_check.get(),
void_check.get(),
active_check.get(),
recon_check.get(),
drwer_check.get()
))
...