如何刷新Python TKINTER选项卡中的内容?

2024-09-28 16:20:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个使用Treeview显示MySQL数据的程序。我想用“刷新按钮”更新它的内容,而不是终止并重新运行程序。我尝试了self.destroy()函数,但它关闭了选项卡,而没有重新打开选项卡。还有其他方法可以刷新树视图吗

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)

refresh

editededited2


Tags: textselftreeinittypecolumnwidthrefresh
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执行所有其他方法,依此类推

我目前没有在一个系统前面,所以我希望这段代码可以工作,并且还没有经过测试,请告诉我是否有任何错误和更改

相关问题 更多 >