2012-04-03 550 views
12

使用標有"Good-bye"的按鈕編寫GUI應用程序。當點擊該窗口時,該窗口關閉。如何通過按下按鈕來關閉Tkinter窗口?

這是我的代碼到目前爲止,但它不工作。任何人都可以幫我解決我的代碼嗎?

from Tkinter import * 

window = Tk() 

def close_window (root): 
    root.destroy() 

frame = Frame(window) 
frame.pack() 
button = Button (frame, text = "Good-bye.", command = close_window) 
button.pack() 

window.mainloop() 
+4

嘿馬特。感謝您提出明確的問題,並附上簡潔明瞭的代碼示例。當你的代碼「不能正常工作」時,你能否確保在將來包含追溯(崩潰)?這也將幫助人們幾乎立即弄清楚代碼的哪部分被破壞了。很顯然,在這種情況下,你的代碼樣本非常小,很容易識別,但它可能確實有助於你在更困難的情況下獲得答案。 – jdi 2012-04-03 06:47:11

+0

對於沒有立即看到問題的人,錯誤是'TypeError:close_window()缺少1個需要的位置參數:'root'。這意味着沒有參數傳遞給回調'close_window',因爲從來沒有'command ='函數。綁定事件回調確實得到一個參數 - 事件對象。 – 2015-03-17 00:23:13

回答

7

你可以創建一個類,擴展了Tkinter的Button類,將被專門由destroy方法關聯到其command屬性關閉窗口:

from tkinter import * 

class quitButton(Button): 
    def __init__(self, parent): 
     Button.__init__(self, parent) 
     self['text'] = 'Good Bye' 
     # Command to close the window (the destory method) 
     self['command'] = parent.destroy 
     self.pack(side=BOTTOM) 

root = Tk() 
quitButton(root) 
mainloop() 

這是輸出:

enter image description here


爲什麼之前你的代碼沒有工作的原因:

def close_window(): 
    # root.destroy() 
    window.destroy() 

我中有你可能有根從別的地方有輕微的感覺,因爲你沒有window = tk()

當您在Tkinter中調用window上的destroy時,意味着銷燬整個應用程序,因爲您的window(根窗口)是應用程序的主窗口。恕我直言,我認爲你應該改變你的windowroot

from tkinter import * 

def close_window(): 
    root.destroy() # destroying the main window 

root = Tk() 
frame = Frame(root) 
frame.pack() 

button = Button(frame) 
button['text'] ="Good-bye." 
button['command'] = close_window 
button.pack() 

mainloop() 
20

以最小的編輯到您的代碼(不知道他們有沒有教課或不在您的課程),更改:

def close_window(root): 
    root.destroy() 

def close_window(): 
    window.destroy() 

,它應該工作。


說明:

你的close_window版本被定義爲期望一個參數,即root。隨後,任何對您的close_window版本的調用都需要這個參數,否則Python會給您一個運行時錯誤

當您創建Button時,您告知按鈕單擊時運行close_window。然而,對於按鈕插件的源代碼是一樣的東西:

# class constructor 
def __init__(self, some_args, command, more_args): 
    #... 
    self.command = command 
    #... 

# this method is called when the user clicks the button 
def clicked(self): 
    #... 
    self.command() # Button calls your function with no arguments. 
    #... 

至於我的代碼州,Button類將調用你的函數不帶參數。然而你的功能正在期待一個論點。因此你有一個錯誤。所以,如果我們採取了這樣的說法,這樣的函數調用將Button類內部執行,我們就只剩下:

def close_window(): 
    root.destroy() 

這是不對的,但是,無論是,因爲root從未賦值。這就好像你還沒有定義x時輸入print(x)

看你的代碼,我想你想叫destroywindow,所以我改變rootwindow

1

您可以使用lambdawindow對象作爲參數的引用傳遞給close_window功能:

button = Button (frame, text="Good-bye.", command = lambda: close_window(window)) 

這工作,因爲command屬性期待一個調用,或可調用狀物體。 A lambda是可調用的,但在本例中它基本上是使用設置參數調用給定函數的結果。

實際上,您正在調用沒有args的函數的lambda包裝器,而不是函數本身。

6

您可以將函數對象window.destroy直接關聯到你的buttoncommand屬性:

button = Button (frame, text="Good-bye.", command=window.destroy) 

這樣你就不需要功能close_window關閉窗口爲您服務。

-1
from tkinter import * 

def close_window(): 
    import sys 
    sys.exit() 

root = Tk() 

frame = Frame (root) 
frame.pack() 

button = Button (frame, text="Good-bye", command=close_window) 
button.pack() 

mainloop() 
相關問題