2008-09-16 101 views

回答

15

找到這樣一個很好的教程雖然Eli Courtwright's計劃將努力fine¹,你真的好像雖然要的只是一個實例後重新配置,當你instantiated²你可以設置的任何屬性的方式。你如何做是通過configure()方法。

¹「罰款」,如果您只使用鼠標;如果您關心Tab鍵並在按鈕上使用[Space]或[Enter],那麼您將不得不實現(複製現有代碼)按鍵事件。通過.configure設置command選項要容易得多。

²實例化後唯一不能改變的屬性是name

1

當然;只需使用bind方法指定按鈕創建後的回調。我剛剛編寫並測試了下面的示例。您可以在http://www.pythonware.com/library/tkinter/introduction/events-and-bindings.htm

from Tkinter import Tk, Button 

root = Tk() 
button = Button(root, text="Click Me!") 
button.pack() 

def callback(event): 
    print "Hello World!" 

button.bind("<Button-1>", callback) 
root.mainloop() 
+1

命令配置選項通常用於按鈕按下。回調函數不需要事件參數。 – tzot 2008-09-16 10:36:47