Hello I am trying to create an on off button and two circles,the one circle is white and the other one red,the circles represent one LED,when I press on I want to see the red circle and wenn I press again see the white. I have write this code but I dont wont to work it with two panels and call the on switch panels function.Is any other way to do that?I mean if I can bind the on button with the red circle and hide the white and when I press again the same button or another one with the label off to hide the red and show the white?
Here is the code:
`import wx
class PanelOne(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent=parent)
button =wx.Button(self, label="Turn ON", pos=(160, 130))
button.Bind(wx.EVT_BUTTON, parent.onSwitchPanels)
self.Bind(wx.EVT_PAINT, self.OnPaint)
def OnPaint(self, evt):
dc = wx.PaintDC(self)
dc.Clear()
dc.SetBrush(wx.Brush('White'))
dc.DrawCircle(200, 230, 35)
class PanelTwo(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent=parent)
button =wx.Button(self, label="Turn OFF", pos=(160, 130))
button.Bind(wx.EVT_BUTTON, parent.onSwitchPanels)
self.Bind(wx.EVT_PAINT, self.OnPaint)
def OnPaint(self, evt):
dc = wx.PaintDC(self)
dc.Clear()
dc.SetBrush(wx.Brush('Red'))
dc.DrawCircle(200, 230, 35)
class MainForm(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY,"LED" )
self.panel_one = PanelOne(self)
self.panel_two = PanelTwo(self)
self.panel_two.Hide()
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.panel_one, 1, wx.EXPAND)
self.sizer.Add(self.panel_two, 1, wx.EXPAND)
self.SetSizer(self.sizer)
def onSwitchPanels(self, event):
if self.panel_one.IsShown():
self.SetTitle("LED")
self.panel_one.Hide()
self.panel_two.Show()
else:
self.SetTitle("LED")
self.panel_one.Show()
self.panel_two.Hide()
self.Layout()
if __name__ == "__main__":
app = wx.App(False)
frame = MainForm()
frame.Show()
app.MainLoop()`
Here is one way using a single panel that alters the same button and same circle.
import wx
class PanelOne(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent=parent)
self.state = [('White', 'Turn ON'), ('Red', 'Turn OFF')]
self.button = wx.Button(self, label=self.state[0][1], pos=(160, 130))
self.button.Bind(wx.EVT_BUTTON, self.switch_state)
self.Bind(wx.EVT_PAINT, self.OnPaint)
def OnPaint(self, evt):
dc = wx.PaintDC(self)
dc.Clear()
dc.SetBrush(wx.Brush(self.state[0][0]))
dc.DrawCircle(200, 230, 35)
def switch_state(self, event):
self.state.append(self.state.pop(0))
self.button.SetLabel(label=self.state[0][1])
self.Refresh()
class MainForm(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "LED")
self.panel_one = PanelOne(self)
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.panel_one, 1, wx.EXPAND)
self.SetSizer(self.sizer)
if __name__ == "__main__":
app = wx.App(False)
frame = MainForm()
frame.Show()
app.MainLoop()