Swayam S.
Swayam S.

Reputation: 75

How do I retrieve the ID of a bitmap button in wxpython?

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

I create multiple buttons using the same code. How do I retrieve an individual buttons' ID later on?

Thanks in advance, Swayam

Upvotes: 0

Views: 440

Answers (2)

Mike Driscoll
Mike Driscoll

Reputation: 33071

Here's a quick and dirty script that will show you how to get button ids and labels. I'm using normal wx.Button objects since there's no good way to include images for bitmap buttons on 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()

Upvotes: 0

joaquin
joaquin

Reputation: 85603

GetId is the method to get the Id of an object. So you can write:

id_of_button_n = button_n.GetId()

However in your case, this is not neccessary because you already stored the ids as the keys of the dictionary self.btarray !

Upvotes: 1

Related Questions