XRC and wx.Dialog Problem

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

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

···

On Monday, April 23, 2012 2:21:41 AM UTC+2, Nebelhom wrote:

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

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.

···

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

--
Robin Dunn
Software Craftsman

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)

Hi,

···

On Monday, April 23, 2012 12:13:24 PM UTC-5, Patrick Ruoff wrote:

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)

The only documentation I’m aware of is on the wiki here: TwoStageCreation - wxPyWiki. 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

Hi folks,

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

Apologies for this rather simple, little mistake.

···

On 23 Apr., 19:29, Mike Driscoll <kyoso...@gmail.com> wrote:

Hi,

On Monday, April 23, 2012 12:13:24 PM UTC-5, Patrick Ruoff wrote:

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

The only documentation I'm aware of is on the wiki here:TwoStageCreation - wxPyWiki. 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