Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 无法切换到新面板_Python_Wxpython - Fatal编程技术网

Python 无法切换到新面板

Python 无法切换到新面板,python,wxpython,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)

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

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()

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

首先,当我单击按钮一时,您会出现错误

Traceback (most recent call last):
  File "<pyshell#10>", line 28, in onSwitchPanels1
AttributeError: 'PageOne' object has no attribute 'page1'

在console/terminal/cmd.exe中运行时,是否收到任何错误消息?如果是,则将所有错误消息置于问题中。是否要在选项卡内创建新面板?还是要创建下一个选项卡?我也有同样的错误。我尝试了一些解决方案,但仍然不起作用。我想在点击按钮“button One”后打开一个新的面板。我做了一些修改,但我不知道我是否理解你真正想做的事情。也许我的问题对你来说不够清楚。我已经编辑了我的问题。顺便说一句,谢谢你的帮助。我主要是想理解你的代码——以及为什么我感到困惑。旁边有三个词:面板,标签,页面。有时您将其用作具有相同含义的单词-例如在两行中
#创建的第一个选项卡。
类PageOne(wx.Panel):
-您有
选项卡
,但类有名称
页面
,它使用类
面板
。但是你用了不同含义的词-
我创建了一个有两个选项卡的面板
-可能你指的是有两个面板(选项卡)的
笔记本
(如果我们在代码中使用类名)
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()