0

这是我的代码以及错误消息。

from Tkinter import *

class Window01 (Frame):


def __init__(self, master):
Frame.__init__(self)
self.reveal()
self.create_widget()
self.grid()


def create_widget(self):

self.lbl = Label (self, text = "This is a Widget App.")
self.lbl.grid(row =1, column =0, columnspan =2, sticky = W)

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

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

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

def reveal (self):
contents = self.entbx.get()

if  contents =="magic":
message = "Access Granted"

else:
message = "Denied"
self.txt.delete(0.0, END)
elf.txt.insert(0.0, message)


root = Tk()
root.title ("Widget_Button")
root.geometry ("300x150")
app = Window01 (root)
root.mainloop()

文件“C:\PyDev\Py_Widgets101\src\Py_Widget03.py”,第 10 行,在init self.reveal() 文件“C:\PyDev\Py_Widgets101\src\Py_Widget03.py”,第 30 行,显示内容 = self .entbx.get() AttributeError: Window01 实例没有属性 'entbx'

4

1 回答 1

1

self.entbx由创建create_widget()。你打电话reveal()——这需要self.entbx——在你打电话之前create_widget()

self.reveal()
self.create_widget()
于 2013-06-29T07:11:22.993 回答