XRC and wx.Dialog Problem

125 views
Skip to first unread message

Nebelhom

unread,
Apr 22, 2012, 8:21:41 PM4/22/12
to wxPython-users
Hi folks,

I am trying to call a wx.Dialog from XRC, but I am running into
problems, that I don't understand. I hope one of you can shed some
light on this. The XRC file is fairly large, so I shall see if there
is something fundamentally wrong with my python code first.

When I run this code below, I obtain the dialog I want
("config_dialog.xrc" refers to my particular wx.dialog) and another
empty dialog box to which also the ShowModal() is tied:

import wx
from wx import xrc

class ConfigDialog(wx.Dialog):
def __init__(self, parent, id, title,
style=wx.DEFAULT_DIALOG_STYLE):
wx.Dialog.__init__(self, parent, id, title)

self._parent = parent
self.res = xrc.XmlResource("config_dialog.xrc")
self.dlg = self.res.LoadDialog(None, "ConfigDlg")

self.dlg.Show()

class MyApp(wx.App):
def OnInit(self):
dia = ConfigDialog(None, -1, "Config Dialog")
dia.ShowModal()
dia.Destroy()
return True

app = MyApp(0)
app.MainLoop()

When I change the line

self.dlg = self.res.LoadDialog(None, "ConfigDlg")

to

self.dlg = self.res.LoadDialog(self, "ConfigDlg")

I obtain the desired Dialog on its own, but my application freezes up
completely. Is there something wrong with my code? I wasn't able to
isolate any exact cause from my XRC file, but to be sure I do not
leave anything out, the most fancy control I try to use in there is a
wxNotebook.

Any suggestions are greatly appreciated. Thanks

Johannes

Patrick Ruoff

unread,
Apr 23, 2012, 9:43:33 AM4/23/12
to wxpytho...@googlegroups.com
Hi,

the problem is that the dialog you loaded from your xrc resource is dia.dlg, not dia.
dia is just an empty dialog that is created because you derive your dialog class
from wx.Dialog.
What you probably want is just deriving ConfigDialog from object
and calling dia.dlg.ShowModal() in your app.
(or define a method in ConfigDialog that does it)
Calling Show() is not needed in this case,
thats for modeless dialogs

Patrick

Robin Dunn

unread,
Apr 23, 2012, 12:36:48 PM4/23/12
to wxpytho...@googlegroups.com
On 4/23/12 6:43 AM, Patrick Ruoff wrote:
> Hi,
>
> the problem is that the dialog you loaded from your xrc resource is
> dia.dlg, not dia.
> dia is just an empty dialog that is created because you derive your
> dialog class
> from wx.Dialog.
> What you probably want is just deriving ConfigDialog from object
> and calling dia.dlg.ShowModal() in your app.
> (or define a method in ConfigDialog that does it)
> Calling Show() is not needed in this case,
> thats for modeless dialogs


It is possible to "load on self" with XRC, which seems to be what is
wanted here. It is done using the 2-phase create scheme that wxWidgets
provides, except in this case XRC calls the Create() method. It would
be done something like this:

class ConfigDialog(wx.Dialog):
def __init__(self, parent):


self.res = xrc.XmlResource("config_dialog.xrc")

pre = wx.PreDialog()
self.res.LoadOnDialog(pre, parent, "ConfigDlg")
self.PostCreate(pre)

Notice the use of wx.PreDialog() (this is wxPython's name for the
"default" C++ constructor) and LoadOnDialog. The PostCreate method is
used to transfer the guts of pre into self, so it acts like it was a
real instance of ConfigDialog.

--
Robin Dunn
Software Craftsman
http://wxPython.org

Patrick Ruoff

unread,
Apr 23, 2012, 1:13:24 PM4/23/12
to wxpytho...@googlegroups.com

It is possible to "load on self" with XRC, which seems to be what is
wanted here.  It is done using the 2-phase create scheme that wxWidgets
provides, except in this case XRC calls the Create() method.  It would
be done something like this:

class ConfigDialog(wx.Dialog):
        def __init__(self, parent):
                self.res = xrc.XmlResource("config_dialog.xrc")
                pre = wx.PreDialog()
                       self.res.LoadOnDialog(pre, parent, "ConfigDlg")
                self.PostCreate(pre)

Notice the use of wx.PreDialog() (this is wxPython's name for the
"default" C++ constructor) and LoadOnDialog.  The PostCreate method is
used to transfer the guts of pre into self, so it acts like it was a
real instance of ConfigDialog.

Nice feature!
Is there any documentation on it? (I couldn't find much using google / the wiki)

Mike Driscoll

unread,
Apr 23, 2012, 1:29:01 PM4/23/12
to wxpytho...@googlegroups.com
Hi,

The only documentation I'm aware of is on the wiki here: http://wiki.wxpython.org/TwoStageCreation. Well, I think it may be mentioned in the wxPython books too, now that I think about it. I can't remember for sure though and I don't have those books handy to check.

- Mike

Nebelhom

unread,
Apr 24, 2012, 11:37:17 AM4/24/12
to wxPython-users
Hi folks,

thanks a lot for the solutions! It works perfectly now and I learnt
something new again about wxpython :)

Apologies for this rather simple, little mistake.
Reply all
Reply to author
Forward
0 new messages