2011-05-06 67 views
3
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() 

爲什麼Tkinter程序在我按下「退出」按鈕時無法正常結束?Python Tkinter應用程序不能正常退出

+0

你在使用IDLE嗎? – 2011-05-06 19:36:09

+0

不使用IDLE。 – jldupont 2011-05-06 19:37:44

回答

4

隨着self.destroy()你只是破壞了框架,而不是頂級容器,你需要做self.master.destroy()讓它正確地退出

3

這不起作用的原因是因爲您正在使用不正確的方式在quit_pressed中結束程序。你現在正在做的是殺死自己的框架,而不是根框架。自我框架是一種新的框架,您已將其網格化到根框架中,因此,當您終止自我框架時,您並不會殺死根框架。這可能聽起來令人困惑,因爲我的打字風格,所以讓我舉個例子。

目前,你有

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

您可以通過更改功能,這對解決這個問題,

def quit_pressed(self): 
    quit() #This will kill the application itself, not the self frame.