简体   繁体   中英

Automatically refreshing a Python script after a certain time

I have a script that takes a trains departure time from a Website and saves the difference between that time and datetime.now in a variable called resp. Resp contains the time until the next train departs.

Then it creates a new window using Tkinter:

class Example(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text=resp)
        label.grid(row=1, column=1)

        self.grid_rowconfigure(1, weight=1)
        self.grid_columnconfigure(1, weight=1)

if __name__ == "__main__":
    root = tk.Tk()
    Example(root).grid(sticky="nsew")
    root.grid_rowconfigure(0, weight=1)
    root.grid_columnconfigure(0, weight=1)
    root.geometry('1920x1080')

    root.mainloop()

and displays resp in that window.

My problem is that the departure time (resp) should always be up to date, which means it would have to refresh resp(which means it would have to run the code that is not included in this question) every second.

Including the whole script in a while True: doesn't work and would probably not be an efficient way of doing it.

Is there a way of doing this?

My Code: https://drive.google.com/open?id=1romGOeUqKa2B-fKQyily6DXDqZTY9xMf

You need to use multithreading. A separate thread can refresh resp every second, while your main thread handles displaying resp .

Tkinter provides a method named after that lets you schedule work to be run in the future. If the work to be performed can execute in under a few hundred milliseconds, you can use this instead of something more complex like multithreading or multiprocessing.

Widgets have a method named configure , which allows you to change attributes of the widget after it has been created. You can use this method to change the data displayed in the widget.

For example, let's assume you have a function called get_resp . Maybe it calls a rest service, maybe it reads a file, it doesn't really matter. If you want to call this function and update the display every 10 seconds, for example, you might do it like this:

class Example(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)
        self.label = tk.Label(self, text=resp)
        ...
    def refresh_display(self):
        # get the new value
        resp = self.get_resp()

        # update the display with the new value
        self.label.configure(text=resp)

        # have this function be called again in 10 seconds
        self.after(10000, self.refresh_display)

If self.get_resp() takes too long, another option would be to create a thread that calls self.get_resp() and then sets a variable with the new value (or pushes it on a thread-safe queue). You could then modify refresh_display to update the variable with whatever is stored in the variable or whatever is in the queue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM