Skip to content
Advertisement

Using Tkinter, Threading and After method

im working on a selenium project using tkinter in python. I want to execute many selenium task at one time(could be up to 100), so I used threading to accomplish this goal, but I came to a problem, due to the fact that I need all individual selenium task to wait a couple seconds between instructions, I used the ‘.after’ method but for some reason my program is still freezing up, I’ve done a lot of research but I cant seem to find the answer yet. Isn’t there a way to use tkinter, threading and some sort of sleeping in python? Do I need to switch to multiprocessing? but I would need all selenium processes to finish within the same 2 minutes and each process takes about a minute.(e.g lets say i start 100, 1 minute processes at 6:00 all task would need to be finished by 6:02)

I created minimal code which mimics my selenium script, so its easier to read, here it is:

JavaScript

Advertisement

Answer

You main problem is that after() and Thread() similar to command= needs only function’s name without () and it will later use () to execute it.

Other problem is that after() doesn’t stop code but it only send information to mainloop to execute function after 2000ms and later Python runs at once next line after after(). You have to add Label in fucntion executed by after()

JavaScript

JavaScript

EDIT: Because gogogo() runs in separated thread so you can also use time.sleep() because it doesn’t block mainloop() in main thread

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement