Cannot open a new panel

I’m trying to open a new panel after I clicked button. I created a panel with two tabs and each tab has buttons. This button will open a new panel when clicked. However, I faced some problems. This is my code:

import wx

import os

page1 = None;

page2 = None;

class PanelThree(wx.Panel):

def init(self, parent):

wx.Panel.init(self, parent)

panel = wx.Panel(self, size = (800, 600))

vbox = wx.BoxSizer(wx.VERTICAL)

button8 = wx.Button(self, label=“Button Two”, pos=(0, 0))

···

**

# the first tab created.

*class PageOne(wx.Panel): *

*def init(self, parent): *

wx.Panel.init(self, parent)

panel = wx.Panel(self, size = (800,600))

vbox = wx.BoxSizer(wx.VERTICAL)

button1 = wx.Button(panel, -1, “Button One”, (0, 20), size = (200, 30))

button1.Bind(wx.EVT_BUTTON, self.onSwitchPanels1)

**

def onSwitchPanels1(self, event):

if self.page1.IsShown():

self.SetTitle(“Panel”)

self.page1.Hide()

self.panel_two.Show()

else:

self.SetTitle(“Panel”)

self.page1.Show()

self.panel_two.Hide()

self.Layout()

# second tab created.

class PageTwo(wx.Panel):

def init(self, parent):

wx.Panel.init(self, parent)

panel = wx.Panel(self, size = (800,600))

#self.mypanel = wx.Panel(self, -1, size = (800, 600))

vbox = wx.BoxSizer(wx.VERTICAL)

button7 = wx.Button(panel, -1, “Button A”, (0, 20), size = (200, 30))

#button7.Bind(wx.EVT_BUTTON, parent.onSwitchPanels7)

button8 = wx.Button(panel, -1, “Button B”, (0, 70), size = (200, 30))

#button8.Bind(wx.EVT_BUTTON, parent.onSwitchPanels8)

# the main frame/panel/windows.

class MainFrame(wx.Frame):

def init(self):

self.something = “something”;

wx.Frame.init(self, None,title=“My Panel”, size=(800, 600))

#grid = TableGrid(self)

**

# Here we create a panel and a notebook on the panel

p = wx.Panel(self)

nb = wx.Notebook(p)

global page1,page2;

**

# create the page windows as children of the notebook

page1 = PageOne(nb)

page2 = PageTwo(nb)

# add the pages to the notebook with the label to show on the tab

nb.AddPage(page1, “Tab One”)

nb.AddPage(page2, “Tab Two”)

**

# finally, put the notebook in a sizer for the panel to manage

# the layout

sizer = wx.BoxSizer()

sizer.Add(nb, 1, wx.EXPAND)

p.SetSizer(sizer)

**

self.page1 = PageOne(nb)

self.page1.Hide()

self.panel_two = PanelThree(p)

self.panel_two.Hide()

self.sizer = wx.BoxSizer(wx.VERTICAL)

self.sizer.Add(self.page1, 1, wx.EXPAND)

self.sizer.Add(self.panel_two, 1, wx.EXPAND)

#self.SetSizer(self.sizer)

**

if name == “main”:

app = wx.App()

MainFrame().Show()

app.MainLoop()

I’ve tried some solutions but the second panel won’t appear.

Move your to
MainFrame - when you bind the button(s) bind to
parent.onSwitchPanels1 and get rid of the globals, use self.page1,
etc in MainFrame. You need to a) avoid globals and b) make sure
that your methods have the thing they are referencing in scope.

···

On 14/07/14 03:53, Noor wrote:

      I'm

trying to open a new panel after I clicked button. I created a
panel with two tabs and each tab has buttons. This button will
open a new panel when clicked. However, I faced some problems.
This is my code:

import wx

import os

page1 = None;

page2 = None;

  •              class
    

PanelThree(wx.Panel):*

  •              def
    

init(self, parent):*

wx.Panel.init(self, parent)

  •              panel =
    

wx.Panel(self, size = (800, 600))*

  •              vbox =
    

