我正在尝试将python3中的函数内部的函数定义为
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()导致错误:
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'我真的是python的新手,所以努力去理解这个错误的范围和来源。此外,根据this post的说法,我可能通过在函数中声明一个函数来做一些非常有趣的事情。
我需要的是定义一个函数(get_specs),它将被另一个函数(get_nument)调用。
这实际上是一段gtk3代码,但我猜这是python的问题。在目前的状态下,完整的代码可以在here中看到。请帮帮忙。
发布于 2014-03-24 04:50:38
这一点:
File "hw3.py", line 42, in get_nument
self.spec.connect("activate",self.get_specs)在get_nument中,仅将get_specs引用为get_specs。所以,试一试:
self.spec.connect("activate", get_specs)它只是一个在get_nument作用域中声明的函数,所以它就像任何变量一样。举个例子:
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.https://stackoverflow.com/questions/22596388
复制相似问题