5

真的很抱歉用 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”绝对相似,但不会产生任何问题。你知道为什么会出现这些错误吗?

非常感谢您的帮助!

4

1 回答 1

6

原因在于以下几行:

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()

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

于 2012-10-09T14:51:40.537 回答