Reputation:
I've created a little GUI for one of my scripts. All is working well.
When I click on one Button, it launches a big function that is parsing a lot of data from some websites.
But once I've clicked on the Button, the program Freezes until the function is run entirely. All is working fine, but why is my GUI freezing during the execution of the function. I'd like to print a little progress bar, but it's not possible.
Here is part of the program:
self.Button1 = Button(self.MENU, text="IELTS", command=self.My_Command)
self.Button1.grid(row=0, column=0,sticky=W+E)
def My_Command(self):
## HERE WE LAUNCH THE FUNCTION
Module_1.main() # My Big Function from another file
self.Button1.config(text="DONE")
I can't do/print anything durint the execution of Module_1.main() ... the GUI is totally freezed.
The Module_1.main() function is a threaded parser (parsing some data from two websites), it takes generally 2 minutes to be ran. If someone have an idea to be able to interact with the program during the 2 minutes needed for the execution of this function, it would be very helpful.
Upvotes: 9
Views: 10444
Reputation: 386342
Tkinter is single threaded. Screen updates happen on each trip through the event loop. Any time you have a long running command you are preventing the event loop from completing an iteration, thus preventing the processing of events, thus preventing redraws.
Your only solution is a) use a thread for the long running command, b) use a process for the long running command, or c) break the command up into small chunks that each can be run in a few ms so you can run one chunk during subsequent iterations of the event loop. You have one other solution which is to call the update_idletasks
method of a widget periodically, but that's more of a workaround than a fix.
Bear in mind that Tkinter is not thread safe, so using threads requires extra care. You can only call methods on widgets from the main thread, which means other threads must communicate with the main thread via a thread-safe queue.
Upvotes: 7