Call function outside the class

Hi everyone!.

I would call the function On_Click, from within Frame MainWindow .

Code:-------------------------------------------------------

import wx

class MainWindow(wx.Frame):

    def __init__(self):

        wx.Frame.__init__(self, None, -1, 'Button Example', size=(300,
100))

        self.panel = wx.Panel(self, -1)

        self.button = wx.Button(self.panel, -1, "Hello", pos=(50, 20))

        self.button.SetDefault()

        self.button.Bind(wx.EVT_BUTTON, OnClick)

def OnClick(self, event):

    self.button.SetLabel("Clicked")

if __name__ == '__main__':

    app = wx.App()
    window = MainWindow()
    window.Show()
    app.MainLoop()

···

------------------------------------------

But it throws the error:

TypeError: OnClick() takes exactly 2 arguments (1 given)

The first argument is self, but the second event that is not fun.

I appreciate any ideas.

Best Regards.

Cris

Hi,

It is because the OnClick function is outside the class it has no
self.
To make this work it should be like this:

Code:-------------------------------------------------------
import wx
class MainWindow(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, 'Button Example',
size=(300,
100))
        self.panel = wx.Panel(self, -1)
        self.button = wx.Button(self.panel, -1, "Hello", pos=(50,
20))
        self.button.SetDefault()
        self.button.Bind(wx.EVT_BUTTON, self.OnClick)

   def OnClick(self, event):
        self.button.SetLabel("Clicked")

if __name__ == '__main__':
    app = wx.App()
    window = MainWindow()
    window.Show()
    app.MainLoop()

···

On Apr 3, 4:21 pm, cris <abarz...@gmail.com> wrote:

Hi everyone!.

I would call the function On_Click, from within Frame MainWindow .

Code:-------------------------------------------------------

import wx

class MainWindow(wx.Frame):

def \_\_init\_\_\(self\):

    wx\.Frame\.\_\_init\_\_\(self, None, \-1, &#39;Button Example&#39;, size=\(300,

100))

    self\.panel = wx\.Panel\(self, \-1\)

    self\.button = wx\.Button\(self\.panel, \-1, &quot;Hello&quot;, pos=\(50, 20\)\)

    self\.button\.SetDefault\(\)

    self\.button\.Bind\(wx\.EVT\_BUTTON, OnClick\)

def OnClick(self, event):

self\.button\.SetLabel\(&quot;Clicked&quot;\)

if __name__ == '__main__':

app = wx\.App\(\)
window = MainWindow\(\)
window\.Show\(\)
app\.MainLoop\(\)

------------------------------------------

But it throws the error:

TypeError: OnClick() takes exactly 2 arguments (1 given)

The first argument is self, but the second event that is not fun.

I appreciate any ideas.

Best Regards.

Cris

------------------------------------------

If you want OnClick to be out of the class, then let go of self.

def OnClick(evt):
   rest of code

Hope this helps.

VC

One more thing:

if you decide you want the function out of the class it should be:

def OnClick(evt):
    app.MainWindow.button.SetLabel('Clicked')

Again because the function is out of the class it has no attribute
self.
self points to the class it belongs to. So since you are not in the
class you have to manually point to the function which in this case
is:
app.MainWindow

VC

Hi VC

Thanks for the Tips!

Best Regards

Cris

···

-----Mensaje original-----
De: VC <napoleonics@gmail.com>
Reply-to: wxpython-users@googlegroups.com
Para: wxPython-users <wxpython-users@googlegroups.com>
Asunto: [wxPython-users] Re: Call function outside the class
Fecha: Tue, 3 Apr 2012 19:38:17 -0700 (PDT)

One more thing:

if you decide you want the function out of the class it should be:

def OnClick(evt):
   app.MainWindow.button.SetLabel('Clicked')

Again because the function is out of the class it has no attribute
self.
self points to the class it belongs to. So since you are not in the
class you have to manually point to the function which in this case
is:
app.MainWindow

VC

Hi cris,

I actually made a mistake, it is window.button.SetLabel('clicked')

I did not pay attention to how you set up your app.
here is the code, i tested it, it works.

import wx
class MainWindow(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, 'Button Example', size=(300,
100))
        self.panel = wx.Panel(self, -1)
        self.button = wx.Button(self.panel, -1, "Hello", pos=(50,
20))
        self.button.SetDefault()
        self.button.Bind(wx.EVT_BUTTON, OnClick)

def OnClick(event):
    window.button.SetLabel("Clicked")

if __name__ == '__main__':
    app = wx.App()
    window = MainWindow()
    window.Show()
    app.MainLoop()

···

On Apr 3, 9:47 pm, craf <abarz...@gmail.com> wrote:

>-----Mensaje original-----
>De: VC <napoleon...@gmail.com>
>Reply-to: wxpython-users@googlegroups.com
>Para: wxPython-users <wxpython-users@googlegroups.com>
>Asunto: [wxPython-users] Re: Call function outside the class
>Fecha: Tue, 3 Apr 2012 19:38:17 -0700 (PDT)
>One more thing:
>if you decide you want the function out of the class it should be:
>def OnClick(evt):
> app.MainWindow.button.SetLabel('Clicked')
>Again because the function is out of the class it has no attribute
>self.
>self points to the class it belongs to. So since you are not in the
>class you have to manually point to the function which in this case
>is:
>app.MainWindow
>VC

Hi VC

Thanks for the Tips!

Best Regards

Cris

No problems, ;=)

Best Regards.

Cris

···

-----Mensaje original-----
De: VC <napoleonics@gmail.com>
Reply-to: wxpython-users@googlegroups.com
Para: wxPython-users <wxpython-users@googlegroups.com>
Asunto: [wxPython-users] Re: Call function outside the class
Fecha: Tue, 3 Apr 2012 21:17:24 -0700 (PDT)

On Apr 3, 9:47 pm, craf <abarz...@gmail.com> wrote:

>-----Mensaje original-----
>De: VC <napoleon...@gmail.com>
>Reply-to: wxpython-users@googlegroups.com
>Para: wxPython-users <wxpython-users@googlegroups.com>
>Asunto: [wxPython-users] Re: Call function outside the class
>Fecha: Tue, 3 Apr 2012 19:38:17 -0700 (PDT)
>One more thing:
>if you decide you want the function out of the class it should be:
>def OnClick(evt):
> app.MainWindow.button.SetLabel('Clicked')
>Again because the function is out of the class it has no attribute
>self.
>self points to the class it belongs to. So since you are not in the
>class you have to manually point to the function which in this case
>is:
>app.MainWindow
>VC

Hi VC

Thanks for the Tips!

Best Regards

Cris

Hi cris,

I actually made a mistake, it is window.button.SetLabel('clicked')

I did not pay attention to how you set up your app.
here is the code, i tested it, it works.

import wx
class MainWindow(wx.Frame):
   def __init__(self):
       wx.Frame.__init__(self, None, -1, 'Button Example', size=(300,
100))
       self.panel = wx.Panel(self, -1)
       self.button = wx.Button(self.panel, -1, "Hello", pos=(50,
20))
       self.button.SetDefault()
       self.button.Bind(wx.EVT_BUTTON, OnClick)

def OnClick(event):
   window.button.SetLabel("Clicked")

if __name__ == '__main__':
   app = wx.App()
   window = MainWindow()
   window.Show()
   app.MainLoop()