Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AttributeError in callback function

I'm really sorry to bother you with an AttributeError, but I just can't figure out what's wrong with my code though I worked through many solved question regarding AttributeErrors.

This is my code:

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

If I execute it, I get the following error message:

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'

The funny issue - to my opinion - is that "entry1" is absolutely similar to "entry2" and "entry3" but doesn't make any problems. Do you know why these errors occur?

Thank you very much for your help!

like image 731
Josef Pörnbacher Avatar asked Oct 09 '12 14:10

Josef Pörnbacher


1 Answers

The reason is the lines:

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

Which happens before entry2 or entry3 is defined, but after entry1 is defined. This causes the self.dir_ch callback function to be called. That function then relies on entry1, entry2, and entry3 all existing.

One fix would be to define all three fields, with the lines:

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

before connecting or changing any of them.

like image 125
David Robinson Avatar answered Sep 20 '22 20:09

David Robinson