Python Forum
TKinter restarting the mainloop when button pressed
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TKinter restarting the mainloop when button pressed
#1
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:

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
Reply


Messages In This Thread
TKinter restarting the mainloop when button pressed - by zazas321 - Jan-22-2021, 08:44 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] TKinter Remove Button Frame Nu2Python 8 814 Jan-16-2024, 06:44 PM
Last Post: rob101
  tkinter - touchscreen, push the button like click the mouse John64 5 744 Jan-06-2024, 03:45 PM
Last Post: deanhystad
  Centering and adding a push button to a grid window, TKinter Edward_ 15 4,375 May-25-2023, 07:37 PM
Last Post: deanhystad
  Can't get tkinter button to change color based on changes in data dford 4 3,362 Feb-13-2022, 01:57 PM
Last Post: dford
  Creating a function interrupt button tkinter AnotherSam 2 5,416 Oct-07-2021, 02:56 PM
Last Post: AnotherSam
  [Tkinter] Have tkinter button toggle on and off a continuously running function AnotherSam 5 4,918 Oct-01-2021, 05:00 PM
Last Post: Yoriz
  tkinter showing image in button rwahdan 3 5,519 Jun-16-2021, 06:08 AM
Last Post: Yoriz
  tkinter button image Nick_tkinter 4 3,958 Mar-04-2021, 11:33 PM
Last Post: deanhystad
  tkinter python button position problem Nick_tkinter 3 3,483 Jan-31-2021, 05:15 AM
Last Post: deanhystad
  tkinter touchscreen scrolling - button press makes unwanted scrolling nanok66 1 3,923 Dec-28-2020, 10:00 PM
Last Post: nanok66

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020