2017-12-18 15 views
0

次のコードが与えられます。このコードを実行するときtkinter-appを完全に閉じるには?

import tkinter as tk 


class Application(tk.Frame): 
    def __init__(self, master=None): 
     tk.Frame.__init__(self, master) 
     self.grid() 
     self.createWidgets() 

    def createWidgets(self): 
     self.quitButton = tk.Button(self, text='Quit', command=self.destroy) 
     self.quitButton.grid() 


app = Application() 
app.master.title('Sample application') 
app.mainloop() 

小さなウィンドウがそれでQUIT-ボタンが表示されます。ボタンは押されても消えますが、ウィンドウはそのままです。 ボタンとクリックしてウィンドウとアプリケーションを閉じるにはどうすればいいですか?ボタンを押して、ウィンドウを破壊するあなたは、あなたのコードでself.master.destroyを欠いている

+2

self.master.destroy' 'と' self.destroy'を交換してください。 'Self'は単なるフレームですが、ルートウィンドウは' master'です(暗黙的に 'None'を渡すので)! – CommonSense

+0

ouh! UPS。どうもありがとうございました。 – lordnik22

+1

簡単な答えは、ルートウィンドウで '.destroy()'を呼び出すことです。 –

答えて

0

Quit

import tkinter as tk 


class Application(tk.Frame): 
    def __init__(self, master=None): 
     tk.Frame.__init__(self, master) 
     self.grid() 
     self.createWidgets() 

    def createWidgets(self): 
     self.quitButton = tk.Button(self, text='Quit', command=self.master.destroy) 
     self.quitButton.grid() 


app = Application() 
app.master.title('Sample application') 
app.mainloop() 
関連する問題