4
from TKinter import *
class Ui(Frame):
 def __init__(self)
 Frame.__init__(self, None)
 self.grid()
 bquit=Button(self, text="Quit", command=self.quit_pressed)
 bquit.grid(row=0, column=0)
 def quit_pressed(self):
 self.destroy()
app=Ui()
app.mainloop()

Why doesn't this Tkinter program end properly when I press the "Quit" button?

asked May 6, 2011 at 19:34
0

3 Answers 3

6

With self.destroy() you're just destroying the Frame, not the the top level container, you need to do self.master.destroy() for it to exit correctly

answered May 6, 2011 at 19:46
Sign up to request clarification or add additional context in comments.

Comments

4

The reason this does not work is because you are using an incorrect way to end the program in quit_pressed. What you are doing right now is killing the self frame, not the root frame. The self frame is a new frame that you have gridded into the root frame, therefore when you kill the self frame, you are not killing the root frame. This may sound confusing due to my typing style, so let me give an example.

Currently, you have

def quit_pressed(self):
 self.destroy() #This destroys the current self frame, not the root frame which is a different frame entirely

You are able to remedy this by changing the function to this,

def quit_pressed(self):
 quit() #This will kill the application itself, not the self frame.
answered May 6, 2011 at 19:45

Comments

0

Another more brute-force method to consider if you keep having issues. I obtained the process ID (PID) for the main program and any subprocesses generated. I have included a snipping of how I implemented this so that all processes closed when I pressed the exit window button in Linux. Its a class called 'Moisure' using custom tkinter where process objects generated called P and L (for my plotter and logger processes) called inside were obtained to kill. Your program should finish the if statement upon closing. The 'try' statement is in case I never started those processes. This has not been tested on any other operating, but it should work on windows as well but Ive read packaging as an exe has fixed this.

import os, signal
if __name__ == "__main__":
 app = Moisture()
 app.mainloop()
 try:
 app.P.kill()
 except:
 pass
 try:
 app.L.kill()
 except:
 pass
 PID = os.getpid()
 os.kill(PID, signal.SIGKILL) 

Hopefully it helps someone

answered Jul 13, 2023 at 17:51

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.