wx.BoxSizer(wx.VERTICAL)*

  •              button8 =
    

wx.Button(self, label=“Button Two”, pos=(0, 0))*

**

  •              # the first tab
    

created.*

  •              class
    

PageOne(wx.Panel): *

  •              def
    

init(self, parent): *

wx.Panel.init(self, parent)

  •              panel =
    

wx.Panel(self, size = (800,600))*

  •              vbox =
    

wx.BoxSizer(wx.VERTICAL)*

  •              button1 =
    

wx.Button(panel, -1, “Button One”, (0, 20), size =
(200, 30))*

button1.Bind(wx.EVT_BUTTON, self.onSwitchPanels1)

**

  •              def
    

onSwitchPanels1(self, event):*

  •              if
    

self.page1.IsShown():*

self.SetTitle(“Panel”)

self.page1.Hide()

self.panel_two.Show()

else:

self.SetTitle(“Panel”)

self.page1.Show()

self.panel_two.Hide()

self.Layout()

  •              # second tab
    

created.*

  •              class
    

PageTwo(wx.Panel):*

  •              def
    

init(self, parent):*

wx.Panel.init(self, parent)

  •              panel =
    

wx.Panel(self, size = (800,600))*

#self.mypanel = wx.Panel(self, -1, size = (800, 600))

  •              vbox =
    

wx.BoxSizer(wx.VERTICAL)*

  •              button7 =
    

wx.Button(panel, -1, “Button A”, (0, 20), size = (200,
30))*

#button7.Bind(wx.EVT_BUTTON, parent.onSwitchPanels7)

  •              button8 =
    

wx.Button(panel, -1, “Button B”, (0, 70), size = (200,
30))*

#button8.Bind(wx.EVT_BUTTON, parent.onSwitchPanels8)

  •              # the main
    

frame/panel/windows.*

  •              class
    

MainFrame(wx.Frame):*

  •              def
    

init(self):*

self.something = “something”;

  •              wx.Frame.__init__(self, None,title="My Panel",
    

size=(800, 600))*

  •              #grid =
    

TableGrid(self)*

**

  •              # Here we
    

create a panel and a notebook on the panel*

  •              p =
    

wx.Panel(self)*

  •              nb =
    

wx.Notebook(p)*

  •              global
    

page1,page2;*

**

  •              # create
    

the page windows as children of the notebook*

  •              page1 =
    

PageOne(nb)*

  •              page2 =
    

PageTwo(nb)*

  •              # add the
    

pages to the notebook with the label to show on the
tab*

nb.AddPage(page1, “Tab One”)

nb.AddPage(page2, “Tab Two”)

**

  •              #
    

finally, put the notebook in a sizer for the panel to
manage*

  •              # the
    

layout*

  •              sizer =
    

wx.BoxSizer()*

sizer.Add(nb, 1, wx.EXPAND)

p.SetSizer(sizer)

**

self.page1 = PageOne(nb)

self.page1.Hide()

self.panel_two = PanelThree(p)

self.panel_two.Hide()

self.sizer = wx.BoxSizer(wx.VERTICAL)

self.sizer.Add(self.page1, 1, wx.EXPAND)

self.sizer.Add(self.panel_two, 1, wx.EXPAND)

#self.SetSizer(self.sizer)

**

  •              if __name__ ==
    

main”:*

  •              app =
    

wx.App()*

MainFrame().Show()

app.MainLoop()

      I've tried some solutions

but the second panel won’t appear.

  You received this message because you are subscribed to the Google

Groups “wxPython-users” group.

  To unsubscribe from this group and stop receiving emails from it,

send an email to wxpython-users+unsubscribe@googlegroups.com.

  For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

onSwitchPanels1

There is a lot of code in here that does nothing, or nothing useful:
within your panel classes, you are creating another panel, which I don’t think
you intend e,g,:
panel = wx.Panel(…)
“self” is already referring to your panel. You are putting a panel in a panel. Is that intentional?
You are also defining BoxSizers on the panels which do nothing.

