problem binding a function to a button, in XRC dialog...

hello,

I get the following:

  File "ui.py", line 15, in __init__
    EVT_BUTTON(self, XRCID("button_1"), self.Hello)
AttributeError: 'Dialog' object has no attribute 'Hello'

when running my application...

which does this:

class Dialog(wxDialogPtr):
    def __init__(self, parent, resource):
        w = resource.LoadDialog(parent, 'Dialog')
        wxDialogPtr.__init__(self, w.this)
        self.thisown = 1
        self.Center()

        EVT_BUTTON(self, XRCID("button_1"), self.Hello)

    def Hello(self, evt):
        print "hello!"

...

--> I can't figure out why the class Dialog has no method Hello...printing the self.__dict__ indeed shows that Hello is not bound...

any idea?

thanks a lot,

Stan.

Stan Pinte wrote:

hello,

I get the following:

File "ui.py", line 15, in __init__
   EVT_BUTTON(self, XRCID("button_1"), self.Hello)
AttributeError: 'Dialog' object has no attribute 'Hello'

when running my application...

which does this:

class Dialog(wxDialogPtr):
   def __init__(self, parent, resource):
       w = resource.LoadDialog(parent, 'Dialog')
       wxDialogPtr.__init__(self, w.this)
       self.thisown = 1
       self.Center()

       EVT_BUTTON(self, XRCID("button_1"), self.Hello)

   def Hello(self, evt):
       print "hello!"

...

--> I can't figure out why the class Dialog has no method Hello...printing the self.__dict__ indeed shows that Hello is not bound...

any idea?

It looks correct above but check your indentation and watch out for tabs.

···

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

Robin Dunn wrote:

Stan Pinte wrote:

hello,

I get the following:

File "ui.py", line 15, in __init__
   EVT_BUTTON(self, XRCID("button_1"), self.Hello)
AttributeError: 'Dialog' object has no attribute 'Hello'

when running my application...

which does this:

class Dialog(wxDialogPtr):
   def __init__(self, parent, resource):
       w = resource.LoadDialog(parent, 'Dialog')
       wxDialogPtr.__init__(self, w.this)
       self.thisown = 1
       self.Center()

       EVT_BUTTON(self, XRCID("button_1"), self.Hello)

   def Hello(self, evt):
       print "hello!"

...

--> I can't figure out why the class Dialog has no method Hello...printing the self.__dict__ indeed shows that Hello is not bound...

any idea?

It looks correct above but check your indentation and watch out for tabs.

strange...because the class __dict__ shows the method, not the instance...

E:\test-xrc>python ui.py
{'__module__': '__main__', 'Hello': <function Hello at 0x0098DB30>, '__init__':
<function __init__ at 0x009A61B0>, '__doc__': None}
Traceback (most recent call last):
  File "ui.py", line 44, in ?
    testPreferencesDialog()
  File "ui.py", line 39, in testPreferencesDialog
    app = MyApp(0)
  File "ui.py", line 23, in __init__
    wxApp.__init__(self, num)
  File "C:\Python23\Lib\site-packages\wx\_core.py", line 5243, in __init__
    self._BootstrapApp()
  File "C:\Python23\Lib\site-packages\wx\_core.py", line 4931, in _BootstrapApp
    return _core_.PyApp__BootstrapApp(*args, **kwargs)
  File "ui.py", line 33, in OnInit
    dialog = Dialog(None, resource)
  File "ui.py", line 15, in __init__
    EVT_BUTTON(self, XRCID("button_1"), self.Hello)
AttributeError: 'Dialog' object has no attribute 'Hello'

I am new to python, so this maybe a problem with me :wink:

thanks!!

Stan.

Robin Dunn wrote:

Stan Pinte wrote:

hello,

I get the following:

File "ui.py", line 15, in __init__
   EVT_BUTTON(self, XRCID("button_1"), self.Hello)
AttributeError: 'Dialog' object has no attribute 'Hello'

when running my application...

which does this:

class Dialog(wxDialogPtr):
   def __init__(self, parent, resource):
       w = resource.LoadDialog(parent, 'Dialog')
       wxDialogPtr.__init__(self, w.this)
       self.thisown = 1
       self.Center()

       EVT_BUTTON(self, XRCID("button_1"), self.Hello)

   def Hello(self, evt):
       print "hello!"

...

--> I can't figure out why the class Dialog has no method Hello...printing the self.__dict__ indeed shows that Hello is not bound...

any idea?

It looks correct above but check your indentation and watch out for tabs.

I found out what was the problem...

--> self.Hello is unbound, because at that moment, self references the pointer, not the Dialog instance...

