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?

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

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.

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

Comments

Your Answer

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.