如何将 python Tkinter 项目中的列表框视图更改为树视图
How do I change a listbox view in my python Tkinter project to a tree view
我尝试使用树视图显示我的项目输出数据,但在所有按钮点击中始终出现以下错误
然后我切换到列表框视图,我能够毫无错误地执行我的项目,显示所有数据,滚动条工作并且 select 工作完美。
我想如何使用树视图并使其与 selection 和滚动条一起使用。
如果有人可以协助更正我的代码以改用树视图并指出我的错误。我已经评论了树视图代码。
from tkinter import *
from app_backend import Database
from tkinter import ttk
class Database:
def __init__(self):
self.conn = pymysql.connect(host="localhost",user="usrname",
password="",database="database",connect_timeout=10)
self.cur = self.conn.cursor()
self.cur.execute("SELECT * FROM property_damage_claim")
self.conn.ping(reconnect=True)
#we will leave the database open here.
def insert(self,clf_name,cll_name,contact,policy,Insurance,in_contact,status):
self.conn.ping(reconnect=True)
self.cur.execute("INSERT INTO property_damage_claim VALUES (
NULL,%s,%s,%s,%s,%s,%s,%s)",(clf_name,cll_name,contact,policy,
Insurance,in_contact,status))
self.conn.commit()
def view(self):
self.conn.ping(reconnect=True)
self.cur.execute("SELECT * FROM property_damage_claim")
rows = self.cur.fetchall()
return rows
def search(self,clf_name ='',cll_name ='',contact ='',policy='',Insurance='',in_contact='',status=''):
self.conn.ping(reconnect=True)
self.cur.execute("SELECT * FROM property_damage_claim WHERE clf_name=%s OR
cll_name=%s OR contact=%s OR policy=%s OR Insurance=%s OR in_contact=%s OR
status=%s",(clf_name,cll_name,contact,policy,Insurance,in_contact,status))
rows = self.cur.fetchall()
return rows
def delete(self,id):
self.conn.ping(reconnect=True)
self.cur.execute("DELETE FROM property_damage_claim WHERE id=%s",(id,))
self.conn.commit()
def update(self,id,clf_name,cll_name,contact,policy,Insurance,in_contact,status):
self.conn.ping(reconnect=True)
self.cur.execute("UPDATE property_damage_claim SET clf_name=%s, cll_name=%s, contact=%s,
policy=%s, Insurance=%s, in_contact=%s, status=%s WHERE id=%s",(clf_name,cll_name,
contact,policy,Insurance,in_contact,status,id))
self.conn.commit()
def __del__(self):
self.conn.close()
db = Database()
def selected_row(event):
try:
global selected_content
index = student_list.curselection()[0]
selected_content = student_list.get(index)
e1.delete(0,END)
e1.insert(END,selected_content[1])
e2.delete(0,END)
e2.insert(END,selected_content[2])
e3.delete(0,END)
e3.insert(END,selected_content[3])
e4.delete(0,END)
e4.insert(END,selected_content[4])
e5.delete(0,END)
e5.insert(END,selected_content[5])
e6.delete(0,END)
e6.insert(END,selected_content[6])
e7.delete(0,END)
e7.insert(END,selected_content[7])
except IndexError:
pass
# connecting the backend functions
def view_all():
student_list.delete(0,END)
for row in db.view():
student_list.insert(END,row)
def search_record():
student_list.delete(0,END)
for row in db.search(clf_name.get(),cll_name.get(),contact.get(),
policy.get(),Insurance.get(),in_contact.get(),status.get()):
student_list.insert(END,row)
def add_record():
try:
if cll_name.get():
db.insert(clf_name.get(),cll_name.get(),contact.get(),policy.get(),
Insurance.get(),in_contact.get(),status.get())
student_list.delete(0,END)
student_list.insert(END,(clf_name.get(),cll_name.get(),contact.get(),
policy.get(),Insurance.get(),in_contact.get(),status.get()))
e1.delete(0,END)
e2.delete(0,END)
e3.delete(0,END)
e4.delete(0,END)
e5.delete(0,END)
e6.delete(0,END)
e7.delete(0,END)
except:
print("tcl Error")
def delete_record():
db.delete(selected_content[0])
e1.delete(0,END)
e2.delete(0,END)
e3.delete(0,END)
e4.delete(0,END)
e5.delete(0,END)
e6.delete(0,END)
e7.delete(0,END)
view_all()
def update_record():
db.update(selected_content[0],clf_name.get(),cll_name.get(),
contact.get(),policy.get(),Insurance.get(),in_contact.get(),status.get())
view_all()
def new_record():
e1.delete(0,END)
e2.delete(0,END)
e3.delete(0,END)
e4.delete(0,END)
e5.delete(0,END)
e6.delete(0,END)
e7.delete(0,END)
window = Tk()
window.geometry("1000x2000")
bg = PhotoImage(file = "bgg.gif")
label1 = Label( window, image = bg)
label1.place(x = 0, y = 0)
window.wm_title("Goodwill Brokers")
wr1=LabelFrame(window, text="")
wr2=LabelFrame(window, text="Client Data")
wr3=LabelFrame(window, text="Search")
wr4=LabelFrame(window, text="Client List")
wr1.pack(fill="both", expand="no", padx="20", pady="10")
wr2.pack(fill="both", expand="no", padx="20", pady="10")
wr3.pack(fill="both", expand="no", padx="20", pady="10")
wr4.pack(fill="both", expand="no", padx="20", pady="10")
# GUI Components
# Labels
lb_title = ttk.Label(wr1,text="Goodwill Brokers".upper())
lb_title.grid(row=0,column=0,columnspan=4,pady=15)
lb_date = ttk.Label(wr2,text="First Name:")
lb_date.grid(row=4,column=0,sticky="e")
lb_sname = ttk.Label(wr2,text="Last Name:")
lb_sname.grid(row=5,column=0,sticky="e")
lb_grade = ttk.Label(wr2,text="Contact:")
lb_grade.grid(row=6,column=0,sticky="e")
lb_class = ttk.Label(wr2,text="Policy:")
lb_class.grid(row=7,column=0,sticky="e")
lb_pname = ttk.Label(wr2,text="Insurance:")
lb_pname.grid(row=4,column=2,sticky="e")
lb_psign = ttk.Label(wr2,text="Contact Person/Entity:")
lb_psign.grid(row=5,column=2,sticky="e")
lb_cin = ttk.Label(wr2,text="Status:")
lb_cin.grid(row=6,column=2,sticky="e")
# Entries
clf_name = StringVar()
e1 = ttk.Entry(wr2,textvariable= clf_name)
e1.grid(row =4,column=1)
cll_name = StringVar()
e2 = ttk.Entry(wr2,textvariable= cll_name)
e2.grid(row =5,column=1)
contact = StringVar()
e3 = ttk.Entry(wr2,textvariable= contact)
e3.grid(row =6,column=1)
policy = StringVar()
e4 = ttk.Entry(wr2,textvariable= policy)
e4.grid(row =7,column=1)
Insurance = StringVar()
e5 = ttk.Entry(wr2,textvariable= Insurance)
e5.grid(row =4,column=3)
in_contact = StringVar()
e6 = ttk.Entry(wr2,textvariable= in_contact)
e6.grid(row =5,column=3)
status = StringVar()
e7 = ttk.Entry(wr2,textvariable= status)
e7.grid(row =6,column=3)
# Buttons
b1 = ttk.Button(wr3,text="View All", width=23,command=view_all)
b1.grid(row=8,column=0,columnspan=2,sticky="e",padx=10,pady=6)
b2 = ttk.Button(wr3,text="Search", width=23,command=search_record)
b2.grid(row=8,column=2,columnspan=2,sticky="w",padx=10,pady=6)
b3 = ttk.Button(wr3,text="Add", width=23,command=add_record)
b3.grid(row=9,column=0,columnspan=2,sticky="e",padx=10,pady=6)
b4 = ttk.Button(wr3,text="Update", width=23,command=update_record)
b4.grid(row=9,column=2,columnspan=2,sticky="w",padx=10,pady=6)
b5 = ttk.Button(wr3,text="Delete", width=23,command=delete_record)
b5.grid(row=10,column=0,columnspan=2,sticky="e",padx=10,pady=6)
b6 = ttk.Button(wr3,text="Close", width=23,command=window.destroy)
b6.grid(row=11,column=0,columnspan=2,sticky=E,padx=10,pady=6)
b7 = ttk.Button(wr3,text="Clear", width=23,command=new_record)
b7.grid(row=10,column=2,columnspan=2,sticky="w",padx=10,pady=6)
# Listbox and scrollbar
student_list= Listbox(wr4,height=6,width=60)
student_list.grid(row=12,column=0,rowspan=6,columnspan=3,sticky=E,padx=10,pady=10)
''' student_list = ttk.Treeview(wr4, columns=(1,2,3,4,5,6,7), show='headings')
student_list.grid()
student_list.column("#1", anchor=CENTER)
student_list.heading("#1", text="ID")
student_list.column("#2", anchor=CENTER)
student_list.heading("#2", text="FNAME")
student_list.column("#3", anchor=CENTER)
student_list.heading("#3", text="LNAME")
student_list.column("#4", anchor=CENTER)
student_list.heading("#4", text="LNAME")
student_list.column("#5", anchor=CENTER)
student_list.heading("#5", text="LNAME")
student_list.column("#6", anchor=CENTER)
student_list.heading("#6", text="LNAME")
student_list.column("#7", anchor=CENTER)
student_list.heading("#7", text="LNAME")
'''
sb1 = Scrollbar(wr4)
sb1.grid(row=12,column=3,rowspan=6,sticky=W)
student_list.configure(yscrollcommand=sb1.set)
sb1.configure(command=student_list.yview)
# get the selected row from Listox to use Delete and Update Commands
student_list.bind('<<ListboxSelect>>',selected_row)
window.resizable(0,0)
window.mainloop()
所以我在多处研究后发现了这一点。您需要先创建下面的树视图
创建树视图滚动条
tree_scroll = Scrollbar(wr4)
tree_scroll.grid(row=12,column=3,rowspan=6,sticky=W)
# Create The Treeview
student_list = ttk.Treeview(wr4, yscrollcommand=tree_scroll.set,
selectmode="extended",show='headings')
student_list.grid()
# Configure the Scrollbar
tree_scroll.config(command=student_list.yview)
# Define Our Columns
student_list['columns'] = ("ID", "F_Name", "L_Name", "Contact",
"Policy", "Insurance", "Contact P/E", "Status")
# Format Our Columns
student_list.column("#0", width=0, stretch=NO)
student_list.column("ID", anchor=W, width=50)
student_list.column("F_Name", anchor=W, width=80)
student_list.column("L_Name", anchor=CENTER, width=80)
student_list.column("Contact", anchor=CENTER, width=120)
student_list.column("Policy", anchor=CENTER, width=120)
student_list.column("Insurance", anchor=CENTER, width=120)
student_list.column("Contact P/E", anchor=CENTER, width=120)
student_list.column("Status", anchor=CENTER, width=150)
# Create Headings
student_list.heading("#0", text="", anchor=W)
student_list.heading("ID", text="ID", anchor=W)
student_list.heading("F_Name", text="F_Name", anchor=W)
student_list.heading("L_Name", text="L_Name", anchor=CENTER)
student_list.heading("Contact", text="Contact", anchor=CENTER)
student_list.heading("Policy", text="Policy", anchor=CENTER)
student_list.heading("Insurance", text="Insurance", anchor=CENTER)
student_list.heading("Contact P/E", text="Contact P/E", anchor=CENTER)
student_list.heading("Status", text="Status", anchor=CENTER)
然后使用循环创建一个函数来替换它。
def view_all():
for record in student_list.get_children():
student_list.delete(record)
for row in db.view():
student_list.insert(parent='', index='end', text='', values=.
(row[0], row[1], row[2], row[3], row[4], row[5], row[6],
row[7]))
调用数据库class
class Database:
def view(self):
self.conn.ping(reconnect=True)
self.cur.execute("SELECT * FROM property_damage_claim")
rows = self.cur.fetchall()
return rows
我尝试使用树视图显示我的项目输出数据,但在所有按钮点击中始终出现以下错误
然后我切换到列表框视图,我能够毫无错误地执行我的项目,显示所有数据,滚动条工作并且 select 工作完美。
我想如何使用树视图并使其与 selection 和滚动条一起使用。 如果有人可以协助更正我的代码以改用树视图并指出我的错误。我已经评论了树视图代码。
from tkinter import *
from app_backend import Database
from tkinter import ttk
class Database:
def __init__(self):
self.conn = pymysql.connect(host="localhost",user="usrname",
password="",database="database",connect_timeout=10)
self.cur = self.conn.cursor()
self.cur.execute("SELECT * FROM property_damage_claim")
self.conn.ping(reconnect=True)
#we will leave the database open here.
def insert(self,clf_name,cll_name,contact,policy,Insurance,in_contact,status):
self.conn.ping(reconnect=True)
self.cur.execute("INSERT INTO property_damage_claim VALUES (
NULL,%s,%s,%s,%s,%s,%s,%s)",(clf_name,cll_name,contact,policy,
Insurance,in_contact,status))
self.conn.commit()
def view(self):
self.conn.ping(reconnect=True)
self.cur.execute("SELECT * FROM property_damage_claim")
rows = self.cur.fetchall()
return rows
def search(self,clf_name ='',cll_name ='',contact ='',policy='',Insurance='',in_contact='',status=''):
self.conn.ping(reconnect=True)
self.cur.execute("SELECT * FROM property_damage_claim WHERE clf_name=%s OR
cll_name=%s OR contact=%s OR policy=%s OR Insurance=%s OR in_contact=%s OR
status=%s",(clf_name,cll_name,contact,policy,Insurance,in_contact,status))
rows = self.cur.fetchall()
return rows
def delete(self,id):
self.conn.ping(reconnect=True)
self.cur.execute("DELETE FROM property_damage_claim WHERE id=%s",(id,))
self.conn.commit()
def update(self,id,clf_name,cll_name,contact,policy,Insurance,in_contact,status):
self.conn.ping(reconnect=True)
self.cur.execute("UPDATE property_damage_claim SET clf_name=%s, cll_name=%s, contact=%s,
policy=%s, Insurance=%s, in_contact=%s, status=%s WHERE id=%s",(clf_name,cll_name,
contact,policy,Insurance,in_contact,status,id))
self.conn.commit()
def __del__(self):
self.conn.close()
db = Database()
def selected_row(event):
try:
global selected_content
index = student_list.curselection()[0]
selected_content = student_list.get(index)
e1.delete(0,END)
e1.insert(END,selected_content[1])
e2.delete(0,END)
e2.insert(END,selected_content[2])
e3.delete(0,END)
e3.insert(END,selected_content[3])
e4.delete(0,END)
e4.insert(END,selected_content[4])
e5.delete(0,END)
e5.insert(END,selected_content[5])
e6.delete(0,END)
e6.insert(END,selected_content[6])
e7.delete(0,END)
e7.insert(END,selected_content[7])
except IndexError:
pass
# connecting the backend functions
def view_all():
student_list.delete(0,END)
for row in db.view():
student_list.insert(END,row)
def search_record():
student_list.delete(0,END)
for row in db.search(clf_name.get(),cll_name.get(),contact.get(),
policy.get(),Insurance.get(),in_contact.get(),status.get()):
student_list.insert(END,row)
def add_record():
try:
if cll_name.get():
db.insert(clf_name.get(),cll_name.get(),contact.get(),policy.get(),
Insurance.get(),in_contact.get(),status.get())
student_list.delete(0,END)
student_list.insert(END,(clf_name.get(),cll_name.get(),contact.get(),
policy.get(),Insurance.get(),in_contact.get(),status.get()))
e1.delete(0,END)
e2.delete(0,END)
e3.delete(0,END)
e4.delete(0,END)
e5.delete(0,END)
e6.delete(0,END)
e7.delete(0,END)
except:
print("tcl Error")
def delete_record():
db.delete(selected_content[0])
e1.delete(0,END)
e2.delete(0,END)
e3.delete(0,END)
e4.delete(0,END)
e5.delete(0,END)
e6.delete(0,END)
e7.delete(0,END)
view_all()
def update_record():
db.update(selected_content[0],clf_name.get(),cll_name.get(),
contact.get(),policy.get(),Insurance.get(),in_contact.get(),status.get())
view_all()
def new_record():
e1.delete(0,END)
e2.delete(0,END)
e3.delete(0,END)
e4.delete(0,END)
e5.delete(0,END)
e6.delete(0,END)
e7.delete(0,END)
window = Tk()
window.geometry("1000x2000")
bg = PhotoImage(file = "bgg.gif")
label1 = Label( window, image = bg)
label1.place(x = 0, y = 0)
window.wm_title("Goodwill Brokers")
wr1=LabelFrame(window, text="")
wr2=LabelFrame(window, text="Client Data")
wr3=LabelFrame(window, text="Search")
wr4=LabelFrame(window, text="Client List")
wr1.pack(fill="both", expand="no", padx="20", pady="10")
wr2.pack(fill="both", expand="no", padx="20", pady="10")
wr3.pack(fill="both", expand="no", padx="20", pady="10")
wr4.pack(fill="both", expand="no", padx="20", pady="10")
# GUI Components
# Labels
lb_title = ttk.Label(wr1,text="Goodwill Brokers".upper())
lb_title.grid(row=0,column=0,columnspan=4,pady=15)
lb_date = ttk.Label(wr2,text="First Name:")
lb_date.grid(row=4,column=0,sticky="e")
lb_sname = ttk.Label(wr2,text="Last Name:")
lb_sname.grid(row=5,column=0,sticky="e")
lb_grade = ttk.Label(wr2,text="Contact:")
lb_grade.grid(row=6,column=0,sticky="e")
lb_class = ttk.Label(wr2,text="Policy:")
lb_class.grid(row=7,column=0,sticky="e")
lb_pname = ttk.Label(wr2,text="Insurance:")
lb_pname.grid(row=4,column=2,sticky="e")
lb_psign = ttk.Label(wr2,text="Contact Person/Entity:")
lb_psign.grid(row=5,column=2,sticky="e")
lb_cin = ttk.Label(wr2,text="Status:")
lb_cin.grid(row=6,column=2,sticky="e")
# Entries
clf_name = StringVar()
e1 = ttk.Entry(wr2,textvariable= clf_name)
e1.grid(row =4,column=1)
cll_name = StringVar()
e2 = ttk.Entry(wr2,textvariable= cll_name)
e2.grid(row =5,column=1)
contact = StringVar()
e3 = ttk.Entry(wr2,textvariable= contact)
e3.grid(row =6,column=1)
policy = StringVar()
e4 = ttk.Entry(wr2,textvariable= policy)
e4.grid(row =7,column=1)
Insurance = StringVar()
e5 = ttk.Entry(wr2,textvariable= Insurance)
e5.grid(row =4,column=3)
in_contact = StringVar()
e6 = ttk.Entry(wr2,textvariable= in_contact)
e6.grid(row =5,column=3)
status = StringVar()
e7 = ttk.Entry(wr2,textvariable= status)
e7.grid(row =6,column=3)
# Buttons
b1 = ttk.Button(wr3,text="View All", width=23,command=view_all)
b1.grid(row=8,column=0,columnspan=2,sticky="e",padx=10,pady=6)
b2 = ttk.Button(wr3,text="Search", width=23,command=search_record)
b2.grid(row=8,column=2,columnspan=2,sticky="w",padx=10,pady=6)
b3 = ttk.Button(wr3,text="Add", width=23,command=add_record)
b3.grid(row=9,column=0,columnspan=2,sticky="e",padx=10,pady=6)
b4 = ttk.Button(wr3,text="Update", width=23,command=update_record)
b4.grid(row=9,column=2,columnspan=2,sticky="w",padx=10,pady=6)
b5 = ttk.Button(wr3,text="Delete", width=23,command=delete_record)
b5.grid(row=10,column=0,columnspan=2,sticky="e",padx=10,pady=6)
b6 = ttk.Button(wr3,text="Close", width=23,command=window.destroy)
b6.grid(row=11,column=0,columnspan=2,sticky=E,padx=10,pady=6)
b7 = ttk.Button(wr3,text="Clear", width=23,command=new_record)
b7.grid(row=10,column=2,columnspan=2,sticky="w",padx=10,pady=6)
# Listbox and scrollbar
student_list= Listbox(wr4,height=6,width=60)
student_list.grid(row=12,column=0,rowspan=6,columnspan=3,sticky=E,padx=10,pady=10)
''' student_list = ttk.Treeview(wr4, columns=(1,2,3,4,5,6,7), show='headings')
student_list.grid()
student_list.column("#1", anchor=CENTER)
student_list.heading("#1", text="ID")
student_list.column("#2", anchor=CENTER)
student_list.heading("#2", text="FNAME")
student_list.column("#3", anchor=CENTER)
student_list.heading("#3", text="LNAME")
student_list.column("#4", anchor=CENTER)
student_list.heading("#4", text="LNAME")
student_list.column("#5", anchor=CENTER)
student_list.heading("#5", text="LNAME")
student_list.column("#6", anchor=CENTER)
student_list.heading("#6", text="LNAME")
student_list.column("#7", anchor=CENTER)
student_list.heading("#7", text="LNAME")
'''
sb1 = Scrollbar(wr4)
sb1.grid(row=12,column=3,rowspan=6,sticky=W)
student_list.configure(yscrollcommand=sb1.set)
sb1.configure(command=student_list.yview)
# get the selected row from Listox to use Delete and Update Commands
student_list.bind('<<ListboxSelect>>',selected_row)
window.resizable(0,0)
window.mainloop()
所以我在多处研究后发现了这一点。您需要先创建下面的树视图
创建树视图滚动条
tree_scroll = Scrollbar(wr4)
tree_scroll.grid(row=12,column=3,rowspan=6,sticky=W)
# Create The Treeview
student_list = ttk.Treeview(wr4, yscrollcommand=tree_scroll.set,
selectmode="extended",show='headings')
student_list.grid()
# Configure the Scrollbar
tree_scroll.config(command=student_list.yview)
# Define Our Columns
student_list['columns'] = ("ID", "F_Name", "L_Name", "Contact",
"Policy", "Insurance", "Contact P/E", "Status")
# Format Our Columns
student_list.column("#0", width=0, stretch=NO)
student_list.column("ID", anchor=W, width=50)
student_list.column("F_Name", anchor=W, width=80)
student_list.column("L_Name", anchor=CENTER, width=80)
student_list.column("Contact", anchor=CENTER, width=120)
student_list.column("Policy", anchor=CENTER, width=120)
student_list.column("Insurance", anchor=CENTER, width=120)
student_list.column("Contact P/E", anchor=CENTER, width=120)
student_list.column("Status", anchor=CENTER, width=150)
# Create Headings
student_list.heading("#0", text="", anchor=W)
student_list.heading("ID", text="ID", anchor=W)
student_list.heading("F_Name", text="F_Name", anchor=W)
student_list.heading("L_Name", text="L_Name", anchor=CENTER)
student_list.heading("Contact", text="Contact", anchor=CENTER)
student_list.heading("Policy", text="Policy", anchor=CENTER)
student_list.heading("Insurance", text="Insurance", anchor=CENTER)
student_list.heading("Contact P/E", text="Contact P/E", anchor=CENTER)
student_list.heading("Status", text="Status", anchor=CENTER)
然后使用循环创建一个函数来替换它。
def view_all():
for record in student_list.get_children():
student_list.delete(record)
for row in db.view():
student_list.insert(parent='', index='end', text='', values=.
(row[0], row[1], row[2], row[3], row[4], row[5], row[6],
row[7]))
调用数据库class
class Database:
def view(self):
self.conn.ping(reconnect=True)
self.cur.execute("SELECT * FROM property_damage_claim")
rows = self.cur.fetchall()
return rows