2012-01-10 117 views
1
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) 
     button =wx.Button(self, label="Save", pos=(200, 325)) 
     button.Bind(wx.EVT_BUTTON, self.onSwitchPanels) 

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


# Run the program 
if __name__ == "__main__": 
    app = wx.App(False) 
    frame = MyForm() 
    frame.Show() 
    app.MainLoop() 

更改面板我想打電話給我時,請在類PanelOne.This應用程序的按鈕,我想喜歡this code in Tkinter工作功能onSwitchPanels。與按鈕的wxPython

我有堆疊傢伙,幫助我,非常感謝。

回答

7

我在一年前編寫了關於這個主題的教程,雖然我使用菜單來切換。你可以調整代碼來讓你的按鈕做到這一點。這裏的教程:http://www.blog.pythonlibrary.org/2010/06/16/wxpython-how-to-switch-between-panels/

編輯:上面的代碼的問題是三重。首先在onSwitchPanels方法中,您需要具有「self.panel_one.Hide()」。請注意代碼中缺少的括號。其次,你真的需要「self.Layout()」不縮進,所以它和if語句在同一層,而不在「else」部分。最後,你不能從PanelOne調用「onSwitchPanels」,因爲它沒有在那裏定義。所以它是這樣的,雖然你可以改變它:

button.Bind(wx.EVT_BUTTON, parent.onSwitchPanels) 

長得醜,而且並不推薦使用,但它的作品。不過,您應該使用PubSub。

編輯#2:猜猜我應該剛剛發佈的代碼,因爲OP甚至不會嘗試我的建議。

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) 
     button =wx.Button(self, label="Save", pos=(200, 325)) 
     button.Bind(wx.EVT_BUTTON, parent.onSwitchPanels) 

######################################################################## 
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", 
          size=(800,600)) 

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

# Run the program 
if __name__ == "__main__": 
    app = wx.App(False) 
    frame = MyForm() 
    frame.Show() 
    app.MainLoop() 
+0

你可以幫助更多,我的代碼我發現它在你發佈的博客,很好的工作。 – TLSK 2012-01-10 21:21:17

+0

但你知道已經:) – TLSK 2012-01-10 21:22:12

+0

是的我知道樹摺疊問題,但知道當我點擊按鈕,我可以看到在同一幀中的兩個面板,我只想顯示第二個面板。 – TLSK 2012-01-10 21:37:24