【问题标题】:How to refresh the contents within the Python TKINTER Tab?如何刷新 Python TKINTER 选项卡中的内容?
【发布时间】:2021-04-11 21:24:19
【问题描述】:

我有一个使用 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)

【问题讨论】:

  • 你不应该用下面给出的答案来更新问题。

标签: python mysql tkinter refresh mysql-python


【解决方案1】:

你可以使用

for widgets in yourwidgetname.winfo_children():
    widgets.destroy()

并通过调用初始化它们的函数来重新初始化它

您可以通过这种方式销毁框架中的所有小部件,并且不会完全破坏您的框架。它将销毁其子小部件。

【讨论】:

    【解决方案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,因此您需要使用self.tree 声明Treeview,并使用self.tree 执行所有其他方法等等。

    我目前不在系统前面,所以我希望这段代码可以正常工作并且尚未经过测试,如果有任何错误和更改,请告诉我。

    【讨论】:

    • I got this error: tree.delete(*tree.get_children()) # 删除树视图内的所有时间 NameError: name 'tree' is not defined
    • @Daniel 哦,是的,当然,您必须在__init__() 中将树定义为self.tree,然后使用self.tree.delete(....) 等等。
    • 在我将所有内容从树更改为 self.tree 后,它就可以工作了。但我没有更改 __init__() 中的任何内容。
    • 您必须将每个tree 更改为self.tree
    猜你喜欢
    • 2022-09-27
    • 2015-09-09
    • 1970-01-01
    • 1970-01-01
    • 2011-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多