I have a Tkinter GUI that is running a pretty lengthy calculations on the background. I want to keep the window responsive while this is happening and also for the user to see the progress bars of various actions (right now it freezes after a few seconds).
Here's the rough structure of my code (only the relevant bits):
class ImputerGUI:
def __init__(self):
self._create_interface()
self.root.mainloop()
def _create_interface(self):
self.root.geometry("770x790")
self.root.resizable(False, False)
self.root.winfo_toplevel().title("Model encoder")
self._create_frames()
self._create_buttons()
# Other stuff
def _create_buttons(self):
process_data = tk.Button(self.lower, text='Process data',
width=30, bg='#f2fafc',
font=('Helvetica', 12, 'bold'),
justify=tk.CENTER,
command=self._process_data)
process_data.grid(row=0, column=1, pady=10, padx=30, rowspan=2, sticky=tk.N + tk.S)
def _process_data(self):
if self.bmf_filename:
threading.Thread(target=self._convert_bmf_file()).start()
else:
threading.Thread(target=self._load_csv_file()).start()
# Bunch of other calculations
self._save_output()
I had a look at the answers here and tried to use threading
module to spawn calculation tasks in their separate threads so that mainloop()
can continue running. But for some reason this doesn't seem to have any effect on the behaviour.
I've also seen answers to the question here that I can possibly achieve the desired result using after()
but not sure how to apply it to my code.