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

I want to call function onSwitchPanels when i click the button in class PanelOne.This application i want to work like this code in Tkinter.

I have stack guys, help me and thanks a lot.

Community
  • 1
  • 1
TLSK
  • 275
  • 1
  • 6
  • 25

1 Answers1

7

I wrote a tutorial on this subject over a year ago, although I use a menu to do the switching. You can adjust the code to make your button do it though. Here's the tutorial: http://www.blog.pythonlibrary.org/2010/06/16/wxpython-how-to-switch-between-panels/

EDIT: The problem with the code above is three-fold. First in the onSwitchPanels method, you need to have "self.panel_one.Hide()". Note the parentheses that are missing in your code. Secondly, you really need to have the "self.Layout()" un-indented so it's on the same level as the if statement, NOT inside the "else" part. Lastly, you can't call "onSwitchPanels" from PanelOne because it's not defined there. You can change it so it's like this though:

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

Ugly and not really recommended, but it works. You should use PubSub for that instead though.

EDIT #2: Guess I should have just posted the code since the OP won't even try my suggestions.

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()
Mike Driscoll
  • 32,629
  • 8
  • 45
  • 88
  • Can you help some more, and my code i found it in the blog you have post, nice work. – TLSK Jan 10 '12 at 21:21
  • Yes i know about the tree-fold problems,but know when i click the button i can see both panels in the same frame,i want to show only the second panel. – TLSK Jan 10 '12 at 21:37
  • 1
    I added the fixed example. I don't know why you're having issues. My code works. – Mike Driscoll Jan 10 '12 at 22:00
  • Thanks a lot its ok now,i fixed it was my mistake. – TLSK Jan 10 '12 at 22:26
  • +1 "Guess I should have just posted the code since the OP won't even try my suggestions" That's much more common than I never thought – joaquin Jan 10 '12 at 23:39