In your MainFrame you are actually creating two PageOne’s in an attempt to hide PageOne (I think).

The WIT will help you a lot, learn to use it.

···

On Sunday, July 13, 2014 10:53:45 PM UTC-4, Noor wrote:

I’m trying to open a new panel after I clicked button. I created a panel with two tabs and each tab has buttons. This button will open a new panel when clicked. However, I faced some problems. This is my code:

import wx

import os

page1 = None;

page2 = None;

class PanelThree(wx.Panel):

def init(self, parent):

wx.Panel.init(self, parent)

panel = wx.Panel(self, size = (800, 600))

vbox = wx.BoxSizer(wx.VERTICAL)

button8 = wx.Button(self, label=“Button Two”, pos=(0, 0))

**

# the first tab created.

*class PageOne(wx.Panel): *

*def init(self, parent): *

wx.Panel.init(self, parent)

panel = wx.Panel(self, size = (800,600))

vbox = wx.BoxSizer(wx.VERTICAL)

button1 = wx.Button(panel, -1, “Button One”, (0, 20), size = (200, 30))

button1.Bind(wx.EVT_BUTTON, self.onSwitchPanels1)

**

def onSwitchPanels1(self, event):

if self.page1.IsShown():

self.SetTitle(“Panel”)

self.page1.Hide()

self.panel_two.Show()

else:

self.SetTitle(“Panel”)

self.page1.Show()

self.panel_two.Hide()

self.Layout()

# second tab created.

class PageTwo(wx.Panel):

def init(self, parent):

wx.Panel.init(self, parent)

panel = wx.Panel(self, size = (800,600))

#self.mypanel = wx.Panel(self, -1, size = (800, 600))

vbox = wx.BoxSizer(wx.VERTICAL)

button7 = wx.Button(panel, -1, “Button A”, (0, 20), size = (200, 30))

#button7.Bind(wx.EVT_BUTTON, parent.onSwitchPanels7)

button8 = wx.Button(panel, -1, “Button B”, (0, 70), size = (200, 30))

#button8.Bind(wx.EVT_BUTTON, parent.onSwitchPanels8)

# the main frame/panel/windows.

class MainFrame(wx.Frame):

def init(self):

self.something = “something”;

wx.Frame.init(self, None,title=“My Panel”, size=(800, 600))

#grid = TableGrid(self)

**

# Here we create a panel and a notebook on the panel

p = wx.Panel(self)

nb = wx.Notebook(p)

global page1,page2;

**

# create the page windows as children of the notebook

page1 = PageOne(nb)

page2 = PageTwo(nb)

# add the pages to the notebook with the label to show on the tab

nb.AddPage(page1, “Tab One”)

nb.AddPage(page2, “Tab Two”)

**

# finally, put the notebook in a sizer for the panel to manage

# the layout

sizer = wx.BoxSizer()

sizer.Add(nb, 1, wx.EXPAND)

p.SetSizer(sizer)

**

self.page1 = PageOne(nb)

self.page1.Hide()

self.panel_two = PanelThree(p)

self.panel_two.Hide()

self.sizer = wx.BoxSizer(wx.VERTICAL)

self.sizer.Add(self.page1, 1, wx.EXPAND)

self.sizer.Add(self.panel_two, 1, wx.EXPAND)

#self.SetSizer(self.sizer)

**

if name == “main”:

app = wx.App()

MainFrame().Show()

app.MainLoop()

I’ve tried some solutions but the second panel won’t appear.

in PageOne class you have only the self Panel, and the panel Panel that you created… there is no self.page1 or self.panel_two… you created those in a different class, and you didn’t tell the PageOne object about them (by passing them as an argument/parameter).

If you want the button on the first tab to cause that tab’s contents to change, then you should create both panels in PageOne, and save them as class variables (e.g. self.panel1 and self.panel2) then in your button method, you can Hide() and Show() on the self.panel1 or self.panel2 objects.

···

On Sunday, July 13, 2014 7:53:45 PM UTC-7, Noor wrote:

