# Python tkinter stopping mainloop and going back to IDLE state

#### zazas321

Joined Nov 29, 2015
926
Hello. I have developed a tkinter GUI for my application. I have been working with tkinter and learning it for a couple of months now. I have tried various tkinter GUI methods ( frames, canvas and etc ) and I found a way that works best for me. However, I am having one trouble regarding breaking out of mainloop while its running. I have developed a simple script to show what I mean:

Code:
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()
self.master.mainloop()

def Start_program(self):
print("Program start")
self.master.after(1000,self.Start_program)

def Restart(self):# IF TASK STARTED SET RESTART =1, IF NOT restart devices and refresh app
print("HERE I WANT TO INTERRUPT START PROGRAM AND RETURN TO IDLE STATE")
print("REFRESH GUI ELEMENTS, DESTROY ANY WIDGETS IF CREATED")
print("RESET THE GLOBAL VARIABLE VALUES")
self.master.quit()
#master.mainloop()
#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 code above is simplified representation of my actual program. This example as well as my actual program runs in recursive mode to make use of tkinter mainloop and after method. Once a specific task is started, the user must complete it before doing something else ( for example starting a new task ). However, in a rare case, user may need to stop and restart the task if something wrong happen. I need to be able to do that while the mainloop is running.

The program above shows exactly what I want to acomplish. When start prorgram button is pressed, user starts a task which calls itself repeadetly until is completed, however, if user does not wish to complete it or something important happened, I need to restart the mainloop and return to the idle state where the program is waiting for user input. What do I need to do in the restart function to make that happen? Appereciate any suggestions!

As you can see from the image above, once the button is pressed, the "program start" text is displayed every 1 second because the function is called recursively. When I press the restart button, I want to interrupt the program start, stop it and return to the idle state.