0

I am trying to define a function inside a function in python3 as

from gi.repository import Gtk, GObject

class EntryWindow(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title="Generate Init")
        self.set_size_request(100, 50)
        self.nument = Gtk.Entry()

        <did some work>

    def get_nument(self,nument):
      numele= self.nument.get_text()
      print(numele)
      <did some more work and define a function again>
        def get_specs(self,spec):
            numspec=self.spec.get_text()

resulting to an error:

Traceback (most recent call last):
  File "hw3.py", line 42, in get_nument
    self.spec.connect("activate",self.get_specs)
AttributeError: 'EntryWindow' object has no attribute 'get_specs'

I am really new to python, so trying hard for understanding the scope of self and origin of this error. Also, possibly I am doing things really funny by declaring a function inside a function, according to this post.

What I need is to define a function(get_specs) which will be called by another function (get_nument).

This is actually a gtk3 code, but I guess its problem with python. The complete code, at its present state can be seen here. Kindly help.

Community
  • 1
  • 1
BaRud
  • 3,055
  • 7
  • 41
  • 89

1 Answers1

0

This:

File "hw3.py", line 42, in get_nument
  self.spec.connect("activate",self.get_specs)

In get_nument, reference get_specs as just get_specs. So, try:

self.spec.connect("activate", get_specs)

It's just a function that you've declared in the scope of get_nument, so it's just like any variable. An example:

def foo(self):

    x = 34
    def bar():
        # ...

    # Use x:
    print(x)
    # Use bar:
    print(bar())  # Note: no self, because bar isn't part of self.
Thanatos
  • 42,585
  • 14
  • 91
  • 146