回调函数中的AttributeError

2024-06-25 23:12:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我真的很抱歉因为AttributeError而打扰您,但是我只是不知道我的代码有什么问题,尽管我已经解决了许多关于AttributeError的问题。在

这是我的代码:

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

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

^{pr2}$

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

非常感谢你的帮助!在


Tags: textselfgtkdatasettingsputdefconnect
1条回答
网友
1楼 · 发布于 2024-06-25 23:12:36

原因是台词:

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

它发生在定义entry2或{}之前,但发生在定义entry1之后。这将导致调用self.dir_ch回调函数。然后该函数依赖于entry1entry2、和{}都存在。在

一种解决方法是定义所有三个字段,其中包括以下行:

^{pr2}$

在连接或更改它们之前。在

相关问题 更多 >