Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

mysql - How to refresh the contents within the Python TKINTER Tab?

I have a program that displays some data from MySQL using Treeview. I would like to update the contents of it using a "refresh button" instead of terminating and re-running the program again. I have tried self.destroy() function, but it closes the tab and doesn't reopen the tab. Are there other ways to just refresh the 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)

refresh

edited edited2


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try to fetch the values again and then insert the new values after deleting the existing values, like:

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  

Note that I used self.tree, so you need to declare the Treeview with self.tree and do all other methods with self.tree and so on.

I am not currently in front of a system, so I hope this code works and is not tested yet, let me know for any mistakes and changes.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...