Parent & Child Windows (wxGlade)

I’m having trouble understanding the relationship between a child and parent window. I’m just trying to create a dialog box that is spawned from a “main” frame that has to be dismissed before the user can access the “main” frame again. Using the Dialog_Modal style (in wxGlade) doesn’t seem to do it…

I thought the code below would create a parent and a child relationship, but I’m obviously wrong. What am I missing??

Thanks everyone,
Adrian

import wx
class MyMainFrame(wx.Frame):
def init(self, *args, **kwds):

    # begin wxGlade: MyMainFrame.__init__
    kwds["style"] = wx.DEFAULT_FRAME_STYLE
    wx.Frame.__init__(self, *args, **kwds)
    self.btnShowDialog = wx.Button(self, -1, "Show Dialog Box")

    self.__set_properties()
    self.__do_layout()

    self.Bind(wx.EVT_BUTTON, self.showDialog, self.btnShowDialog)
    # end wxGlade

def __set_properties(self):
    # begin wxGlade: MyMainFrame.__set_properties

    self.SetTitle("frame_1")
    # end wxGlade

def __do_layout(self):
    # begin wxGlade: MyMainFrame.__do_layout
    sizer_1 = wx.BoxSizer(wx.VERTICAL)
    sizer_2 = wx.BoxSizer(wx.VERTICAL)

    sizer_2.Add(self.btnShowDialog, 0, wx.ALL, 3)
    sizer_1.Add(sizer_2, 1, wx.EXPAND, 0)
    self.SetSizer(sizer_1)
    sizer_1.Fit(self)
    self.Layout()
    # end wxGlade

def showDialog(self, event): # wxGlade: MyMainFrame.<event_handler>
    MySecondaryDialog(self).Show()

end of class MyMainFrame

class MySecondaryDialog(wx.Dialog):
def init(self, *args, **kwds):

    # begin wxGlade: MySecondaryDialog.__init__
    kwds["style"] = wx.DEFAULT_DIALOG_STYLE
    wx.Dialog.__init__(self, *args, **kwds)

    self.__set_properties()
    self.__do_layout()

    # end wxGlade

def __set_properties(self):
    # begin wxGlade: MySecondaryDialog.__set_properties
    self.SetTitle("dialog_1")
    # end wxGlade

def __do_layout(self):

    # begin wxGlade: MySecondaryDialog.__do_layout
    self.Layout()
    # end wxGlade

end of class MySecondaryDialog

if name == “main”:
app = wx.PySimpleApp(0)
wx.InitAllImageHandlers()

frame_1 = MyMainFrame(None, -1, "")
app.SetTopWindow(frame_1)
frame_1.Show()
app.MainLoop()

Hi Adrian,

I'm having trouble understanding the relationship between a child and parent window. I'm just trying to create a dialog box that is spawned from a "main" frame that has to be dismissed before the user can access the "main" frame again. Using the Dialog_Modal style (in wxGlade) doesn't seem to do it..

I thought the code below would create a parent and a child relationship, but I'm obviously wrong. What am I missing??

Thanks everyone,
Adrian

You almost had it. What you want is ShowModal(), not just Show() when calling the dialog.

···

-------------------
Mike Driscoll

Blog: http://blog.pythonlibrary.org
Python Extension Building Network: http://www.pythonlibrary.org

Thanks Mike! That did the trick! Does the same hold true for a frame? As in, if FrameOne spawns FrameTwo, can I use the same ShowModal() trick to bring up FrameTwo as a “child” of FrameOne? (Or do I just have a completely warped understanding of the “child-parent” thing?)

Adrian

···

On Wed, Sep 24, 2008 at 12:16 PM, Mike Driscoll mike@pythonlibrary.org wrote:

Hi Adrian,

I’m having trouble understanding the relationship between a child and parent window. I’m just trying to create a dialog box that is spawned from a “main” frame that has to be dismissed before the user can access the “main” frame again. Using the Dialog_Modal style (in wxGlade) doesn’t seem to do it…

I thought the code below would create a parent and a child relationship, but I’m obviously wrong. What am I missing??

Thanks everyone,

Adrian

You almost had it. What you want is ShowModal(), not just Show() when calling the dialog.


Mike Driscoll

Blog: http://blog.pythonlibrary.org

Python Extension Building Network: http://www.pythonlibrary.org


wxpython-users mailing list

wxpython-users@lists.wxwidgets.org

http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

Adrian,

