为什么我会得到这个类tkinter.Entry.get() AttributeError error.


我很

抱歉我的英语不好。

当我尝试从类外get()名为entryEntry时,我收到错误。

错误:

AttributeError: 'function' object has no attribute 'entry' ""

示例代码:

class asd:
    def gui(self, guiwindow, labelplacex, labelplacey, textx, texty, entryx, entryy):
        self.root = tk
        self.image = tk.PhotoImage(file=aimage_path)
        self.label = tk.Label(image=self.image)
        self.label.pack()
        self.label.place(x=labelplacex , y=labelplacey)
        self.text = Label(guiawindow, text="Code:")
        self.text.pack()
        self.text.place(x = textx,y = texty)
        self.entry = Entry(guiwindow)
        self.entry.pack()
        self.entry.place(x = entryx,y = entryy)
        self.button = Button(Tk(), text="Back Menu", command=self.asdfg)
        self.button.pack()
        self.root.mainloop()
asd.gui.entry.get()

怎么做??

你有两个问题。首先,您需要创建一个 asd 的实例,其次,您需要调用 gui 方法。

例如,像这样:

asd_instance = asd()
asd_instance.gui(...)
asd_instance.entry.get()

Not:...表示您需要传递给gui的所有参数,这些参数与问题无关。关键是,必须先创建类的实例,然后才能获取实例的属性。在这种情况下,在调用 gui 方法之前,所需的属性将不存在。因此,必须首先创建一个实例,然后调用 gui 方法,然后才能访问该属性。

请尝试asd.entry.get()

假设已经定义了self.entry,你应该能够通过调用类本身和变量来访问它,因为单词"self"意味着类本身。

在这种情况下,GUI只是一个函数,self.entry属于类ASD,而不是函数GUI。

最新更新