我想用2个面板编写简单的代码。简单的“Hello”和旁边的按钮。按钮将面板更改为panel_2,文本“Panel 2”和旁边的按钮。我不知道如何实现这一目标。有我的代码:
import wx
class Glowne(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent=parent)
self.SetSize((800,600))
tekst = 'HELLO'
font = wx.Font(18, wx.DEFAULT, wx.NORMAL, wx.NORMAL)
wx.StaticText(self, -1, tekst, (300, 10)).SetFont(font)
btn = wx.Button(self, -1, "Change panel", (345, 100))
self.Bind(wx.EVT_BUTTON, Program.zmiana, btn)
class Glowne1(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
tekst = 'Panel 2'
font = wx.Font(18, wx.DEFAULT, wx.NORMAL, wx.NORMAL)
wx.StaticText(self, -1, tekst, (300, 10)).SetFont(font)
btn = wx.Button(self, -1, "Change panel", (345, 100))
#self.Bind(wx.EVT_BUTTON, frame.zmiana, btn)
class Program(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY,'Program')
panel_one = Glowne(self)
self.panel_two = Glowne1(self)
self.panel_two.Hide()
self.SetSize((800,600))
self.Centre()
def zmiana(self):
self.panel_one.Hide()
self.panel_two.Show()
if __name__ == "__main__":
app = wx.App(False)
frame = Program()
frame.Show()
app.MainLoop()
感谢您的帮助。我读过this tutorial但我无法在简单的代码中使用它。
答案 0 :(得分:1)
程序框架需要添加一个sizer,面板需要自我添加到btn,这样它们就可以从类Program中加入,绑定到类Program中的按钮。
这是一个例子
import wx
class Glowne(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent=parent)
self.SetSize((800, 600))
tekst = 'HELLO'
font = wx.Font(18, wx.DEFAULT, wx.NORMAL, wx.NORMAL)
wx.StaticText(self, -1, tekst, (300, 10)).SetFont(font)
self.btn = wx.Button(self, -1, "Change panel", (345, 100))
class Glowne1(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
tekst = 'Panel 2'
font = wx.Font(18, wx.DEFAULT, wx.NORMAL, wx.NORMAL)
wx.StaticText(self, -1, tekst, (300, 10)).SetFont(font)
self.btn = wx.Button(self, -1, "Change panel", (345, 100))
class Program(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, 'Program')
sizer = wx.BoxSizer()
self.SetSizer(sizer)
self.panel_one = Glowne(self)
sizer.Add(self.panel_one, 1, wx.EXPAND)
self.panel_one.btn.Bind(wx.EVT_BUTTON, self.show_panel_two)
self.panel_two = Glowne1(self)
sizer.Add(self.panel_two, 1, wx.EXPAND)
self.panel_two.btn.Bind(wx.EVT_BUTTON, self.show_panel_one)
self.panel_two.Hide()
self.SetSize((800, 600))
self.Centre()
def show_panel_one(self, event):
self.panel_one.Show()
self.panel_two.Hide()
self.Layout()
def show_panel_two(self, event):
self.panel_two.Show()
self.panel_one.Hide()
self.Layout()
if __name__ == "__main__":
app = wx.App(False)
frame = Program()
frame.Show()
app.MainLoop()