如何在使用类时自动刷新tkinter中的数据?

2024-04-19 10:26:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我设置了一个多页tkinter应用程序gui,如下所示:

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

我试图用after方法刷新数据,但我不知道怎么做

使用我在互联网上找到的方法,它只在启动时加载treeview或在启动时冻结应用程序。如果我只是用一个按钮调用刷新函数,它就可以正常工作。 有没有办法每x秒自动刷新一次数据

编辑:

我尝试了很多方法:在课堂外这样称呼它:

if __name__ == "__main__":
    app = RaktarApp()
    app.geometry("1280x720")
    app.after(1000,app.refresh())
    app.mainloop()

在我收拾好树景后叫它

treeview.after(1000,refresh())

在一些页面上,我发现它也应该在刷新功能中,但这只是冻结了窗口

  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())

我还尝试在主类中放置refresh函数


Tags: thenameselftreeappsizeinitdef
1条回答
网友
1楼 · 发布于 2024-04-19 10:26:06

在stovfl的注释中,我发现了一条注释,他们说after方法没有将函数作为第二个参数调用,因此()将我的代码弄乱了。 为此更改了刷新功能,一切正常

    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)

相关问题 更多 >