2016-05-05 136 views
0

我希望在框架中占据相同空间的两个面板(我将在后面添加更多面板),并在按下各个按钮时显示/隐藏面板工具栏,“mListPanel”应该是默认的。目前,启动应用程序并且按钮不执行任何操作时会显示设置面板。我搜索了几个小时,并尝试了很多东西,仍然无法使其工作。我很抱歉,如果这很简单,我今天才开始学习python。wxPython:使用按钮在多个面板之间切换

这是代码看起来像现在:

import wx 

    class mListPanel(wx.Panel): 
     def __init__(self, parent): 
      wx.Panel.__init__(self, parent=parent) 
       #wx.StaticText(self, -1, label='Search:')#, pos=(10, 3)) 
       #wx.TextCtrl(self, pos=(10, 10), size=(250, 50)) 

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

    class bifr(wx.Frame): 
     def __init__(self): 
      wx.Frame.__init__(self, None, wx.ID_ANY, "Title") 

      self.listPanel = mListPanel(self) 
      self.optPanel = settingsPanel(self) 

      menuBar = wx.MenuBar() 
      fileButton = wx.Menu() 
      importItem = wx.Menu() 
      fileButton.AppendMenu(wx.ID_ADD, 'Add M', importItem) 
      importItem.Append(wx.ID_ANY, 'Import from computer') 
      importItem.Append(wx.ID_ANY, 'Import from the internet') 
      exitItem = fileButton.Append(wx.ID_EXIT, 'Exit') 
      menuBar.Append(fileButton, 'File') 
      self.SetMenuBar(menuBar) 
      self.Bind(wx.EVT_MENU, self.Quit, exitItem) 

      toolBar = self.CreateToolBar() 
      homeToolButton = toolBar.AddLabelTool(wx.ID_ANY, 'Home', wx.Bitmap('icons/home_icon&32.png')) 
      importLocalToolButton = toolBar.AddLabelTool(wx.ID_ANY, 'Import from computer', wx.Bitmap('icons/comp_icon&32.png')) 
      importToolButton = toolBar.AddLabelTool(wx.ID_ANY, 'Import from the internet', wx.Bitmap('icons/arrow_bottom_icon&32.png')) 
      settingsToolButton = toolBar.AddLabelTool(wx.ID_ANY, 'settings', wx.Bitmap('icons/wrench_plus_2_icon&32.png')) 
      toolBar.Realize() 

      self.Bind(wx.EVT_TOOL, self.switchPanels(), settingsToolButton) 
      self.Bind(wx.EVT_TOOL, self.switchPanels(), homeToolButton) 
      self.Layout() 

     def switchPanels(self): 
      if self.optPanel.IsShown(): 
       self.optPanel.Hide() 
       self.listPanel.Show() 
       self.SetTitle("Home") 
      elif self.listPanel.IsShown(): 
       self.listPanel.Hide() 
       self.optPanel.Show() 
       self.SetTitle("Settings") 
      else: 
       self.SetTitle("Error") 
      self.Layout() 

     def Quit(self, e): 
      self.Close() 

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

回答

0

首先,我会强烈建议您了解wxpython sizers,并让他们有很好的理解(他们真的没有那么难的理解)尽快深入探讨wxpython,只是一个友好的提示:)。

至于你的例子,一些事情: 当你不使用sizers时,你必须给每个窗口的大小和位置,否则他们不会显示,所以你必须改变你的面板类为这样的事情(这同样是仅用于演示,你应该wx.sizers可以这样做,而不是位置和大小):

class mListPanel(wx.Panel): 
    def __init__(self, parent): 
     wx.Panel.__init__(self, parent=parent,pos=(0,100),size=(500,500)) 

class settingsPanel(wx.Panel): 
    def __init__(self, parent): 
     wx.Panel.__init__(self, parent=parent,pos=(0,200),size (1000,1000)) 

进一步,结合一个事件时,它应该是这样的:

self.Bind(wx.EVT_TOOL, self.switchPanels, settingsToolButton) 
self.Bind(wx.EVT_TOOL, self.switchPanels, homeToolButton) 

请注意我是如何写出只有函数没有添加(),因为一个事件传递给它,你不能输入你自己的参数到一个事件发出的函数(除非你用下面的语法lambda e:FooEventHandler(paramaters))做它

和事件处理(功能)应该是这样的:

def switchPanels(self, event): 
    if self.optPanel.IsShown(): 
     self.optPanel.Hide() 
     self.listPanel.Show() 
     self.SetTitle("Home") 
    elif self.listPanel.IsShown(): 
     self.listPanel.Hide() 
     self.optPanel.Show() 
     self.SetTitle("Settings") 
    else: 
     self.SetTitle("Error") 
    self.Layout() 

存在应始终是结合事件作为事件对象传递有功能放在自己第二个参数,你可以找到它的相关方法和参数(在这个例子中是wx.EVT_TOOL)。