Home > database >  How to reload part of the code in tkinter many times?
How to reload part of the code in tkinter many times?

Time:09-05

In the code below, a part of the main code is placed. I want this file, which is updated at different times, to be updated in tekinter, but the problem is that my UI is not updated until it is closed and opened.

style = ttk.Style(GLabel_820)
style.theme_use('clam')
style.configure("mystyle.Treeview", highlightthickness=0,bd=0,font=('Far_Nazanin', 11))
style.configure("mystyle.Treeview.Heading", font=('Far_Nazanin', 15)) 
style.layout("mystyle.Treeview", [('mystyle.Treeview.treearea', {'sticky': 'nswe'})])

tree = ttk.Treeview(GLabel_820)
tedadtardod=0      
filename = 'output/pelak1.xlsx'
filename = r"{}".format(filename)
df = pd.read_excel(filename,engine='openpyxl')
tedadkhat=len(df)
tree["column"] = list(df.columns)
tree["show"] = "headings"

for col in tree["column"]:
    tree.heading(col, text=col)
    df_rows = df.to_numpy().tolist()
    for row in df_rows:
        tedadtardod=tedadtardod 1
        if tedadtardod>tedadkhat-10:
            tree.insert("",0,values=row)

CodePudding user response:

Have you tried placing that part of the code in a loop. You can use a while loop or a for loop.

CodePudding user response:

Well, with my research I managed to solve my problem, so I'm posting the result here for others to use

def reload(self):
    while True :
        try :
            gf = pd.read_excel('output/pelak1.xlsx',engine='openpyxl')
            break
        except EOFError as e:
            time.sleep(1)
    NewPelakLength=len(gf)
    global LastPelakLength
    global toc
    tic = time.perf_counter()
    elap=tic-toc
    if NewPelakLength != LastPelakLength:# or elap>10
        toc = time.perf_counter()
        LastPelakLength=len(gf)
        self.pelak_list()
    self.after(6000, self.reload)
  • Related