在以下代碼中(靈感來自this片段),我使用單個事件處理程序buttonClick
來更改窗口的標題。目前,我需要評估事件的ID是否與按鈕的ID相對應。如果我決定添加50個按鈕而不是2個,則此方法可能變得麻煩。有一個更好的方法嗎?獲取調用事件按鈕名稱的最佳方法?
import wx
class MyFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, 'wxBitmapButton',
pos=(300, 150), size=(300, 350))
self.panel1 = wx.Panel(self, -1)
self.button1 = wx.Button(self.panel1, id=-1,
pos=(10, 20), size = (20,20))
self.button1.Bind(wx.EVT_BUTTON, self.buttonClick)
self.button2 = wx.Button(self.panel1, id=-1,
pos=(40, 20), size = (20,20))
self.button2.Bind(wx.EVT_BUTTON, self.buttonClick)
self.Show(True)
def buttonClick(self,event):
if event.Id == self.button1.Id:
self.SetTitle("Button 1 clicked")
elif event.Id == self.button2.Id:
self.SetTitle("Button 2 clicked")
application = wx.PySimpleApp()
window = MyFrame()
application.MainLoop()
我經常上的小部件設置的屬性以及當我想不出更好的辦法。 – FogleBird 2009-06-11 23:01:04