Thanks Mike! That did the trick! Does the same hold true for a frame? As in, if FrameOne spawns FrameTwo, can I use the same ShowModal() trick to bring up FrameTwo as a "child" of FrameOne? (Or do I just have a completely warped understanding of the "child-parent" thing?)

Adrian

You can use ShowModal() on a normal frame, but I think the recommended practice is to use dialogs as they're supposed to be modal. As for whether or not it's a child, as I understand it, if the second frame's parent is the first frame, then you can think of the second frame as a child of the first. However, when I create "sub-frames", I usually pass my app object as the parent to my frames or I just pass None so they don't have a parent.

I'm not sure that the terminology is that important in this case, but maybe one of the others will weigh in on this. In the mean time, I think you'll find the Style guide helpful:

http://wiki.wxpython.org/wxPython%20Style%20Guide

And it looks like someone just added a sample application to the wiki too: wxPython by Example - wxPyWiki

Of course, you are also welcome to read my blog where I talk about various wxPython things, like sizers and my own sample app.

···

-------------------
Mike Driscoll

Blog: http://blog.pythonlibrary.org
Python Extension Building Network: http://www.pythonlibrary.org

Mike Driscoll wrote:

Adrian,

Thanks Mike! That did the trick! Does the same hold true for a frame? As in, if FrameOne spawns FrameTwo, can I use the same ShowModal() trick to bring up FrameTwo as a "child" of FrameOne? (Or do I just have a completely warped understanding of the "child-parent" thing?)

Adrian

You can use ShowModal() on a normal frame,

No, only the dialog class has the ShowModal method. Frames have MakeModal but it isn't really the same. It will disable interaction with other top level windows in the app but it does not create a nested event loop like ShowModal does (in other words, it doesn't block until the frame is dismissed.)

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Robin Dunn wrote:

<div class="moz-text-flowed" style="font-family: -moz-fixed">Mike Driscoll wrote:

Adrian,

Thanks Mike! That did the trick! Does the same hold true for a frame? As in, if FrameOne spawns FrameTwo, can I use the same ShowModal() trick to bring up FrameTwo as a "child" of FrameOne? (Or do I just have a completely warped understanding of the "child-parent" thing?)

Adrian

You can use ShowModal() on a normal frame,

No, only the dialog class has the ShowModal method. Frames have MakeModal but it isn't really the same. It will disable interaction with other top level windows in the app but it does not create a nested event loop like ShowModal does (in other words, it doesn't block until the frame is dismissed.)

Oops...I knew there was a way to do it, but that it wasn't the same. In fact, my intuition was telling me to shut up but I failed to listen. Oh well. Hopefully the OP won't be too scarred from this...

Mike

Robin, thanks for the info! I couldn’t find anything called MakeModal() when I was originally scouring the wxwodgets.org site! (Actually, is that the right place to look??)

Now that I’ve had an opportunity to try it out with a (very) simple app, I can certainly make my “secondary” frame behave modally, but when it gets dismissed, my “main” frame seems to be modal as well… What’s the proper usage here? Is there a “make un-modal” method?

Here’s my code that creates the problem:

import wx

class MyMainFrame(wx.Frame):

def init(self, *args, **kwds):

begin wxGlade: MyMainFrame.init

kwds[“style”] = wx.DEFAULT_FRAME_STYLE

wx.Frame.init(self, *args, **kwds)

self.btnShowDialog = wx.Button(self, -1, “Show Second Frame”)

self.__set_properties()

self.__do_layout()

self.Bind(wx.EVT_BUTTON, self.showDialog, self.btnShowDialog)

end wxGlade

def __set_properties(self):

begin wxGlade: MyMainFrame.__set_properties

self.SetTitle(“Main Frame”)

end wxGlade

def __do_layout(self):

begin wxGlade: MyMainFrame.__do_layout

sizer_1 = wx.BoxSizer(wx.VERTICAL)

sizer_2 = wx.BoxSizer(wx.VERTICAL)

sizer_2.Add(self.btnShowDialog, 0, wx.ALL, 3)

sizer_1.Add(sizer_2, 1, wx.EXPAND, 0)

self.SetSizer(sizer_1)

sizer_1.Fit(self)

self.Layout()

end wxGlade

def showDialog(self, event): # wxGlade: MyMainFrame.<event_handler>

fr2 = MyFrame(self)

fr2.MakeModal()

fr2.Show()

end of class MyMainFrame

class MyFrame(wx.Frame):

def init(self, *args, **kwds):

begin wxGlade: MyFrame.init

