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
#6
I have found a way how to go back to idle state with the following code:

import tkinter as tk
from tkinter import Button,Entry,Canvas,Label,ttk
import concurrent.futures




class Application(tk.Frame): 
    def __init__(self,master=None):
        self.master = master
        
    def Create_canvas(self,canvas_width,canvas_height):
        global canvas#described as global because used outside class
        canvas = tk.Canvas(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):
        global restart
        print("Program start,checking if restart is required")
        if(restart == 1):
            print("GOING BACK TO IDLE STATE")
            restart = 0
            return
        else:
            master.after(1000,self.Start_program)
        
    def Restart(self):# IF TASK STARTED SET RESTART =1, IF NOT restart devices and refresh app
        global restart
        restart = 1
        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")

        
        #master.mainloop()
        #WHAT TO DO IN THIS FUNCTION TO GO BACK TO INITIAL MAINLOOP STATE??
        return

def main():
    global restart
    restart = 0

main()
master = tk.Tk()
app = Application(master=master)
app.Create_canvas(1920,1080)

app.Application_Intro()    
        
        
However, I do not think that this is the best way to achieve what I want. It works as expected, I have setup a global variable "restart" which I set to 1 when restart button is pressed. During the task which is calling itself recursively, I am checking whether restart is 1, if so, restart the task and go to idle state, if not, continue doing the same task.
I still think that this is not the best way to acomplish. If anyone got any other suggestions let me know! I would love to improve it
Reply


Messages In This Thread
RE: TKinter restarting the mainloop when button pressed - by zazas321 - Jan-25-2021, 11:01 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] TKinter Remove Button Frame Nu2Python 8 1,027 Jan-16-2024, 06:44 PM
Last Post: rob101
  tkinter - touchscreen, push the button like click the mouse John64 5 878 Jan-06-2024, 03:45 PM
Last Post: deanhystad
  Centering and adding a push button to a grid window, TKinter Edward_ 15 4,911 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,450 Feb-13-2022, 01:57 PM
Last Post: dford
  Creating a function interrupt button tkinter AnotherSam 2 5,576 Oct-07-2021, 02:56 PM
Last Post: AnotherSam
  [Tkinter] Have tkinter button toggle on and off a continuously running function AnotherSam 5 5,053 Oct-01-2021, 05:00 PM
Last Post: Yoriz
  tkinter showing image in button rwahdan 3 5,666 Jun-16-2021, 06:08 AM
Last Post: Yoriz
  tkinter button image Nick_tkinter 4 4,090 Mar-04-2021, 11:33 PM
Last Post: deanhystad
  tkinter python button position problem Nick_tkinter 3 3,587 Jan-31-2021, 05:15 AM
Last Post: deanhystad
  tkinter touchscreen scrolling - button press makes unwanted scrolling nanok66 1 4,033 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