无法切换到新面板

时间:2014-07-14 02:38:40

标签: python wxpython

点击按钮后,我正在尝试切换新面板。我创建了一个带有两个选项卡(笔记本)的面板,每个选项卡都有按钮。单击这些按钮将切换到新面板。但是,我遇到了一些错误。这是我的代码:

import wx

page1 = None;
page2 = None;

# create a new panel
class NewPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        panel = wx.Panel(self, size = (800, 600))
        vbox = wx.BoxSizer(wx.VERTICAL)
        button8 = wx.Button(self, label="Button Two", pos=(0, 0))

# the first notebook created on the panel.
class BookOne(wx.Panel): 
    def __init__(self, parent):        
        wx.Panel.__init__(self, parent)
        panel = wx.Panel(self, size = (800,600))

        vbox = wx.BoxSizer(wx.VERTICAL)

        button1 = wx.Button(panel, -1, "Button One", (0, 20), size = (200, 30))
        button1.Bind(wx.EVT_BUTTON, self.onSwitchPanels1)

    def onSwitchPanels1(self, event):
        if self.page1.IsShown():
           self.SetTitle("Panel")
           self.page1.Hide()
           self.new_panel.Show()
        else:
           self.SetTitle("Panel")
           self.page1.Show()
           self.new_panel.Hide()
        self.Layout()


# second notebook created on the same panel.
class BookTwo(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        panel = wx.Panel(self, size = (800,600))
        #self.mypanel = wx.Panel(self, -1, size = (800, 600))

        vbox = wx.BoxSizer(wx.VERTICAL)

        button7 = wx.Button(panel, -1, "Button A", (0, 20), size = (200, 30))
        #button7.Bind(wx.EVT_BUTTON, parent.onSwitchPanels7)
        button8 = wx.Button(panel, -1, "Button B", (0, 70), size = (200, 30))
        #button8.Bind(wx.EVT_BUTTON, parent.onSwitchPanels8)


# the main frame/panel/windows.
class MainFrame(wx.Frame):
    def __init__(self):
        self.something  = "something";
        wx.Frame.__init__(self, None,title="My Panel", size=(800, 600))

        # Here we create a panel and a notebook on the panel
        p = wx.Panel(self)
        nb = wx.Notebook(p)

        global page1,page2;

        # create the page windows as children of the notebook
        page1 = BookOne(nb)
        page2 = BookTwo(nb)

        # add the pages to the notebook with the label to show on the tab
        nb.AddPage(page1, "Tab One")
        nb.AddPage(page2, "Tab Two")


        # finally, put the notebook in a sizer for the panel to manage
        # the layout
        sizer = wx.BoxSizer()
        sizer.Add(nb, 1, wx.EXPAND)
        p.SetSizer(sizer)


        self.page1 = BookOne(nb)
        self.page1.Hide()
        self.new_panel = NewPanel(p)
        self.new_panel.Hide()

        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer.Add(self.page1, 1, wx.EXPAND)
        self.sizer.Add(self.new_panel, 1, wx.EXPAND)
        #self.SetSizer(self.sizer)


if __name__ == "__main__":
    app = wx.App()
    MainFrame().Show()
    app.MainLoop()

我尝试了一些解决方案,但在点击“按钮1”按钮后,第二个面板不会出现。

1 个答案:

答案 0 :(得分:0)

首先,当我点击Button One

时,您会收到错误消息
Traceback (most recent call last):
  File "<pyshell#10>", line 28, in onSwitchPanels1
AttributeError: 'PageOne' object has no attribute 'page1'

修改

我做了一些修改 - 现在它显示/隐藏tab threetab four
但我认为我并不了解你到底该做什么。

也许这段代码可以帮助你做你真正需要的事情。

最重要的部分是.GetParent() - 我用它来访问主类中的元素。

我删除了global因为我认为我们使用类不使用globals

import wx
import os

wildcard = "All files (*.*)|*.*"

class PanelThree(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)

        vbox = wx.BoxSizer(wx.VERTICAL)

        button8 = wx.Button(self, label="Button Two", pos=(0, 0))

# the first tab created.
class PageOne(wx.Panel): 
    def __init__(self, parent):        
        wx.Panel.__init__(self, parent)

        vbox = wx.BoxSizer(wx.VERTICAL)

        button1 = wx.Button(self, -1, "Button One", (0, 20), size = (200, 30))
        button1.Bind(wx.EVT_BUTTON, self.onSwitchPanels1)

    def onSwitchPanels1(self, event):
        if self.GetParent().GetParent().GetParent().page1.IsShown():
           #self.SetTitle("Panel")
           self.GetParent().GetParent().GetParent().page1.Hide()
           self.GetParent().GetParent().GetParent().panel_two.Show()
        else:
           #self.SetTitle("Panel")
           self.GetParent().GetParent().GetParent().page1.Show()
           self.GetParent().GetParent().GetParent().panel_two.Hide()
        self.Layout()


# second tab created.
class PageTwo(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)

        vbox = wx.BoxSizer(wx.VERTICAL)

        button7 = wx.Button(self, -1, "Button A", (0, 20), size = (200, 30))
        #button7.Bind(wx.EVT_BUTTON, parent.onSwitchPanels7)
        button8 = wx.Button(self, -1, "Button B", (0, 70), size = (200, 30))
        #button8.Bind(wx.EVT_BUTTON, parent.onSwitchPanels8)


# the main frame/panel/windows.
class MainFrame(wx.Frame):
    def __init__(self):
        self.something  = "something";
        wx.Frame.__init__(self, None,title="My Panel", size=(800, 600))

        # Here we create a panel and a notebook on the panel
        p = wx.Panel(self)
        nb = wx.Notebook(p)

        # create the page windows as children of the notebook
        self.page1a = PageOne(nb)
        self.page2a = PageTwo(nb)

        # add the pages to the notebook with the label to show on the tab
        nb.AddPage(self.page1a, "Tab One")
        nb.AddPage(self.page2a, "Tab Two")

        # finally, put the notebook in a sizer for the panel to manage
        # the layout
        sizer = wx.BoxSizer()
        sizer.Add(nb, 1, wx.EXPAND)
        p.SetSizer(sizer)

        self.page1 = PageOne(nb)
        self.page1.Hide()
        self.panel_two = PanelThree(nb)
        self.panel_two.Hide()

        nb.AddPage(self.page1, "Tab Three")
        nb.AddPage(self.panel_two, "Tab Four")

        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer.Add(self.page1, 1, wx.EXPAND)
        self.sizer.Add(self.panel_two, 1, wx.EXPAND)
        #self.SetSizer(self.sizer)


if __name__ == "__main__":
    app = wx.App()
    MainFrame().Show()
    app.MainLoop()