我有一个程序可以使用Treeview从MySQL显示一些数据。我想使用“刷新按钮”更新它的内容,而不是终止并重新运行该程序。我已经尝试过self.destroy()函数,但是它关闭了选项卡,并且没有重新打开选项卡。还有其他方法可以刷新Treeview吗?
class tab_four(Frame):
def __init__(self, *args, **kwargs):
Frame.__init__(self, *args, **kwargs)
...
#tree.heading("#0", text = " ")
tree.heading("1", text = "Equipment Type")
tree.heading("2", text = "Total")
tree.heading("3", text = "Unavailable")
tree.heading("4", text = "Available")
#tree.column('#0', stretch = NO, minwidth = 0, width = 0)
tree.column('#1', stretch = YES, minwidth = 0, width = 100, anchor = CENTER)
tree.column('#2', stretch = YES, minwidth = 0, width = 100, anchor = CENTER)
tree.column('#3', stretch = YES, minwidth = 0, width = 100, anchor = CENTER)
tree.column('#4', stretch = YES, minwidth = 0, width = 100, anchor = CENTER)
command = "SELECT et.`Equipment Type`, COUNT(i.`Equipment Type`) Total, SUM(IF(`Status`='Unavailable',1,0)) Unavailable, SUM(IF(`Status`='Available',1,0)) Available FROM `Equipment Types` et LEFT JOIN Inventory i ON et.`Equipment Type` = i.`Equipment Type` GROUP BY 1 ORDER BY 1"
mycursor.execute(command)
display = mycursor.fetchall()
for row in display:
tree.insert("", "end", values=row)
def refresh_clicked(self):
self.destroy()
self.__init__()
button_refresh = tk.Button(topframe, text = "Refresh", state = NORMAL, command = self.refresh_clicked)
button_refresh.grid(row = 1, column = 2, sticky = W, padx = 5, pady = 2)
尝试再次获取值,然后在删除现有值后插入新值,例如:
def refresh_clicked(self):
command = """SELECT et.`Equipment Type`, COUNT(i.`Equipment Type`) Total, SUM(IF(`Status`='Unavailable',1,0)) Unavailable, SUM(IF(`Status`='Available',1,0)) Available FROM `Equipment Types` et LEFT JOIN Inventory i ON et.`Equipment Type` = i.`Equipment Type` GROUP BY 1 ORDER BY 1"""
mycursor.execute(command)
display = mycursor.fetchall()
self.tree.delete(*self.tree.get_children()) # Delete all times inside the treeview
for row in display:
self.tree.insert('','end',value=row) # Insert newly fetched values
请注意,我使用了self.tree
,因此您需要声明Treeview
withself.tree
并使用等执行所有其他方法self.tree
。
我目前不在系统前面,所以希望该代码有效并且尚未测试,请告知我任何错误和更改。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句