您好我正在尝试创建一个开关按钮和两个圆圈,一个圆圈为白色,另一个圆圈为红色,圆圈代表一个LED,当我按下时我想要看到红色圆圈并且我再次按下看到白色。我已经编写了这段代码,但是我不打算用两个面板工作并调用开关面板功能。还有其他任何方法吗?我的意思是如果我可以用红色圆圈绑定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()`
答案 0 :(得分:1)
以下是使用单个面板改变相同按钮和相同圆圈的一种方法。
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()