这是一个快速而肮脏的脚本,它将向您展示如何获取按钮 ID 和标签。我使用的是普通的 wx.Button 对象,因为没有好的方法可以在 Stack 上包含位图按钮的图像:
import random
import wx
########################################################################
class MyPanel(wx.Panel):
""""""
#----------------------------------------------------------------------
def __init__(self, parent):
"""Constructor"""
wx.Panel.__init__(self, parent=parent)
mainSizer = wx.BoxSizer(wx.VERTICAL)
i = random.choice(range(5, 10))
for index, item in enumerate(range(i)):
num = index + 1
btn = wx.Button(self, label="Button %s" % num)
btn.Bind(wx.EVT_BUTTON, self.onClick)
mainSizer.Add(btn, 0, wx.ALL|wx.CENTER, 5)
self.SetSizer(mainSizer)
#----------------------------------------------------------------------
def onClick(self, event):
""""""
btn = event.GetEventObject()
print "%s id => %s" % (btn.GetLabel(), btn.GetId())
########################################################################
class MainFrame(wx.Frame):
""""""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
wx.Frame.__init__(self, None, title="Random Buttons", size=(1024, 768))
panel = MyPanel(self)
self.Show()
if __name__ == "__main__":
app = wx.App(False)
frame = MainFrame()
app.MainLoop()