Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Tkinter application doesn't quit properly

Tags:

python

tkinter

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?

like image 541
jldupont Avatar asked Jan 20 '23 19:01

jldupont


2 Answers

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

like image 124
Juan Gomez Avatar answered Jan 28 '23 14:01

Juan Gomez


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.
like image 34
Matt Habel Avatar answered Jan 28 '23 15:01

Matt Habel