Ive been looking to convert one of my programs to include a GUI, and am having issues with the timing aspects of it. Ive included a mock scenario of what my problem is below:
Ive defined my GUI as an object via a class, and am using the buttons to call other methods in the class. These methods used to use the time.sleep() function to wait a certain time before completing a task, and am now using the root.after method instead. However, once the button is pressed and the method with the delay is caused, (delay 10s), the GUI hangs until this method is finished, and thus the "stop" and any other buttons are useless. Considering that this method will eventually be calling itself in a type of recursion, this would mean the GUI would be "frozen" untill the program is finished with no option to interrupt of change settings. Would there be a way to circumvent this?
class App: def __init__(self, master): self.master = master # start BUTTON self.start_button = Button(master, text="Start", command=lambda: self.test(), height=2, width=30, fg='blue') # STOP BUTTON self.stop_button = Button(master, text="Stop", command=lambda: self.standby('STOP'), height=2, width=25, fg='orange') # QUIT BUTTON self.quit_button = Button(master, text="Quit", command=self.quit, height=2, width=25, fg='red') ######LAYOUT##### self.start_button.grid(row=2, column=0, columnspan=2) self.stop_button.grid(row=2, column=2, columnspan=1) self.quit_button.grid(row=2, column=3, columnspan=1) def standby(self, text): print(text) def quit(self): print('QUIT') self.master.destroy() def test(self): print('start') root.after(10000) #issue is here, Id like to have it wait 10sec and not freeze the entire GUI print('10sec') return root = Tk() my_gui = App(root) root.mainloop()
Ultimately, Im looking for a way to be able to induce a 10-15s delay inside the methods while still being able to use the other buttons, ie stop and quit, aswell as radiobuttons and things in the future.
Many thanks in advance