problem calling method from event handler

I’m trying to call a method from an event handler, but that doesn’t seem to work. What am I doing wrong? Please note that I’m a (wx)Python beginner…

class MyFrame(wx.Frame):

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

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

    self.Bind(wx.EVT_BUTTON, self.test, self.testbutton)

    ...

def sendmail(self):

    ...

def test(self, event):

    self.sendmail()

    event.Skip()

Clicking the test button returns:

Traceback (most recent call last):

File “D:\python\bx\bxgui.py”, line 120, in test

self.sendmail()

AttributeError: ‘MyFrame’ object has no attribute ‘sendmail’

Thanks!

Hi Pieter,

I'm trying to call a method from an event handler, but that doesn't seem to work. What am I doing wrong? Please note that I'm a (wx)Python beginner...

class MyFrame(wx.Frame):
    def __init__(self, *args, **kwds):
        wx.Frame.__init__(self, *args, **kwds)
        self.Bind(wx.EVT_BUTTON, self.test, self.testbutton)
        ...
    def sendmail(self):
        ...
    def test(self, event):
        self.sendmail()
        event.Skip()

Clicking the test button returns:

Traceback (most recent call last):
  File "D:\python\bx\bxgui.py", line 120, in test
    self.sendmail()
AttributeError: 'MyFrame' object has no attribute 'sendmail'

Thanks!

Hmmm...I think you're leaving something out here. This should "just work". I whipped together a quick sample and it works:

<code>

import wx
class MyForm(wx.Frame):
     def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, "Button Tutorial")
         # Add a panel so it looks the correct on all platforms
        panel = wx.Panel(self, wx.ID_ANY)
        btn = wx.Button(panel, wx.ID_ANY, 'Push me!')
        self.Bind(wx.EVT_BUTTON, self.onButton, btn)

    def sendmail(self):
        print "in sendmail"
           def onButton(self, event):
        print "in onButton"
        self.sendmail()
         # Run the program
if __name__ == "__main__":
    app = wx.PySimpleApp()
    frame = MyForm().Show()
    app.MainLoop()

</code>

What platform are you on? Python and wxPython versions may be helpful as well. I am using wxPython 2.8.9.1 (unicode), Python 2.5.2 on Windows XP.

···

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

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

Could it be possible that this is something like you added
the definition of sendmail and then didn't save your program (with
this new addition) when you tried to run it? Because it seems right.

···

On Wed, Jan 14, 2009 at 11:13 AM, Pieter Provoost <pieterprovoost@gmail.com> wrote:

I'm trying to call a method from an event handler, but that doesn't seem to
work. What am I doing wrong? Please note that I'm a (wx)Python beginner...

class MyFrame(wx.Frame):
    def __init__(self, *args, **kwds):
        wx.Frame.__init__(self, *args, **kwds)
        self.Bind(wx.EVT_BUTTON, self.test, self.testbutton)
        ...
    def sendmail(self):
        ...
    def test(self, event):
        self.sendmail()
        event.Skip()

Clicking the test button returns:

Traceback (most recent call last):
  File "D:\python\bx\bxgui.py", line 120, in test
    self.sendmail()
AttributeError: 'MyFrame' object has no attribute 'sendmail'

Hi Peter,

Further to the other responses you are receiving from Mike, C M, may I
give a modest recommendation ?

In my humble opinion - I'm a green horn too - I prefer not to use the
facility (*args, **kwds) which is great but maybe too convenient for a
starter. I believe it is more educational to explicitely write the
positional parameters and the named parameters. It obliges us (the
newbies) to look at the documentation and to learn more in depth. So,
when we write something like:

def __init__(self, row, column):

we know that self refers to the instance and that row and colum are 2
parameters. This comes handy when receiving error messages of the
kind:
"function f expects 2 arguments, 3 received" or that sort of thing.

Furthermore, there are already so many functions or methods (within
classes) that have default value for their parameters in the form:

param = wx.DEFAULT_PARAM_VALUE

So, when calling functions or methods, my recommendation is to be
verbose in the parameter passing.

Hope nobody minds me giving a personal opinion.
Enjoy Python / wxPython as much as I do. I guess I might book 100 hours soon.
Welcome to the group.
Rene

···

On Wed, Jan 14, 2009 at 5:13 PM, Pieter Provoost <pieterprovoost@gmail.com> wrote:

I'm trying to call a method from an event handler, but that doesn't seem to
work. What am I doing wrong? Please note that I'm a (wx)Python beginner...

class MyFrame(wx.Frame):
    def __init__(self, *args, **kwds):
        wx.Frame.__init__(self, *args, **kwds)
        self.Bind(wx.EVT_BUTTON, self.test, self.testbutton)
        ...
    def sendmail(self):
        ...
    def test(self, event):
        self.sendmail()
        event.Skip()

Clicking the test button returns:

Traceback (most recent call last):
  File "D:\python\bx\bxgui.py", line 120, in test
    self.sendmail()
