gpt4 book ai didi

python - 回调函数中的 AttributeError

转载 作者:太空狗 更新时间:2023-10-30 01:02:54 26 4
gpt4 key购买 nike

我真的很抱歉用 AttributeError 来打扰你,但我就是无法弄清楚我的代码有什么问题,尽管我已经解决了很多关于 AttributeErrors 的问题。

这是我的代码:

class Base:

def __init__(self):
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.window.set_position(gtk.WIN_POS_CENTER)
self.window.set_size_request(500,500*9/16)
self.window.set_title("pattern_executer")

self.button1 = gtk.Button(" E x i t ")
self.button1.connect("clicked",self.close)

self.button2 = gtk.Button("Plot")
self.button2.connect("clicked",self.plot)

self.button3 = gtk.Button("Search")
self.button3.connect("clicked",self.search)

self.entry1 = gtk.Entry()
self.entry1.connect("changed",self.dir_ch)
self.entry1.set_text("dir1")

self.entry2 = gtk.Entry()
self.entry2.connect("changed",self.dir_ch)
self.entry2.set_text("name")

self.entry3 = gtk.Entry()
self.entry3.connect("changed",self.dir_ch)
self.entry3.set_text("dir2")

#self.label1 = gtk.Label("FUNCTIONS")

fixed1 = gtk.Fixed()
fixed1.put(self.button1,10,250)
fixed1.put(self.button2,10,60)
fixed1.put(self.button3,10,30)
fixed1.put(self.entry1,90,30)
fixed1.put(self.entry2,90,60)
fixed1.put(self.entry3,90,90)


self.window.add(fixed1)
self.window.show_all()
self.window.connect("destroy",self.close)

def run(self,widget):

print "Run ... "


def search(self,widget):
box = (settings[1] - settings[3]/2,settings[2] - settings[4]/2,settings[1] + settings[3]/2,settings[2] + settings[4]/2)
cut1 = im.crop(box)
cut1.show()

def cut(self):
box = (settings[1] - settings[3]/2,settings[2] - settings[4]/2,settings[1] + settings[3]/2,settings[2] + settings[4]/2)
cut1 = im.crop(box)
return cut1

def gpv(self): #get pixel value
data = self.cut()
data = list(data.getdata())
data = np.array(data)
data = data.reshape(settings[4],settings[3])
data = np.flipud(data)
return data

def plot(self,widget):
xlist = np.linspace(0,1.0,settings[3])
ylist = np.linspace(0,1.0,settings[4])
X,Y = np.meshgrid(xlist, ylist)

fig = plt.figure()
fig = plt.contour(X, Y, self.gpv(),settings[0],colors = "k")
fig = plt.contourf(X, Y, self.gpv(),settings[0])

plt.show(fig)

def dir_ch(self,widget):
directories[0] = self.entry1.get_text()
directories[1] = self.entry2.get_text()
directories[2] = self.entry3.get_text()



def close(self,widget,data=None):
print "Closed"
gtk.main_quit()


def main(self):
gtk.main()

if __name__ == "__main__":
base = Base()
base.main()

如果我执行它,我会收到以下错误消息:

Traceback (most recent call last):
File "E:\cutter\cutter.py", line 113, in dir_ch
directories[1] = self.entry2.get_text()
AttributeError: Base instance has no attribute 'entry2'
Traceback (most recent call last):
File "E:\cutter\cutter.py", line 114, in dir_ch
directories[2] = self.entry3.get_text()
AttributeError: Base instance has no attribute 'entry3'

在我看来,有趣的问题是“entry1”与“entry2”和“entry3”完全相似,但不会产生任何问题。你知道为什么会出现这些错误吗?

非常感谢您的帮助!

最佳答案

原因是线条:

self.entry1.connect("changed",self.dir_ch)
self.entry1.set_text("dir1")

发生在 之前 entry2entry3 被定义,但是之后 entry1被定义为。这会导致调用 self.dir_ch 回调函数。然后该函数依赖于所有现有的 entry1entry2entry3

一个解决方法是定义所有三个字段,使用以下行:

    self.entry1 = gtk.Entry()
self.entry2 = gtk.Entry()
self.entry3 = gtk.Entry()

在连接或更改其中任何一个之前。

关于python - 回调函数中的 AttributeError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12802595/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com