Skip to content Skip to sidebar Skip to footer

Send Variables In Dynamically Created Buttons With Wxpython

I want to send a variable to a function from dynamically created buttons (14 of them) # creating buttons for i in range(0, 14): m_pauser.append(wx.Button(panel, 5, 'Pau

Solution 1:

I suppose the issue is that every button gets the same ID, which seems to be 5 in your example. It is a bad habit to hardcode wxwidgets IDs. Use -1 or wx.NewId() instead.

EDIT: It is not the ID (but ID they should be unique, anyway). The reason is that i does point to the last value it had (during the loop, i is always "the same thing" and there are never "distinct i's). Do the following instead:

For the sake of clarity do can do the following:

m_pauser = []
for i inrange(0, 14):
    btn = wx.Button(panel, -1, "Pause "+str(i))
    m_pauser.append(btn)
    btn.Bind(wx.EVT_BUTTON, lambda event, temp=i: self.dispenser_pause(event, temp))
    box.Add(btn, 0, wx.ALL, 10)

The reason is explained better in the wxPython wiki than I could do it.

Post a Comment for "Send Variables In Dynamically Created Buttons With Wxpython"