I setup a multiple page tkinter app gui that looks like this:
class App(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
tk.Tk.iconbitmap(self, "asd")
tk.Tk.wm_title(self, "asd")
self.title_font = tkfont.Font(family='Helvetica', size=18, weight="bold", slant="italic")
# Creates a root frame that will be used as a parent frame for the other frames
root = tk.Frame(self)
root.pack(side="bottom", fill="both", expand=True)
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (asd1):
page_name = F.__name__
frame = F(parent=root, controller=self)
self.frames[page_name] = frame
# put all of the pages in the same location;
# the one on the top of the stacking order
# will be the one that is visible.
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame("asd1")
def show_frame(self, page_name):
"""Show a frame for the given page name"""
frame = self.frames[page_name]
frame.tkraise()
class asd1(tk.Frame):
def refresh(self):
#dt.prints just searches in a mysql database and returns a nested array
array = dt.prints(mydb)
data_size = len(array)
self.tree.delete(*self.tree.get_children()
self.tree.insert("", 0, values=(array[random.randrange(0, data_size)]))
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
+extra code where treeview is placed
I tried to refresh the data with the after method, but i cant figure out how to do that.
With the methods i found on the internet it only loaded the treeview at startup or froze the app at start. if i just call the refresh function with a button it works fine. Is there a way to automatically refresh the data every x second?
edit:
I tried many ways: calling it outside the class like this:
if __name__ == "__main__":
app = RaktarApp()
app.geometry("1280x720")
app.after(1000,app.refresh())
app.mainloop()
calling it after the i packed the treeview
treeview.after(1000,refresh())
On some pages i found that it should be in the refresh function too, but that just froze the window
def refresh(self):
#dt.prints just searches in a mysql database and returns a nested array
array = dt.prints(mydb)
data_size = len(array)
self.tree.delete(*self.tree.get_children()
self.tree.insert("", 0, values=(array[random.randrange(0, data_size)]))
self.tree.after(1000,self.refresh())
I also tried placing the refresh function in tha main class.