I’m trying to open a new panel after I clicked button. I created a panel with two tabs and each tab has buttons. This button will open a new panel when clicked. However, I faced some problems. This is my code:

import wx

import os

page1 = None;

page2 = None;

class PanelThree(wx.Panel):

def init(self, parent):

wx.Panel.init(self, parent)

panel = wx.Panel(self, size = (800, 600))

vbox = wx.BoxSizer(wx.VERTICAL)

button8 = wx.Button(self, label=“Button Two”, pos=(0, 0))

**

# the first tab created.

*class PageOne(wx.Panel): *

*def init(self, parent): *

wx.Panel.init(self, parent)

panel = wx.Panel(self, size = (800,600))

vbox = wx.BoxSizer(wx.VERTICAL)

button1 = wx.Button(panel, -1, “Button One”, (0, 20), size = (200, 30))

button1.Bind(wx.EVT_BUTTON, self.onSwitchPanels1)

**

def onSwitchPanels1(self, event):

if self.page1.IsShown():

self.SetTitle(“Panel”)

self.page1.Hide()

self.panel_two.Show()

else:

self.SetTitle(“Panel”)

self.page1.Show()

self.panel_two.Hide()

self.Layout()

# second tab created.

class PageTwo(wx.Panel):

def init(self, parent):

wx.Panel.init(self, parent)

panel = wx.Panel(self, size = (800,600))

#self.mypanel = wx.Panel(self, -1, size = (800, 600))

vbox = wx.BoxSizer(wx.VERTICAL)

button7 = wx.Button(panel, -1, “Button A”, (0, 20), size = (200, 30))

#button7.Bind(wx.EVT_BUTTON, parent.onSwitchPanels7)

button8 = wx.Button(panel, -1, “Button B”, (0, 70), size = (200, 30))

#button8.Bind(wx.EVT_BUTTON, parent.onSwitchPanels8)

# the main frame/panel/windows.

class MainFrame(wx.Frame):

def init(self):

self.something = “something”;

wx.Frame.init(self, None,title=“My Panel”, size=(800, 600))

#grid = TableGrid(self)

**

# Here we create a panel and a notebook on the panel

p = wx.Panel(self)

nb = wx.Notebook(p)

global page1,page2;

**

# create the page windows as children of the notebook

page1 = PageOne(nb)

page2 = PageTwo(nb)

# add the pages to the notebook with the label to show on the tab

nb.AddPage(page1, “Tab One”)

nb.AddPage(page2, “Tab Two”)

**

# finally, put the notebook in a sizer for the panel to manage

# the layout

sizer = wx.BoxSizer()

sizer.Add(nb, 1, wx.EXPAND)

p.SetSizer(sizer)

**

self.page1 = PageOne(nb)

self.page1.Hide()

self.panel_two = PanelThree(p)

self.panel_two.Hide()

self.sizer = wx.BoxSizer(wx.VERTICAL)

self.sizer.Add(self.page1, 1, wx.EXPAND)

self.sizer.Add(self.panel_two, 1, wx.EXPAND)

#self.SetSizer(self.sizer)

**

if name == “main”:

app = wx.App()

MainFrame().Show()

app.MainLoop()

I’ve tried some solutions but the second panel won’t appear.

also in init you’re creating two PageOne objects:
page1 = PageOne(nb)

and

self.page1 = PageOne(nb)

these are two unique objects… I doubt this is what you wanted… but it is hard to tell.

···

On Sunday, July 13, 2014 7:53:45 PM UTC-7, Noor wrote:

I’m trying to open a new panel after I clicked button. I created a panel with two tabs and each tab has buttons. This button will open a new panel when clicked. However, I faced some problems. This is my code:

import wx

import os

page1 = None;

page2 = None;

class PanelThree(wx.Panel):

def init(self, parent):

wx.Panel.init(self, parent)

panel = wx.Panel(self, size = (800, 600))

vbox = wx.BoxSizer(wx.VERTICAL)

