Only one button in a panel with multiple togglebuttons changes color - wxPython -


i want set color of toggle button of choice in panel have created. problem in numerous toggle buttons have displayed on panel when want change color of each 1 color of last button changes. here's code:

import wx  class frame(wx.frame):  def __init__(self):     wx.frame.__init__(self,none)     self.panel = wx.panel(self,wx.id_any)      self.sizer = wx.boxsizer(wx.vertical)     self.flags_panel = wx.panel(self, wx.id_any, style = wx.sunken_border)      self.sizer.add(self.flags_panel)     self.setsizer(self.sizer,wx.expand | wx.all)     self.flags = flags(self.flags_panel, [8,12])     self.flags.show()  class flags (wx.panel): def __init__(self,panel, num_flags = []):#,rows = 0,columns = 0,radius = 0, hspace = 0, vspace = 0,x_start = 0, y_start = 0     wx.panel.__init__(self,panel,-1, size = (350,700))      num_rows = num_flags[0]     num_columns = num_flags[1]     x_pos_start = 10     y_pos_start = 10      = x_pos_start     j = y_pos_start     buttons = []     in range (num_columns):         buttons.append('toggle button')     self.buttonvalue = false     button in buttons:         index = 0         while index != 15:              self.button = wx.togglebutton(self,-1,size = (10,10), pos = (i,j))             self.bind(wx.evt_togglebutton,self.onflagcreation, self.button)             self.button.show()             += 15             index += 1         j += 15         = 10      self.show()  def onflagcreation(self,event):     if not self.buttonvalue:         self.button.setbackgroundcolour('#fe1919')         self.buttonvalue = true     else:         self.button.setbackgroundcolour('#14e807')         self.buttonvalue = false  if __name__ == '__main__':    app = wx.app(false)    frame = frame()    frame.show()    app.mainloop() 

your problem quite simple. last button changed because it's last button defined:

self.button = wx.togglebutton(self,-1,size = (10,10), pos = (i,j)) 

each time through for loop, reassign self.button attribute different button. want extract button event object , change background color. change function this:

def onflagcreation(self,event):     btn = event.geteventobject()     if not self.buttonvalue:         btn.setbackgroundcolour('#fe1919')         self.buttonvalue = true     else:         btn.setbackgroundcolour('#14e807')         self.buttonvalue = false 

see also:


Comments