Home > front end >  Exit Python Tkinter app cleanly while also using "wait_variable" function on a button in a
Exit Python Tkinter app cleanly while also using "wait_variable" function on a button in a

Time:11-11

I have a problem similar to this post: Exit program within a tkinter class

My variation on the problem involves the wait_variable being used on a button to control "stepping forward" in an app, but also allowing the app to close cleanly.

See my code below:

# To see output unbuffered:
#   python -u delete_win_test.py

import tkinter as tk
from tkinter import *


class GUI(Tk):
    def __init__(self):
        super().__init__()

        # Close the app when the window's X is pressed
        self.protocol("WM_DELETE_WINDOW", self.closing)

        # When this var is set to 1, the move function can continue
        self.var = tk.IntVar()

        # Close the app if the button is pressed
        button = tk.Button(self, text="Exit",
                           command=self.destroy)
        button.place(relx=.5, rely=.5, anchor="c")

        # Step forward
        self.step_button = tk.Button(self, text="Step",
                                     command=lambda: self.var.set(1))
        self.step_button.place(relx=.5, rely=.75, anchor="c")

    def move(self):
        print("doing stuff")  # simulates stuff being done
        self.step_button.wait_variable(self.var)
        self.after(0, self.move)

    def closing(self):
        self.destroy()


app = GUI()
app.move()
app.mainloop()
  • The window shows correctly
  • "Stepping forward" works because "doing stuff" prints to terminal on button click
  • Exiting the window by both pressing X or using the "exit" button both work

The problem: the Python app never exits from the terminal and requires a closing of the terminal.

How can I make the Python program exit cleanly so the user does not need to close and re-open a new terminal window?

Related references for animation, etc:

UPDATE (the solution):

         # Close the app if the button is pressed
         button = tk.Button(self, text="Exit",
-                           command=self.destroy)
                            command=self.closing)
         button.place(relx=.5, rely=.5, anchor="c")
 
         # Step forward
...
     def closing(self):
         self.destroy()
         self.var.set("")
         exit(0)

CodePudding user response:

Your closing function needs to set the variable to cause the app to stop waiting.

def closing(self):
    self.destroy()
    self.var.set("")

CodePudding user response:

in the closing function, you need to call exit to exit the program.

def closing(self):
    self.destroy() #closes tkinkter window
    exit(0) #exits program
  • Related