如何刷新 Python TKINTER Tab 中的内容?
How to refresh the contents within the Python TKINTER Tab?
我有一个程序可以使用 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)
你可以使用
for widgets in yourwidgetname.winfo_children():
widgets.destroy()
并通过调用初始化它们的函数重新初始化它
你可以通过这种方式销毁一个框架中的所有小部件,它不会完全破坏你的框架。它会销毁它的子部件。
尝试再次获取值,然后在删除现有值后插入新值,如:
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
,因此您需要使用 self.tree
声明 Treeview
,并使用 self.tree
执行所有其他方法,依此类推。
我有一个程序可以使用 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)
你可以使用
for widgets in yourwidgetname.winfo_children():
widgets.destroy()
并通过调用初始化它们的函数重新初始化它
你可以通过这种方式销毁一个框架中的所有小部件,它不会完全破坏你的框架。它会销毁它的子部件。
尝试再次获取值,然后在删除现有值后插入新值,如:
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
,因此您需要使用 self.tree
声明 Treeview
,并使用 self.tree
执行所有其他方法,依此类推。