2011-02-15 62 views
0

我的问题是,我有一个创建一个Tkinter的TOPCLASS对象,然后把现场变成它的一类,我想补充一点,运行的方法(这也是在事件处理程序类)每次按下按钮时,但是当事件被称为它说故障一类的内部Tkinter的事件处理程序

AttributeError: Toplevel instance has no attribute 'updateSearch'

class EditStudentWindow(): 

def __init__(self): 

    searchResultList = ['student1', 'student2', 'student3'] # test list 

    ##### window attributes 

    # create window 
    self = Tkinter.Toplevel() 

    #window title 
    self.title('Edit Students') 

    ##### puts stuff into the window 

    # text 
    editStudentInfoLabel = Tkinter.Label(self,text='Select the student from the list below or search for one in the search box provided') 
    editStudentInfoLabel.grid(row=0, column=0) 

    # entry box 
    searchRepositoryEntry = Tkinter.Entry(self) 
    searchRepositoryEntry.grid(row=1, column=0) 

    # list box 
    searchResults = Tkinter.Listbox(self) 
    searchResults.grid(row=2, column=0) 

    ##### event handler 

right here

searchRepositoryEntry.bind('<Key>',command = self.updateSearch) 
    # search results 

    for result in searchResultList: 
     searchResults.insert(Tkinter.END, result) 

def updateSearch(self, event): 
    print('foo')  
+0

你觉得`self = Tkinter.Toplevel()`做了什么? – delnan 2011-02-15 17:56:27

回答

1

对你的榜样的缩进仅仅来看,似乎updateSearch确实不属于定义类银行足球比赛。

假设缩进是一个标记错误,并根据您报告的错误消息,另一个问题是您重新定义了self,因此'self.updateSearch'指向顶层而不是EditStudentWindow类。请注意,该消息表示Toplevel instance has no attribute 'updateSearch'而不是EditStudentWindow instance...

通常,这些小部件是使用继承而非组合创建的。你可能想考虑重构你的代码,看起来像这样:

class EditStudentWindowClass(Tkinter.Toplevel): 
    def __init__(self, *args, **kwargs): 
     Tkinter.Toplevel.__init__(self, *args, **kwargs) 
     self.title('Edit Students') 
     ... 
+0

我试着添加继承,它仍然表示顶层没有该属性,是的,这是一个标记错误,我再次检查和代码缩进 – Brandon 2011-02-15 20:05:22