Hello. I have a complex program running in Python and I want to be able to interrupt it and restart the mainloop at any time I want.
I have made a simplified example of my program which illustrates my problem:
https://ibb.co/djMd5TW
Can someone help me understand what needs to happen in Restart function to get back to idle state in my mainloop
I have made a simplified example of my program which illustrates my problem:
import tkinter as tk from tkinter import Button,Entry,Canvas,Label,ttk class Application(tk.Frame): def __init__(self,master=None): super().__init__(master) self.master = master def Create_canvas(self,canvas_width,canvas_height): global canvas#described as global because used outside class canvas = tk.Canvas(self.master,bg='papaya whip',width=canvas_width,height=canvas_height) def Application_Intro(self): print("starting new app") restart_program_button = tk.Button(canvas, text="Restart_program",font='Helvetica 12 bold', width=20, height=2, command =self.Restart) start_program_button = tk.Button(canvas, text="Start_program",font='Helvetica 12 bold', width=20, height=2, command =self.Start_program) canvas.create_text(960,20,text="MY PROGRAM",font='Helvetica 16 bold') canvas.create_window(710,300,window = restart_program_button) canvas.create_window(710,500,window = start_program_button) canvas.pack() master.mainloop() def Start_program(self): print("Program start") master.after(1000,self.Start_program) def Restart(self): print("HERE I WANT TO INTERRUPT START PROGRAM AND RETURN TO IDLE STATE") #WHAT TO DO IN THIS FUNCTION TO GO BACK TO INITIAL MAINLOOP STATE?? return master = tk.Tk() app = Application(master=master) app.Create_canvas(1920,1080) app.Application_Intro()The program above will create a simple GUI with 2 buttons. Initially, the program will IDLE and wait for user to start an Application. When application is started, it will call itself recursively ( checking various states and doing other complex operations in my real program), however, I Want to be able to interrupt this operation and stop it at any time. That is why I have created a button "RESTART" which should restart the program back to its initial state where I am waiting for user to start and application again. How can I return out of the Start_program function after the button is pressed?
https://ibb.co/djMd5TW
Can someone help me understand what needs to happen in Restart function to get back to idle state in my mainloop