AttributeError: 'MyFrame' object has no attribute 'sendmail'

Thanks!

_______________________________________________
wxpython-users mailing list
wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

This appears to be an indentation issue. While preparing the minimal example (which works) below, I noticed that the indentation is wrong in notepad. In eric4 and notepad++ it appears to be fine though. I repaired the code in notepad and now it works…

import wx

class MyFrame(wx.Frame):
def init(self, *args, **kwds):
wx.Frame.init(self, *args, **kwds)
self.testbutton = wx.Button(self, -1, “test”)
self.__do_layout()
self.Bind(wx.EVT_BUTTON, self.test, self.testbutton)

def __do_layout(self):
    grid_sizer_1 = wx.FlexGridSizer(0, 0, 0, 0,)
    grid_sizer_1.Add(self.testbutton)
    self.SetSizer(grid_sizer_1)
    self.Layout()

def sendmail(self):
     return

def test(self, event):
    self.sendmail()
    event.Skip()

if name == “main”:
app = wx.PySimpleApp(0)
wx.InitAllImageHandlers()
frame_1 = MyFrame(None, -1, “”)
app.SetTopWindow(frame_1)
frame_1.Show()
app.MainLoop()

···

2009/1/14 Mike Driscoll mike@pythonlibrary.org

Hi Pieter,

I’m trying to call a method from an event handler, but that doesn’t seem to work. What am I doing wrong? Please note that I’m a (wx)Python beginner…

class MyFrame(wx.Frame):

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

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

    self.Bind(wx.EVT_BUTTON, self.test, self.testbutton)

    ...

def sendmail(self):

    ...

def test(self, event):

    self.sendmail()

    event.Skip()

Clicking the test button returns:

Traceback (most recent call last):

File “D:\python\bx\bxgui.py”, line 120, in test

self.sendmail()

AttributeError: ‘MyFrame’ object has no attribute ‘sendmail’

Thanks!

Hmmm…I think you’re leaving something out here. This should “just work”. I whipped together a quick sample and it works:

import wx

class MyForm(wx.Frame):

def init(self):

   wx.Frame.__init__(self, None, wx.ID_ANY, "Button Tutorial")



   # Add a panel so it looks the correct on all platforms

   panel = wx.Panel(self, wx.ID_ANY)

   btn = wx.Button(panel, wx.ID_ANY, 'Push me!')

   self.Bind(wx.EVT_BUTTON, self.onButton, btn)

def sendmail(self):

   print "in sendmail"

     def onButton(self, event):

   print "in onButton"

   self.sendmail()

    # Run the program

if name == “main”:

app = wx.PySimpleApp()

frame = MyForm().Show()

app.MainLoop()

What platform are you on? Python and wxPython versions may be helpful as well. I am using wxPython 2.8.9.1 (unicode), Python 2.5.2 on Windows XP.


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


Pieter Provoost

Department of Ecosystem Studies
Centre for Estuarine and Marine Ecology (CEME)
Netherlands Institute of Ecology (NIOO-KNAW)
Korringaweg 7, 4401 NT Yerseke

The Netherlands

+32-478-574420 (Belgium)
+31-113-577472 (The Netherlands)
skype pieterprovoost

Rene Heymans wrote:

In my humble opinion - I'm a green horn too - I prefer not to use the
facility (*args, **kwds) which is great but maybe too convenient for a
starter. I believe it is more educational to explicitely write the
positional parameters and the named parameters.

In general, I agree.

So, when calling functions or methods, my recommendation is to be
verbose in the parameter passing.

I also agree.

However:

class MyFrame(wx.Frame):
    def __init__(self, *args, **kwds):

I think this is the way to go -- it says: make this class take exactly the same arguments as a wx.Frame -- that is what you want here, and it makes more sense than repeating all the wx.Frame arguments twice in this code.

Do look at:

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

for other style suggestions.

-Chris

···

--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

Thank you Chris for your comments.
Very well pointed.
I appreciate your care.
All the best,
Rene

···

On Wed, Jan 14, 2009 at 10:47 PM, Christopher Barker <Chris.Barker@noaa.gov> wrote:

Rene Heymans wrote:

In my humble opinion - I'm a green horn too - I prefer not to use the
facility (*args, **kwds) which is great but maybe too convenient for a
starter. I believe it is more educational to explicitely write the
positional parameters and the named parameters.

In general, I agree.

So, when calling functions or methods, my recommendation is to be
verbose in the parameter passing.

I also agree.

However:

class MyFrame(wx.Frame):
   def __init__(self, *args, **kwds):

I think this is the way to go -- it says: make this class take exactly the
same arguments as a wx.Frame -- that is what you want here, and it makes
more sense than repeating all the wx.Frame arguments twice in this code.

Do look at:

wxPython Style Guide - wxPyWiki

for other style suggestions.

-Chris

--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov
_______________________________________________
wxpython-users mailing list
wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users