0

I basically have 3 separate wxPython GUIs that I would like to combine into a single application, giving each GUI its own notebook tab. I'm not really sure how to do this though and am looking for some advice. I appreciate any comments or thoughts I can get.

My idea is that it should look something like this:

import wx
import OtherFrame

class PageOne(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        t = wx.StaticText(self, -1, "This is a PageOne object", (20,20))

        panel=OtherFrame.Panel(parent)
        box = wx.BoxSizer(wx.VERTICAL)
        panel.SetSizer(self,box)
        panel.Layout(self, parent)        

class PageTwo(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        t = wx.StaticText(self, -1, "This is a PageTwo object", (40,40))

class PageThree(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        t = wx.StaticText(self, -1, "This is a PageThree object", (60,60))


class MainFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title="Simple Notebook Example")

        p = wx.Panel(self)
        nb = wx.Notebook(p)
        page1 = PageOne(nb)
        page2 = PageTwo(nb)
        page3 = PageThree(nb)
        nb.AddPage(page1, "Page 1")
        nb.AddPage(page2, "Page 2")
        nb.AddPage(page3, "Page 3")
        sizer = wx.BoxSizer()
        sizer.Add(nb, 1, wx.EXPAND)
        p.SetSizer(sizer)

if __name__ == "__main__":
    app = wx.App()
    MainFrame().Show()
    app.MainLoop()

But this gives a few errors and crashes. Without the sizers under PageOne, it just puts a gray square in the top right corner, with them, it crashes.

Thanks in advance!

Mike
  • 1,561
  • 3
  • 15
  • 18
  • Actually, you don't need the PageOne, two, three classes any more. You have a Panel class in OtherFrame. So you should be able to do page1 = OtherFrame.Panel(nb) – Mike Driscoll Jan 29 '13 at 19:50

2 Answers2

0

I assume each of your frame's have panels in them with the code you want. Just take the code for the widgets from each of those frames and put those in each of the Page classes. You'll probably have to change the parents of the widgets to "self" instead of "panel" or whatever you call them.

Then you won't be opening a frame in each of the page code base. You don't want to open a frame there. You want the page to be the parent of the widgets, not open something else up.

Mike Driscoll
  • 32,629
  • 8
  • 45
  • 88
  • Would there be a way to do this by importing the other GUIs as modules? The code for each of them is rather long, so I'd like to keep things as organized as possible. I'll go ahead and try pasting the panel code into the page classes though too. Thanks! – Mike Jan 29 '13 at 12:19
  • 1
    That depends. Are the widgets inside a panel class in the other GUIs? If so, then you could just do "import OtherFrame" and then "OtherFrame.MyPanelClass" and pass them the notebook as their parent. – Mike Driscoll Jan 29 '13 at 14:26
  • Yeah, I have everything in a panel class. I imported the file as OtherFrame, then added OtherFrame.Panel(parent) to the the PageOne class. However, this just adds a small gray square in the top left corner of the notebook. I think this happened to me before when I just had a panel without a frame. Do you know what I am doing wrong here? – Mike Jan 29 '13 at 15:59
  • I'm guessing a parenting issue. Make sure the imported panel is getting the notebook as its parent. You might have to put it in a sizer inside the notebook page with proportion set to 1 along with the wx.EXPAND flag. If it snaps into existence when you resize the frame, then all you need is a call to Layout() – Mike Driscoll Jan 29 '13 at 16:38
  • It doesn't seem to snap into existence if I resize the frame. I tried adding sizers (see edited code above), but I keep getting errors and it crashes. I'm afraid I'm not very experienced with sizers, does it look correct? – Mike Jan 29 '13 at 18:36
0

Thanks to your help, I got this to work for me. Since each of my other wx App had Panel classes with all the widgets I wanted, I didn't need to create classes for each page. Here's the code:

import wx
import OtherFrame1
import OtherFrame2
import OtherFrame3

class MainFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title="Simple Notebook Example")

        p = wx.Panel(self)
        nb = wx.Notebook(p)
        page1 = OtherFrame1.Panel(nb)
        page2 = OtherFrame2.Panel(nb)
        page3 = OtherFrame3.Panel(nb)
        nb.AddPage(page1, "Page 1")
        nb.AddPage(page2, "Page 2")
        nb.AddPage(page3, "Page 3")
        sizer = wx.BoxSizer()
        sizer.Add(nb, 1, wx.EXPAND)
        p.SetSizer(sizer)

if __name__ == "__main__":
    app = wx.App(redirect=False)
    MainFrame().Show()
    app.MainLoop()

Thanks for the help!

Mike
  • 1,561
  • 3
  • 15
  • 18