RBuntu RBuntu - 3 months ago 5
Python Question

What is the simplest way to run a constant loop in a tkinter frame?

I want to run a method in the background of my tkinter frame that will constantly check if certain files exist in a specific folder. As long as the files dont exist, there will be a red

tk.label
that says "Incomplete", and as soon as it detects these specific files, the
tk.label
will turn green and say "Complete".

The problem is that my method only runs when the frame is initialized, which is as soon as the program opens. If these files are added or removed after the program is opened, the method won't realize, and the
tk.label
wont change.

What is the best way to run a constant checker in the background? Preferably one that only runs when the frame is opened. Is it just a neverending
while
loop?

Answer

Define a function that does whatever you want, and have that function schedule itself to be run again in the future. It will run until the program quits.

This example assumes a global variable named root that refers to the root window, but any widget reference will work.

def do_something():
    <your code here>
    root.after(3000, do_something)

Call it once to start it, and then it will run forever

do_some_check()
Comments