您好,我正在尝试创建一个开关按钮和两个圆圈,一个圆圈是白色的,另一个是红色的,圆圈代表一个 LED,当我按下时我想看到红色圆圈,然后我再次按下看到白色. 我已经编写了这段代码,但我不会使用两个面板并调用 on switch panel 函数。还有其他方法吗?我的意思是如果我可以将 on 按钮与红色圆圈绑定并隐藏白色以及何时我再次按下相同的按钮或关闭标签的另一个按钮以隐藏红色并显示白色?
这是代码:
`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()`