2015-10-27 83 views
1

我可以成功地打開程序,但每當我給密碼輸入一個輸入,它就會給我這個回溯/錯誤。在python中導致這個屬性錯誤的原因是什麼?

Exception in Tkinter callback 
Traceback (most recent call last): 
    File "C:\Python27\lib\lib-tk\Tkinter.py", line 1536, in __call__ 
    return self.func(*args) 
    File "C:/Users/Frederik/Desktop/password.py", line 30, in reveal 
    content = self.password.get() 
AttributeError: Label instance has no attribute 'get' 

我很新的python,所以我真的不知道是什麼原因導致此錯誤。我的代碼如下所示:

from Tkinter import * 

class Application(Frame): 
    """ GUI App """ 

    def __init__(self, master): 
    """ Init the frame """ 
    Frame.__init__(self,master) 
    self.grid() 
    self.create_widgets() 

    def create_widgets(self): 
    """ Button, text and entry """ 
    self.instruction = Label(self, text = "Enter the password") 
    self.instruction.grid(row = 0, column =0, columnspan = 2, sticky = W) 
    self.password = Label(self, text = "Password: ") 
    self.password.grid(row = 1, column = 0, columnspan = 2, sticky = W) 

    self.enterpassword = Entry(self) 
    self.enterpassword.grid(row = 1, column = 1, sticky = W) 

    self.submit_button = Button(self, text ="Submit", command = self.reveal) 
    self.submit_button.grid(row = 2, column = 0, sticky =W) 

    self.text = Text(self, width = 35, height = 5, wrap = WORD) 
    self.text.grid(row = 3, column = 0, columnspan = 2, sticky = W) 

    def reveal(self): 
    """ Display """ 
    content = self.password.get() 

    if content == "fek_u_D8lan!": 
     message = "Gratz, you feked D8lan!" 

    else: 
     message = "U failed to fek D8lan!" 
    self.text.delete(0.0, END) 
    self.text.insert(0.0, message) 

root = Tk() 
root.title("Fek Dolan") 
root.geometry("250x150") 
app = Application(root) 

root.mainloop() 

回答

3

你應該得到enterpassword(在Entry),而不是password(該Label)。這應該工作:

content = self.enterpassword.get()

+0

謝謝你的工作。我不知道我是如何錯過這一點的。哦,謝謝 – wardz

相關問題