If I define Hello outside of the class, and do

EVT_BUTTON(self, XRCID("button_1"), Hello)

it works perfectly...

I now have to figured out why it does so...

thanks a lot,

Stan.

Stan Pinte wrote:

Robin Dunn wrote:

Stan Pinte wrote:

hello,

I get the following:

File "ui.py", line 15, in __init__
   EVT_BUTTON(self, XRCID("button_1"), self.Hello)
AttributeError: 'Dialog' object has no attribute 'Hello'

when running my application...

which does this:

class Dialog(wxDialogPtr):

I didn't notice this before, but you should not be using wxDialogPtr. That class is an "implementation detail" and is not meant to be used directly.

   def __init__(self, parent, resource):
       w = resource.LoadDialog(parent, 'Dialog')
       wxDialogPtr.__init__(self, w.this)
       self.thisown = 1
       self.Center()

       EVT_BUTTON(self, XRCID("button_1"), self.Hello)

   def Hello(self, evt):
       print "hello!"

Instead you should be doing something like this (untested, just off the top of my head):

class Dialog(wx.Dialog):
  def __init__(self, parent, resource):
    pre = wx.PreDialog() # 1
    resource.LoadOnDialog(pre, parent, 'Dialog') # 2
    self.PostCreate(pre) # 3

    self.Center()
    EVT_BUTTON(self, XRCID("button_1"), self.Hello)

  def Hello(self, evt):
    print "hello"

On line #1 the dialog instance is created, but the UI Object is not. This is called 2-phase create in the wx docs, and is provided for special situations where some things may need to be done in the object before the UI object is created, like setting extended style flags. Normally you would call Create yourself to do the actual creation of the UI object.

Line #2 calls Create for you and loads the controls defined in the resource onto the dialog.

Line #3 transfers the "guts" of the pre instance into self, so self becomes the dialog.

···

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

Robin Dunn wrote:

Stan Pinte wrote:

Robin Dunn wrote:

Stan Pinte wrote:

hello,

I get the following:

File "ui.py", line 15, in __init__
   EVT_BUTTON(self, XRCID("button_1"), self.Hello)
AttributeError: 'Dialog' object has no attribute 'Hello'

when running my application...

which does this:

class Dialog(wxDialogPtr):

I didn't notice this before, but you should not be using wxDialogPtr. That class is an "implementation detail" and is not meant to be used directly.

thanks a lot!!! Indeed, couldn't find doc for xxxPtr types...

Shouldn't we work on the page

http://wiki.wxpython.org/index.cgi/UsingXmlResources

which gives a big example, using wxPointers?

   def __init__(self, parent, resource):
       w = resource.LoadDialog(parent, 'Dialog')
       wxDialogPtr.__init__(self, w.this)
       self.thisown = 1
       self.Center()

       EVT_BUTTON(self, XRCID("button_1"), self.Hello)

   def Hello(self, evt):
       print "hello!"

Instead you should be doing something like this (untested, just off the top of my head):

class Dialog(wx.Dialog):
    def __init__(self, parent, resource):
        pre = wx.PreDialog() # 1
        resource.LoadOnDialog(pre, parent, 'Dialog') # 2
        self.PostCreate(pre) # 3

        self.Center()
        EVT_BUTTON(self, XRCID("button_1"), self.Hello)

    def Hello(self, evt):
        print "hello"

On line #1 the dialog instance is created, but the UI Object is not. This is called 2-phase create in the wx docs, and is provided for special situations where some things may need to be done in the object before the UI object is created, like setting extended style flags. Normally you would call Create yourself to do the actual creation of the UI object.

Line #2 calls Create for you and loads the controls defined in the resource onto the dialog.

Line #3 transfers the "guts" of the pre instance into self, so self becomes the dialog.

pretty clear, thanks!!!

Stan.

Stan Pinte wrote:

Robin Dunn wrote:

Stan Pinte wrote:

Robin Dunn wrote:

Stan Pinte wrote:

hello,

I get the following:

File "ui.py", line 15, in __init__
   EVT_BUTTON(self, XRCID("button_1"), self.Hello)
AttributeError: 'Dialog' object has no attribute 'Hello'

when running my application...

which does this:

class Dialog(wxDialogPtr):

I didn't notice this before, but you should not be using wxDialogPtr. That class is an "implementation detail" and is not meant to be used directly.

thanks a lot!!! Indeed, couldn't find doc for xxxPtr types...

Shouldn't we work on the page

http://wiki.wxpython.org/index.cgi/UsingXmlResources

which gives a big example, using wxPointers?

Yep. Feel free to update it with what you've learned.

···

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