kwds[“style”] = wx.DEFAULT_FRAME_STYLE

wx.Frame.init(self, *args, **kwds)

self.text_ctrl_1 = wx.TextCtrl(self, -1, “Just a textctrl”)

self.button_1 = wx.Button(self, -1, “Close”)

self.__set_properties()

self.__do_layout()

self.Bind(wx.EVT_BUTTON, self.CloseFrame2, self.button_1)

end wxGlade

def __set_properties(self):

begin wxGlade: MyFrame.__set_properties

self.SetTitle(“Secondary Frame”)

end wxGlade

def __do_layout(self):

begin wxGlade: MyFrame.__do_layout

sizer_5 = wx.BoxSizer(wx.VERTICAL)

sizer_5.Add(self.text_ctrl_1, 0, wx.ALL, 3)

sizer_5.Add(self.button_1, 0, wx.ALL, 3)

self.SetSizer(sizer_5)

sizer_5.Fit(self)

self.Layout()

end wxGlade

def CloseFrame2(self, event): # wxGlade: MyFrame.<event_handler>

self.Close()

end of class MyFrame

if name == “main”:

app = wx.PySimpleApp(0)

wx.InitAllImageHandlers()

frame_1 = MyMainFrame(None, -1, “”)

app.SetTopWindow(frame_1)

frame_1.Show()

app.MainLoop()

···

On Fri, Sep 26, 2008 at 9:23 AM, Mike Driscoll mike@pythonlibrary.org wrote:

Robin Dunn wrote:

Mike Driscoll wrote: > Adrian, > > > > Thanks Mike! That did the trick! Does the same hold true for a frame? As in, if FrameOne spawns FrameTwo, can I use the same ShowModal() trick to bring up FrameTwo as a "child" of FrameOne? (Or do I just have a completely warped understanding of the "child-parent" thing?) > > > > > > > > > > Adrian > > > > > > > > > You can use ShowModal() on a normal frame,

No, only the dialog class has the ShowModal method. Frames have MakeModal but it isn’t really the same. It will disable interaction with other top level windows in the app but it does not create a nested event loop like ShowModal does (in other words, it doesn’t block until the frame is dismissed.)

Oops…I knew there was a way to do it, but that it wasn’t the same. In fact, my intuition was telling me to shut up but I failed to listen. Oh well. Hopefully the OP won’t be too scarred from this…

Mike


wxpython-users mailing list

wxpython-users@lists.wxwidgets.org

http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

Adrian Greyling wrote:

Robin, thanks for the info! I couldn't find anything called MakeModal() when I was originally scouring the wxwodgets.org <http://wxwodgets.org> site! (Actually, is that the right place to look??)

I personally recommend wxPython API Documentation — wxPython Phoenix 4.2.2 documentation for docs. The wxwodgets site doesn't really exist, but I'm sure you were just testing us, right? :wink:

···

-------------------
Mike Driscoll

Blog: http://blog.pythonlibrary.org
Python Extension Building Network: http://www.pythonlibrary.org

Adrian Greyling wrote:

Robin, thanks for the info! I couldn't find anything called MakeModal() when I was originally scouring the wxwodgets.org <http://wxwodgets.org> site! (Actually, is that the right place to look??)

Now that I've had an opportunity to try it out with a (very) simple app, I can certainly make my "secondary" frame behave modally, but when it gets dismissed, my "main" frame seems to be modal as well... What's the proper usage here? Is there a "make un-modal" method?

Yep, you should call MakeModal(False) when the frame is closed, or in the frame's EVT_CLOSE handler.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Testing you Mike? No… Just proving that I can’t spell… (See, I told you, I’m not very smart, but I can lift heavy things…)

Adrian

···

On Wed, Oct 1, 2008 at 1:40 PM, Mike Driscoll mike@pythonlibrary.org wrote:

Adrian Greyling wrote:

Robin, thanks for the info! I couldn’t find anything called MakeModal() when I was originally scouring the wxwodgets.org <http://wxwodgets.org> site! (Actually, is that the right place to look??)

I personally recommend http://wxpython.org/docs/api/ for docs. The wxwodgets site doesn’t really exist, but I’m sure you were just testing us, right? :wink:


Mike Driscoll

Blog: http://blog.pythonlibrary.org

Python Extension Building Network: http://www.pythonlibrary.org


wxpython-users mailing list

wxpython-users@lists.wxwidgets.org

http://lists.wxwidgets.org/mailman/listinfo/wxpython-users