0

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.

Weyla
  • 21
  • 5
  • ***"I tried to refresh the data with the after method"***: You didn't show how you do this, [edit] your question and post what you have per the [mcve] guidelines. – stovfl Mar 11 '20 at 22:36
  • I tried to write down what i remember, if thats not enough, tomorrow after work i will try to put a test code together and give more detail what i tried. – Weyla Mar 11 '20 at 23:03
  • 1
    ***"self.tree.after(1000,self.refresh())"***: One mistake, read [Why is Button parameter “command” executed when declared?](https://stackoverflow.com/questions/5767228/why-is-button-parameter-command-executed-when-declared). Are you aware that you want to refresh the `Treeview` **every 1 Second**. This lead to [use threads to preventing main event loop from “freezing”](https://stackoverflow.com/a/16747734/7414759) – stovfl Mar 11 '20 at 23:30
  • I was aware of the lambda function (used it in buttons, but still not sure how it actually works so i'm gonna check it out, thanks for the link). I found the problem in the other link, thank you for the help :) – Weyla Mar 12 '20 at 16:51

1 Answers1

1

In stovfl's comment i found a comment where they say the after method does not call the function as the 2nd paramter so the () messed my code up. Changed the refresh function to this end everything is working fine.

    def refresh(self):
        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)
Weyla
  • 21
  • 5