self.btid = 0
self.btarray = []
self.btarray.append("x")#so that the buttons are appended according to their ids
self.bmt = wx.BitmapButton(panel, btid, pic, pos=(50,50))
self.btarray.append(self.bmt)
self.btid += 1
我使用相同的代码创建多个按钮。如何在以后检索单个按钮的ID?
提前致谢, Swayam
答案 0 :(得分:1)
GetId
是获取对象Id的方法。
所以你可以写:
id_of_button_n = button_n.GetId()
然而在你的情况下,这不是必要的,因为你已经将id存储为字典self.btarray
的键!
答案 1 :(得分:0)
这是一个快速而又脏的脚本,它将向您展示如何获取按钮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()