我的代碼,如何在wxpython中打印具有相同名稱的多個按鈕的ID?
self.one = wx.Button(self, -1, "Add Button", pos = 100,20)
self.one.Bind(wx.EVT_BUTTON, self.addbt)
self.x = 50
self.idr = 0
self.btarr = []
def addbt(self.event)
self.button = wx.Button(self, self.idr, "Button 1", pos = (self.x,100))
self.button.Bind(wx.EVT_BUTTON, self.OnId)
self.idr+=1
self.x +=150
self.btarr.append(self.button)
def OnId(self, Event):
print "The id for the clicked button is: %d" % self.button.GetId()
#I wanna print id of indivual buttons clicked
我使用上面的代碼動態創建多個按鈕。所有創建的按鈕都有相同的參考名稱。點擊每個按鈕我應該分別獲得各自的ID。但是我得到的是最後一個創建的按鈕的ID。如何打印單個按鈕的ID?
在此先感謝!
謝謝!實際上我在原始程序中就是這樣做的。即使我創建了一個按鈕列表,它如何幫助我獲取相應的ID? – 2013-03-12 04:19:41
GetId()獲取ID ...但是你在設置'self.idr = 0'後立即將它設置爲self.idr,因此你總是將該ID設置爲零 – 2013-03-12 15:21:50
謝謝Joran,我已經修改了! – 2013-03-12 16:21:00