点击按钮后,我正在尝试切换新面板。我创建了一个带有两个选项卡(笔记本)的面板,每个选项卡都有按钮。单击这些按钮将切换到新面板。但是,我遇到了一些错误。这是我的代码:
import wx
########################################################################
class TabPanel(wx.Panel):
""" This will be the first notebook tab """
def __init__(self, parent):
wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)
sizer = wx.BoxSizer(wx.VERTICAL)
#txtOne = wx.TextCtrl(self, wx.ID_ANY, "")
#txtTwo = wx.TextCtrl(self, wx.ID_ANY, "")
button1 = wx.Button(self, -1, "Button One", (0, 20), size = (200, 30))
button1.Bind(wx.EVT_BUTTON, self.onSwitchPanels1)
sizer = wx.BoxSizer(wx.VERTICAL)
#sizer.Add(txtOne, 0, wx.ALL, 5)
#sizer.Add(txtTwo, 0, wx.ALL, 5)
self.SetSizer(sizer)
def onSwitchPanels1(self, event):
if self.tab_one.IsShown():
self.SetTitle("Test")
self.tab_one.Hide()
self.panel_one.Show()
else:
self.SetTitle("Test")
self.tab_one.Show()
self.panel_one.Hide()
self.Layout()
########################################################################
class PanelOne(wx.Panel):
""" This is for panel switch """
def ___init___(self, parent):
wx.Panel.___init___(self, parent=parent, id=wx.ID_ANY)
button8 = wx.Button(self, label="T2D", pos=(0, 0))
########################################################################
class NotebookDemo(wx.Notebook):
""" Notebook class """
def __init__(self, parent):
wx.Notebook.__init__(self, parent, id=wx.ID_ANY, style=
wx.BK_DEFAULT
#wx.BK_TOP
#wx.BK_BOTTOM
#wx.BK_LEFT
#wx.BK_RIGHT
)
# Create the first tab and add it to the notebook
tabOne = TabPanel(self)
self.AddPage(tabOne, "TabOne")
# Create and add the second tab
tabTwo = TabPanel(self)
self.AddPage(tabTwo, "TabTwo")
########################################################################
class DemoFrame(wx.Frame):
""" Frame that holds all other widgets """
def __init__(self):
"""Constructor"""
wx.Frame.__init__(self, None, wx.ID_ANY,
"Notebook Tutorial",
size=(600,400)
)
panel = wx.Panel(self)
notebook = NotebookDemo(panel)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(notebook, 1, wx.ALL|wx.EXPAND, 5)
panel.SetSizer(sizer)
self.Layout()
self.Show()
self.tab_one = TabPanel(self)
self.panel_one = PanelOne(self)
self.panel_one.Hide()
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.tab_one, 1, wx.EXPAND)
self.sizer.Add(self.panel_one, 1, wx.EXPAND)
self.SetSizer(self.sizer)
#----------------------------------------------------------------------
if __name__ == "__main__":
app = wx.PySimpleApp()
frame = DemoFrame()
app.MainLoop()
我尝试了一些解决方案,但似乎无效。这是我得到的错误:
AttributeError: 'TabPanel' object has no attribute 'tab_one'
有人可以帮我吗?
答案 0 :(得分:0)
有一些要记住的要点
notebook.SetSelection(<index>)
self.tabOne, self.tabTwo
试试这个:
import wx
class TabPanel(wx.Panel):
""" This will be the first notebook tab """
def __init__(self, parent):
wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)
self.parent=parent
sizer = wx.BoxSizer(wx.VERTICAL)
#txtOne = wx.TextCtrl(self, wx.ID_ANY, "")
#txtTwo = wx.TextCtrl(self, wx.ID_ANY, "")
button1 = wx.Button(self, -1, "Button One", (0, 20), size = (200, 30))
button1.Bind(wx.EVT_BUTTON, self.onSwitchPanels1)
sizer = wx.BoxSizer(wx.VERTICAL)
#sizer.Add(txtOne, 0, wx.ALL, 5)
#sizer.Add(txtTwo, 0, wx.ALL, 5)
self.SetSizer(sizer)
def onSwitchPanels1(self, event):
#if self.tab_one.IsShown():
#self.SetTitle("Test")
self.parent.SetSelection(1)
#self.TabPanel.Hide()
#self.NotebookDemo.tabTwo.Show()
#else:
# self.SetTitle("Test")
# self.tab_one.Show()
# self.panel_one.Hide()
self.Layout()
class TabPanel2(wx.Panel):
""" This will be the first notebook tab """
def __init__(self, parent):
wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)
self.parent=parent
sizer = wx.BoxSizer(wx.VERTICAL)
#txtOne = wx.TextCtrl(self, wx.ID_ANY, "")
#txtTwo = wx.TextCtrl(self, wx.ID_ANY, "")
button1 = wx.Button(self, -1, "Button Two", (0, 20), size = (200, 30))
button1.Bind(wx.EVT_BUTTON, self.onSwitchPanels2)
sizer = wx.BoxSizer(wx.VERTICAL)
#sizer.Add(txtOne, 0, wx.ALL, 5)
#sizer.Add(txtTwo, 0, wx.ALL, 5)
self.SetSizer(sizer)
def onSwitchPanels2(self, event):
pass
#if self.tab_one.IsShown():
#self.SetTitle("Test")
#self.TabPanel.Hide()
#self.NotebookDemo.tabTwo.Show()
#else:
# self.SetTitle("Test")
# self.tab_one.Show()
# self.panel_one.Hide()
self.Layout()
class PanelOne(wx.Panel):
""" This is for panel switch """
def ___init___(self, parent):
wx.Panel.___init___(self, parent=parent, id=wx.ID_ANY)
button8 = wx.Button(self, label="T2D", pos=(0, 0))
class NotebookDemo(wx.Notebook):
""" Notebook class """
def __init__(self, parent):
wx.Notebook.__init__(self, parent, id=wx.ID_ANY, style=
wx.BK_DEFAULT
#wx.BK_TOP
#wx.BK_BOTTOM
#wx.BK_LEFT
#wx.BK_RIGHT
)
# Create the first tab and add it to the notebook
self.tabOne = TabPanel(self)
self.AddPage(self.tabOne, "TabOne")
# Create and add the second tab
self.tabTwo = TabPanel2(self)
self.AddPage(self.tabTwo, "TabTwo")
class DemoFrame(wx.Frame):
""" Frame that holds all other widgets """
def __init__(self):
"""Constructor"""
wx.Frame.__init__(self, None, wx.ID_ANY,
"Notebook Tutorial",
size=(600,400)
)
panel = wx.Panel(self)
notebook = NotebookDemo(panel)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(notebook, 1, wx.ALL|wx.EXPAND, 5)
panel.SetSizer(sizer)
self.Layout()
self.Show()
self.tab_one = TabPanel(self)
self.panel_one = PanelOne(self)
self.panel_one.Hide()
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.tab_one, 1, wx.EXPAND)
self.sizer.Add(self.panel_one, 1, wx.EXPAND)
self.SetSizer(self.sizer)
class MyApp(wx.App):
def OnInit(self):
frame = DemoFrame()
frame.Show(True)
#self.SetTopWindow(frame)
return True
app = MyApp(0)
#Colours(None, -1, 'colours.py')
app.MainLoop()