更改按钮上的多个面板单击wxpython

change multiple panel on button click wxpython

我想用 2 个面板编写简单的代码。左侧面板上的多个按钮和右侧面板上的相应页面,单击按钮时将显示。单击每个按钮都会将面板更改为相应页面,其中包含必要的输入字段以供进一步处理。我不知道我怎样才能做到这一点。 以下是示例 UI 供参考

http://imgur.com/a/xXP1l

我看到下面的代码,其逻辑在一定程度上满足了我的要求,但它打开了新面板

import wx
import wx.grid as gridlib


class PanelOne(wx.Panel):
""""""


def __init__(self, parent):
    """Constructor"""
    wx.Panel.__init__(self, parent=parent)
    txt = wx.TextCtrl(self)


class PanelTwo(wx.Panel):
""""""

#----------------------------------------------------------------------
def __init__(self, parent):
    """Constructor"""
    wx.Panel.__init__(self, parent=parent)

    grid = gridlib.Grid(self)
    grid.CreateGrid(25,12)

    sizer = wx.BoxSizer(wx.VERTICAL)
    sizer.Add(grid, 0, wx.EXPAND)
    self.SetSizer(sizer)


 class MyForm(wx.Frame):

#----------------------------------------------------------------------
def __init__(self):
    wx.Frame.__init__(self, None, wx.ID_ANY, 
                      "Panel Switcher Tutorial")

    self.panel_one = PanelOne(self)
    self.panel_two = PanelTwo(self)
    self.panel_two.Hide()

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


    menubar = wx.MenuBar()
    fileMenu = wx.Menu()
    switch_panels_menu_item = fileMenu.Append(wx.ID_ANY, 
                                              "Switch Panels", 
                                              "Some text")
    self.Bind(wx.EVT_MENU, self.onSwitchPanels, 
              switch_panels_menu_item)
    menubar.Append(fileMenu, '&File')
    self.SetMenuBar(menubar)


  def onSwitchPanels(self, event):
    """"""
    if self.panel_one.IsShown():
        self.SetTitle("Panel Two Showing")
        self.panel_one.Hide()
        self.panel_two.Show()
    else:
        self.SetTitle("Panel One Showing")
        self.panel_one.Show()
        self.panel_two.Hide()
    self.Layout()
if __name__ == "__main__":
  app = wx.App(False)
  frame =MyForm()
  frame.Show()
  app.MainLoop()

我强烈建议您查看 wx.Listbook 控件。 wxPython 演示中有一个很好的示例,或者您也可以查看此 tutorial

wxPython wiki

上也有教程