首页 > 解决方案 > 如何在面板之间切换按钮

问题描述

我无法通过按钮在我的应用程序面板之间导航。我遵循了很多这样的教程,但没有一个能解决我的问题。感谢您的帮助。

####################-----Principal Panel (first Panel)
    import wx
    from OtherPanel import OtherPanel
    class MyPanel(wx.Panel):  
        def __init__(self, parent):
            """Constructor"""
            wx.Panel.__init__(self, parent = parent, size=(600, 500))

            self.SetBackgroundColour(wx.Colour(250, 250, 250))
            parent.Center()
            self.histoPanel = OtherPanel(self)
            self.home()
            self.panel = wx.Panel(self)

        def home(self):
            self.panel = wx.Panel(self)
            self.panel.SetBackgroundColour(wx.Colour(250, 250, 250))
            bS =wx.Button(self, -1,"Go some where ",(200,30), size=(150,150))
            self.Bind(wx.EVT_BUTTON, self.Sy, bS)
            bOthe = wx.Button(self, -1,"Go to OtherPanel",(400,30),(150,150))
            self.Bind(wx.EVT_BUTTON, self.onOpenFrame,bOthe)
        def onOpenFrame(self, event):
            """
            Opens secondary frame
            """
            self.Hide()
            self.histoPanel.Show()

        def Sy(self, event):
            """
            I have another panel (panel 3)
            """
            self.Hide()
            #self.panel3.Show()

--Second Panel(另一个面板)当你点击第一个面板的按钮时,这个面板会显示出来

    import wx

    class OtherPanel(wx.Panel):

        def __init__(self, parent):
        """Constructor"""
            wx.Panel.__init__(self, parent = parent)
            self.panel = wx.Panel(self)
            self.SetBackgroundColour(wx.Colour(250, 250, 250))
            self.Center()
            self.homeHist()
            self.Show()
        def homeHist(self):
            bBack = wx.Button(self, -1, "", pos=(20,30), size=(50,50))
            self.Bind(wx.EVT_BUTTON, self.onClose, bBack)

        def onClose(self, event):
            self.Close()

------我的框架---。这是我的框架/窗口。他必须依次佩戴所有的面板

    import wx
    from MyPanel import MyPanel
    from OtherPanel import OtherPanel

    class MyFrame(wx.Frame):
        """"""
        def __init__(self):
            """Constructor"""
            wx.Frame.__init__(self, None, size = (600,500))
            self.myPanel = MyPanel(self)
            self.otherpanel = OtherPanel(self)
            self.otherpanel.Hide()
            self.menu()

            self.CreateStatusBar(style= wx.BORDER_NONE)
            self.SetBackgroundColour("gray")
            self.SetStatusText("\tthank")


        def menu(self):

            menubar = wx.MenuBar()
            fileMenu = wx.Menu()
            menubar.SetBackgroundColour(wx.Colour(1, 1, 6))

            fileMenu.AppendSeparator()
            live = wx.MenuItem(fileMenu, wx.ID_EXIT, '&Quit\tCtrl+Q', '\tlive my app')
            fileMenu.Append(live)
            self.Bind(wx.EVT_MENU, self.Onexit, live)

            menubar.Append(fileMenu, '\t&Menu')
            self.SetMenuBar(menubar)

        def Onexit(self, event):
            self.Close(True)

        def Bouton(self, event):
            if self.myPanel.IsShown():
                self.SetTitle("Panel Two Showing")
                self.myPanel.Hide()
                self.otherpanel.Show()
            else:
                self.SetTitle("Panel One Showing")
                self.myPanel.Show()
                self.otherpanel.Hide()
            self.Layout()

        if __name__ == "__main__":
            app = wx.App(False)
            frame = MyFrame()
            frame.Show()
            app.MainLoop()

标签: python-3.xwxpython

解决方案


以下是罗宾在他的回答中建议的一个例子:

import wx

class MyPanel(wx.Panel):
    def __init__(self, parent):
        super().__init__(parent)
        self.parent = parent

        self.panel = wx.Panel(self)
        self.btn = wx.Button(self.panel, label="Panel 1", size=(250,75))
        self.btn.Bind(wx.EVT_BUTTON, self.switch)

        vbox1 = wx.BoxSizer(wx.VERTICAL)
        vbox1.Add(self.btn)
        self.panel.SetSizer(vbox1)

        vbox = wx.BoxSizer(wx.VERTICAL)
        vbox.Add(self.panel)
        self.SetSizer(vbox)

        self.Show()

    def switch(self, event):
        self.parent.Swap()

class MyOtherPanel(wx.Panel):
    def __init__(self, parent):
        super().__init__(parent)
        self.parent = parent

        self.panel = wx.Panel(self)
        self.btn = wx.Button(self.panel, label="Panel 2", size=(175,250))
        self.btn.Bind(wx.EVT_BUTTON, self.switch)

        vbox1 = wx.BoxSizer(wx.VERTICAL)
        vbox1.Add(self.btn)
        self.panel.SetSizer(vbox1)

        vbox = wx.BoxSizer(wx.VERTICAL)
        vbox.Add(self.panel)
        self.SetSizer(vbox)

        self.Show()
        self.panel.Hide()

    def switch(self, event):
        self.parent.Swap()

class PanelSwitcher(wx.Frame):
    def __init__(self):
        super().__init__(None)

        vbox = wx.BoxSizer(wx.VERTICAL)
        self.panel1 = MyPanel(self)
        self.panel2 = MyOtherPanel(self)
        vbox.Add(self.panel1)
        vbox.Add(self.panel2)
        self.SetSizer(vbox)
        self.Show()

    def Swap(self):
        if self.panel1.panel.IsShown():
            self.panel1.panel.Hide()
            self.panel2.panel.Show()
        else:
            self.panel2.panel.Hide()
            self.panel1.panel.Show()
        self.Layout()


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

推荐阅读