button8 = wx.Button(self, label=“Button Two”, pos=(0, 0))

**

# the first tab created.

*class PageOne(wx.Panel): *

*def init(self, parent): *

wx.Panel.init(self, parent)

panel = wx.Panel(self, size = (800,600))

vbox = wx.BoxSizer(wx.VERTICAL)

button1 = wx.Button(panel, -1, “Button One”, (0, 20), size = (200, 30))

button1.Bind(wx.EVT_BUTTON, self.onSwitchPanels1)

**

def onSwitchPanels1(self, event):

if self.page1.IsShown():

self.SetTitle(“Panel”)

self.page1.Hide()

self.panel_two.Show()

else:

self.SetTitle(“Panel”)

self.page1.Show()

self.panel_two.Hide()

self.Layout()

# second tab created.

class PageTwo(wx.Panel):

def init(self, parent):

wx.Panel.init(self, parent)

panel = wx.Panel(self, size = (800,600))

#self.mypanel = wx.Panel(self, -1, size = (800, 600))

vbox = wx.BoxSizer(wx.VERTICAL)

button7 = wx.Button(panel, -1, “Button A”, (0, 20), size = (200, 30))

#button7.Bind(wx.EVT_BUTTON, parent.onSwitchPanels7)

button8 = wx.Button(panel, -1, “Button B”, (0, 70), size = (200, 30))

#button8.Bind(wx.EVT_BUTTON, parent.onSwitchPanels8)

# the main frame/panel/windows.

class MainFrame(wx.Frame):

def init(self):

self.something = “something”;

wx.Frame.init(self, None,title=“My Panel”, size=(800, 600))

#grid = TableGrid(self)

**

# Here we create a panel and a notebook on the panel

p = wx.Panel(self)

nb = wx.Notebook(p)

global page1,page2;

**

# create the page windows as children of the notebook

page1 = PageOne(nb)

page2 = PageTwo(nb)

# add the pages to the notebook with the label to show on the tab

nb.AddPage(page1, “Tab One”)

nb.AddPage(page2, “Tab Two”)

**

# finally, put the notebook in a sizer for the panel to manage

# the layout

sizer = wx.BoxSizer()

sizer.Add(nb, 1, wx.EXPAND)

p.SetSizer(sizer)

**

self.page1 = PageOne(nb)

self.page1.Hide()

self.panel_two = PanelThree(p)

self.panel_two.Hide()

self.sizer = wx.BoxSizer(wx.VERTICAL)

self.sizer.Add(self.page1, 1, wx.EXPAND)

self.sizer.Add(self.panel_two, 1, wx.EXPAND)

#self.SetSizer(self.sizer)

**

if name == “main”:

app = wx.App()

MainFrame().Show()

app.MainLoop()

I’ve tried some solutions but the second panel won’t appear.

I don't think "Hiding" a wx.Notebook page is directly possible.

You can use nb.DeletePage(), AddPage() and InsertPage().
I believe that the DeletePage doesn't "delete" the panel instance that the
"page" holds.
Also check out:

http://markmail.org/message/cf5czfx4b2wxyxln
http://stackoverflow.com/questions/7060370/wxpython-disable-a-notebook-tab

I've whipped up a modified version of what you did. See attached.
Perhaps you can take what you are looking for from it.

redone02.py (6.89 KB)

I was just reading that RemovePage leaves the Window untouched, while DeletePage actually destroy’s the Window object.

···

On Friday, July 18, 2014 11:25:08 AM UTC-7, DevPlayer wrote:

I don’t think “Hiding” a wx.Notebook page is directly possible.

You can use nb.DeletePage(), AddPage() and InsertPage().

I believe that the DeletePage doesn’t “delete” the panel instance that the “page” holds.

Also check out:

http://markmail.org/message/cf5czfx4b2wxyxln

http://stackoverflow.com/questions/7060370/wxpython-disable-a-notebook-tab

I’ve whipped up a modified version of what you did. See attached.

Perhaps you can take what you are looking for from it.