Home > OS >  Append data to a dataframe using root.after() from tkinter
Append data to a dataframe using root.after() from tkinter

Time:07-22

first I'm going to explain what my script does, and then problem. I'm trying to automatize a task every 5 mins. This task involves pandas Tkinter and Matplotlib. I'll attach some guide code of mine to help understand this. First, I do some big task to initialize software programs (petroleum ones) to open files and then work with them. Second, I create a Treeview window and a plot window from Tkinter, then I need them to be updated every 5 mins, the Treeview is updated as expected, but the main problem is that I can't update or append some data to an empty dataframe;which is generated in every loop, need this new data to update plot every5 mins. I tried with append like in code, but it's not working, thanks in advance people.

import pandas
import tkinter as tk
## big task here
#create an empty dataframe
dfoil = pd.DataFrame(columns=[['Date','Oil Rate Cal','Oil Rate Mesu']])

root=tk.Tk
def update_item(df,df0,df01,df02):
    #where df,df0,df01,df02 are dataframes are updated and are working correctly
    #another big task here where i can get the desire results and i can see a treeview updating every 5 mins
    #.........
    
    #.........
    #time2 comes from a working dataframe
    dfoil.append({'Date':time2, 'Oil Rate Cal':dff1.iat[3,11],'Oil Rate Mesu':dff1.iat[3,12]},ignore_index=True)
    root.after(1000*60*5, update_item, df,df0,df01,df02)
update_item(my_df,raiserdf,separetordf,compressorsdf)    
root.mainloop

dfoil is the dataframe that is always getting empty after everyloop

CodePudding user response:

You need to add inside the function:

global dfoil

This will make the dataframe global and 'exist' outside the function

Another option is at the end of the function:

return dfoil

and change the line that calls the function to:

dfoil = update_item(my_df,raiserdf,separetordf,compressorsdf) 
  • Related