开发者

Python Tkinter application doesn't quit properly

开发者 https://www.devze.com 2023-03-03 04:55 出处:网络
from TKinter import * class Ui(Frame): def __init__(self) Frame.__init__(self, None) self.grid() bquit=Button(self, text=\"Quit\", command=self.quit_pressed)
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?开发者_如何学Python


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


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.
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号