def add_laytime(self):
self.variaveis()
self.conecta_bd()
self.cursor.execute("""
INSERT OR IGNORE INTO laytime (vessel, comm, destino, weight, porto, loading, demurrage,
dispatch, allowed, day_of_oper, dem_disp, clima, estimated, r)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)""",
(self.vessel, self.comm, self.destino, self.weight, self.porto, self.loading, self.demurrage,
self.dispatch, self.allowed, self.dayoper, self.demdisp, self.clima, self.estimated, self.r))
self.conn.commit()
self.desconecta_bd()
self.select_lista()
self.limpar_tela()
def OnDoubleClick(self, event):
self.limpar_tela()
for n in self.listacli.selection():
col1, col2, col3, col4, col5, col6 = self.listacli.item(n, 'values')
self.vessel_entry.insert(END, col1)
self.comm_entry.insert(END, col2)
self.allowed_value.insert(END, col12)
self.estimated_value.insert(END, col15)
def lista_frame2(self):
self.listacli = ttk.Treeview(self.frame_2, height=3,
column=("col1", "col2", "col12", "col15"))
self.listacli.heading("#0", text="")
self.listacli.heading("#1", text="Vessel")
self.listacli.heading("#2", text="Commodity")
self.listacli.heading("#3", text="Allowed")
self.listacli.heading("#4", text="Estimated ($)")
self.listacli.column("#0", width=1)
self.listacli.column("#1", width=50)
self.listacli.column("#2", width=200)
self.listacli.column("#3", width=125)
self.listacli.column("#4", width=125)
self.listacli.place(relx=0.01, rely=0.1, relwidth=0.95, relheight=0.85)
我有一个需要填写的表格,当我点击添加信息并存储在frame2中时,只有船舶和商品被正确保存。 “allowed”正在接收“dayoper”的值,“estimated_value”正在接收权重。换句话说,前两个是对的,后两个是错的。
我编写了最少的工作代码来测试它,我发现了两个问题。
但两者都会生成错误消息,您应该在控制台/终端中看到该消息。
col1, col2, col3, col4 = self.listacli.item(n, 'values')
col12
和 col15
但您需要 col3
和 col4
self.allowed_value.insert(END, col3) # col12
self.estimated_value.insert(END, col4) # col15
完整的工作代码:
#!/usr/bin/env python3
"""
# author: Bartlomiej "furas" Burek (https://blog.furas.pl)
# date: 2023.11.14
# [sqlite - Tkinter Python - Treeview - Stack Overflow](https://stackoverflow.com/questions/77480165/tkinter-python-treeview)
"""
import tkinter as tk
from tkinter import ttk
class Window(tk.Tk):
def __init__(self):
super().__init__()
# ---
self.frame_1 = tk.Frame(self)
self.frame_1.pack(expand=True, fill="both")
tk.Label(self.frame_1, text="Vessel:").grid(row=0, column=0, sticky="e")
self.vessel_entry = tk.Entry(self.frame_1)
self.vessel_entry.grid(row=0, column=1)
tk.Label(self.frame_1, text="Comm:").grid(row=1, column=0, sticky="e")
self.comm_entry = tk.Entry(self.frame_1)
self.comm_entry.grid(row=1, column=1)
tk.Label(self.frame_1, text="Allowed:").grid(row=2, column=0, sticky="e")
self.allowed_value = tk.Entry(self.frame_1)
self.allowed_value.grid(row=2, column=1)
tk.Label(self.frame_1, text="Estimated:").grid(row=3, column=0, sticky="e")
self.estimated_value = tk.Entry(self.frame_1)
self.estimated_value.grid(row=3, column=1)
# ---
self.frame_2 = tk.Frame(self)
self.frame_2.pack(expand=True, fill="both")
self.lista_frame2()
for x in range(1, 6):
self.listacli.insert("", "end", text=f'{x}', values=(f'V{x}', f'C{x}', f'A{x}', f'E{x}') )
def lista_frame2(self):
self.listacli = ttk.Treeview(self.frame_2, height=3,
column=("col1", "col2", "col12", "col15"))
self.listacli.heading("#0", text="")
self.listacli.heading("#1", text="Vessel")
self.listacli.heading("#2", text="Commodity")
self.listacli.heading("#3", text="Allowed")
self.listacli.heading("#4", text="Estimated ($)")
self.listacli.column("#0", width=1)
self.listacli.column("#1", width=50)
self.listacli.column("#2", width=200)
self.listacli.column("#3", width=125)
self.listacli.column("#4", width=125)
#self.listacli.place(relx=0.01, rely=0.1, relwidth=0.95, relheight=0.85)
self.listacli.pack(expand=True, fill='both')
self.listacli.bind('<Double-1>', self.on_double_click)
def on_double_click(self, event): # PEP8: `lower_case_names` for functions
for n in self.listacli.selection():
#col1, col2, col3, col4, col5, col6 = self.listacli.item(n, 'values')
col1, col2, col3, col4 = self.listacli.item(n, 'values')
self.vessel_entry.delete(0, "end")
self.vessel_entry.insert("end", col1)
self.comm_entry.delete(0, "end")
self.comm_entry.insert("end", col2)
self.allowed_value.delete(0, "end")
self.allowed_value.insert("end", col3) # col12
self.estimated_value.delete(0, "end")
self.estimated_value.insert("end", col4) # col15
w = Window()
w.mainloop()