什么原因导致python中的属性错误?

时间:2015-10-27 09:12:33

标签: python tkinter

我可以成功地打开程序,但每当我给密码输入一个输入时,它就会给我这个追溯/错误。

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

1 个答案:

答案 0 :(得分:3)

您应该获得enterpasswordEntry),而不是passwordLabel)。这应该有效:

content = self.enterpassword.get()