Python 为什么我得到这个类tkinter.Entry.get()AttributeError错误?
我很抱歉我的英语不好 尝试从类外获取名为Python 为什么我得到这个类tkinter.Entry.get()AttributeError错误?,python,python-3.x,function,user-interface,tkinter,Python,Python 3.x,Function,User Interface,Tkinter,我很抱歉我的英语不好 尝试从类外获取名为Entry的Entry时出错 错误: AttributeError:“函数”对象没有属性“条目” 示例代码: class asd: def gui(self, guiwindow, labelplacex, labelplacey, textx, texty, entryx, entryy): self.root = tk self.image = tk.PhotoImage(file=aimage_path)
Entry
的Entry
时出错
错误:
AttributeError:“函数”对象没有属性“条目”
示例代码:
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.entry.get()
假设已经定义了self.entry,您应该能够通过调用类本身,然后调用变量来访问它,因为单词“self”表示类本身
本例中的gui只是一个函数,self.entry属于类asd,而不是函数gui。您有两个问题。首先,需要创建
asd
的实例,其次,需要调用gui
方法
例如,类似这样的内容:
asd_instance = asd()
asd_instance.gui(...)
asd_instance.entry.get()
不是:
…
表示需要传递到gui
的所有参数,这些参数与问题无关。关键是,必须先创建类的实例,然后才能获取实例的属性。在这种情况下,除非调用gui
方法,否则所需的属性将不存在。因此,您必须首先创建一个实例,然后调用gui
方法,然后才能访问属性;它不会有条目的实例。该类需要首先实例化。是的,你是对的,我只是假设他没有发布完整的代码,因为他说了示例代码,我的坏@ÖmerFarukBoran:我遗漏了调用gui()
的所有参数,因为它们与解决方案无关,但否则它应该可以工作。我会尽量说清楚的。