Python tk.entry无法在另一个类中获取()输入

Python tk.entry无法在另一个类中获取()输入,python,tkinter,Python,Tkinter,我试图了解tkinter.entry是如何工作的。 我希望用户输入一些文本,然后当按下enter键时,它将检查字典中的文本并返回相关的字典结果 def enterhit(event): populate_ISBNs() def populate_ISBNs(): print("You hit return.") print(sku.get()) #this should print what is in the entry box class Page1(Page)

我试图了解tkinter.entry是如何工作的。 我希望用户输入一些文本,然后当按下enter键时,它将检查字典中的文本并返回相关的字典结果

def enterhit(event):
    populate_ISBNs()

def populate_ISBNs():
    print("You hit return.") 
    print(sku.get()) #this should print what is in the entry box

class Page1(Page):
    def __init__(self, *args, **kwargs):
        Page.__init__(self, *args, **kwargs)
        tk.Label(self, text="SKU").grid(row=1)
        sku = tk.Entry(self).grid(row=1, column=1) #this is the entry field
        tk.Button(self, text="Search", command=populate_ISBNs).grid(row=1, column=2)

if __name__ == "__main__":
    root = tk.Tk()
    main = MainView(root)
    main.pack(side="top", fill="both", expand=True)
    root.wm_geometry("400x400")

    root.bind('<Return>', enterhit)

    root.mainloop()
def enterhit(事件):
填充_ISBNs()
def populate_ISBNs():
打印(“你点击了回车键。”)
打印(sku.get())#这应该打印输入框中的内容
班级第1页(第页):
定义初始化(self,*args,**kwargs):
第页.uuu初始(self,*args,**kwargs)
tk.Label(self,text=“SKU”).grid(行=1)
sku=tk.Entry(self).grid(行=1,列=1)#这是输入字段
按钮(self,text=“Search”,command=populate\u ISBNs).grid(行=1,列=2)
如果名称=“\uuuuu main\uuuuuuuu”:
root=tk.tk()
main=MainView(根目录)
main.pack(side=“top”,fill=“both”,expand=True)
root.wm_几何体(“400x400”)
root.bind(“”,enterhit)
root.mainloop()
当输入或按下“搜索”按钮时,它应该打印“you hit return.”,然后是在名为“sku”的条目中键入的内容


它会打印“您点击返回”,但也会打印“类型对象‘Page1’没有属性‘sku’”

您应该在条目中添加并定义一个文本变量

sku_var=tk.StringVar()#定义textvariable
tk.Entry(self,textvariable=sku_var)#设置textvariable
在populate_ISBNs()处,将其带到类本身 成功


班级第1页(第页):
sku_var=tk.StringVar()#定义textvariable
定义初始化(self,*args,**kwargs):
第页.uuu初始(self,*args,**kwargs)
tk.Label(self,text=“SKU”).grid(行=1)
tk.Entry(self,textvariable=self.sku_var)#设置textvariable
按钮(self,text=“Search”,command=populate\u ISBNs).grid(行=1,列=2)
def填充ISBNs(自身):
打印(“你点击了回车键。”)
打印(self.sku_var.get())#这应该打印输入框中的内容
(编辑)

另外,由于您使用的是OOP(面向对象编程)

我在类Page1中做了更改:
sku\u var=tk.StringVar()tk.Entry(self,textvariable=sku\u var)tk.Button(self,text=“Search”,command=populate\u ISBNs)
然后将引用更改为
print(Page1.sku_var.get())
但是我仍然收到相同的错误添加了另一个编辑,我认为这会帮助您并使其定向。我尝试了您的方法,但现在我收到错误“name”populate_ISBNs“未定义”我然后尝试移动populate_ISBNs()上面的init,但发生了相同的错误。确实不需要使用textvariable。可以,但它会添加一个需要管理的额外对象。您可以在没有关联变量的情况下获取和设置条目的值。什么是
页面