如何在 python tkinter 中制作条目小部件在按下回车键时给我其中的文本

how to make entry widget in python tkinter give me the text inside it on pressing enter key

所以,最近,我在 python Tkinter 上制作这个游戏,电脑随机给我一个单词,我必须输入我在单词中看到的单词和字母,所以这里的问题是,“ans” 是一个条目小部件,我希望它的工作方式是,一旦我输入完我在给定单词中看到的单词或字母,我应该按下回车键,当我按下回车键时,计算机应该看到小部件内写的任何内容,并且把它放在一个变量中,这样我就可以将该变量键入屏幕右侧 (a1-a18) 标签的文本,然后按回车键它应该完成所有这些操作,并删除之前写在里面的所有内容输入小部件

def word():
    rt = Tk()
    rt.geometry("2445x990+0+0")
    rt.title("the minion game")

    tops = Frame(rt, width =1540, height = 10,bg = "steel blue", relief = SUNKEN)
    tops.pack(side=TOP)

    side = Frame(rt, width = 1540, height = 800, bg = "steel blue", relief = SUNKEN)
    side.pack(side=LEFT)

    sider = Frame(rt, width = 831, height = 800, bg = "steel blue", relief = SUNKEN)
    sider.pack(side=RIGHT)

    words = ["banana", "period", "bottle", "company", "pencil", "fishes"]
    word = random.choice(words)

    lblInfo = Label(tops, font=('arial',30,'bold', 'underline'), text ="   P1                                               Your word is: " + word + "                                                   ", width = 67, justify = "left" , bg = "steel blue", bd = 1, anchor = 'w')
    lblInfo.grid(row=0, column=0)

#left side
    ans = tk.Entry(side,textvariable = name ,bg = "steel blue", font=('arial',15,'bold'),width = 40, insertwidth=4, bd=15, justify = "left").grid(row=3,column=0)

    lblInfo = Label(side, font=('arial',50,'bold'), text = "         Type here", width = 17,justify = "left" , bg = "steel blue", bd = 1, anchor = 'w')
    lblInfo.grid(row=1, column=0)

    lblInfo = Label(side, font=('arial',50,'bold', 'underline'), text = "", width = 9,justify = "left" , bg = "steel blue", bd = 1, anchor = 'w')
    lblInfo.grid(row=2, column=0)

    lblInfo = Label(side, font=('arial',50,'bold', 'underline'), text = "", width = 9,height = 5,justify = "left" , bg = "steel blue", bd = 1, anchor = 'w')
    lblInfo.grid(row=4, column=0)

    lblInfo = Label(side, font=('arial',50,'bold', 'underline'), text = "", width = 9,height = 3,justify = "left" , bg = "steel blue", bd = 1, anchor = 'w')
    lblInfo.grid(row=0, column=0)

    lblInfo = Label(side, font=('arial',5,'bold', 'underline'), text = "", width = 1,height = 35,justify = "left" , bg = "black", bd = 1, anchor = 'w')
    lblInfo.grid(row=0, column=1)

    lblInfo = Label(side, font=('arial',5,'bold', 'underline'), text = "", width = 1,height = 100,justify = "left" , bg = "black", bd = 1, anchor = 'w')
    lblInfo.grid(row=4, column=1)

    lblInfo = Label(side, font=('arial',5,'bold', 'underline'), text = "", width = 1,height = 13,justify = "left" , bg = "black", bd = 1, anchor = 'w')
    lblInfo.grid(row=3, column=1)

    lblInfo = Label(side, font=('arial',5,'bold', 'underline'), text = "", width = 1,height = 13,justify = "left" , bg = "black", bd = 1, anchor = 'w')
    lblInfo.grid(row=1, column=1)

    lblInfo = Label(side, font=('arial',5,'bold', 'underline'), text = "", width = 1,height = 15,justify = "left" , bg = "black", bd = 1, anchor = 'w')
    lblInfo.grid(row=2, column=1)

#right side
    lblInfo = Label(sider, font=('arial',30,'bold', 'underline'), text = "", width = 1,height = 2,justify = "left" , bg = "steel blue", bd = 1, anchor = 'w')
    lblInfo.grid(row=0, column=0)

    lblInfo = Label(sider, font=('arial',35,'bold'), text = "your words:", width = 35,justify = "right" , bg = "steel blue", bd = 1, anchor = 'w')
    lblInfo.grid(row=1, column=0)

    a1 = Label(sider, font=('arial',15,'bold'), text = "a1", width = 35,justify = "right" , bg = "steel blue", bd = 1, anchor = 'w')
    a1.grid(row=2, column=0)

    a2 = Label(sider, font=('arial',15,'bold'), text = "a4", width = 35,justify = "right" , bg = "steel blue", bd = 1, anchor = 'w')
    a2.grid(row=3, column=0)

    a3 = Label(sider, font=('arial',15,'bold'), text = "a3", width = 35,justify = "right" , bg = "steel blue", bd = 1, anchor = 'w')
    a3.grid(row=4, column=0)

    a4 = Label(sider, font=('arial',15,'bold'), text = "a2", width = 35,justify = "right" , bg = "steel blue", bd = 1, anchor = 'w')
    a4.grid(row=5, column=0)

    a5 = Label(sider, font=('arial',15,'bold'), text = "a5", width = 35,justify = "right" , bg = "steel blue", bd = 1, anchor = 'w')
    a5.grid(row=6, column=0)

    a6 = Label(sider, font=('arial',15,'bold'), text = "a6", width = 35,justify = "right" , bg = "steel blue", bd = 1, anchor = 'w')
    a6.grid(row=7, column=0)

    a7 = Label(sider, font=('arial',15,'bold'), text = "a7", width = 35,justify = "right" , bg = "steel blue", bd = 1, anchor = 'w')
    a7.grid(row=8, column=0)

    a8 = Label(sider, font=('arial',15,'bold'), text = "a8", width = 35,justify = "right" , bg = "steel blue", bd = 1, anchor = 'w')
    a8.grid(row=9, column=0)

    a9 = Label(sider, font=('arial',15,'bold'), text = "a9", width = 35,justify = "right" , bg = "steel blue", bd = 1, anchor = 'w')
    a9.grid(row=10, column=0)

    a10 = Label(sider, font=('arial',15,'bold'), text = "a10", width = 35,justify = "right" , bg = "steel blue", bd = 1, anchor = 'w')
    a10.grid(row=11, column=0)

    a11 = Label(sider, font=('arial',15,'bold'), text = "a11", width = 35,justify = "right" , bg = "steel blue", bd = 1, anchor = 'w')
    a11.grid(row=12, column=0)

    a12 = Label(sider, font=('arial',15,'bold'), text = "a12", width = 35,justify = "right" , bg = "steel blue", bd = 1, anchor = 'w')
    a12.grid(row=13, column=0)

    a13 = Label(sider, font=('arial',15,'bold'), text = "a13", width = 35,justify = "right" , bg = "steel blue", bd = 1, anchor = 'w')
    a13.grid(row=14, column=0)

    a14 = Label(sider, font=('arial',15,'bold'), text = "a10", width = 35,justify = "right" , bg = "steel blue", bd = 1, anchor = 'w')
    a14.grid(row=15, column=0)

    a15 = Label(sider, font=('arial',15,'bold'), text = "a11", width = 35,justify = "right" , bg = "steel blue", bd = 1, anchor = 'w')
    a15.grid(row=16, column=0)

    a16 = Label(sider, font=('arial',15,'bold'), text = "a12", width = 35,justify = "right" , bg = "steel blue", bd = 1, anchor = 'w')
    a16.grid(row=17, column=0)

    a17 = Label(sider, font=('arial',15,'bold'), text = "a13", width = 35,justify = "right" , bg = "steel blue", bd = 1, anchor = 'w')
    a17.grid(row=18, column=0)

    a18 = Label(sider, font=('arial',15,'bold'), text = "a13", width = 35,justify = "right" , bg = "steel blue", bd = 1, anchor = 'w')
    a18.grid(row=19, column=0)

如果有人知道我该怎么做以及在这段代码中的什么地方添加它,请告诉我。

谢谢

要绑定 return/enter 键,请使用 entry.bind("<Return>", func):

from tkinter import *

class UI:
    def __init__(self):
        self.entry = Entry(root)
        # Bind the return key
        self.entry.bind("<Return>", self.func)
        self.entry.pack()

    def func(self, event):
        print(self.entry.get())


root = Tk()
UI()
root.mainloop()

试试这个:

def word():
    global ans
    ...
    ans = Entry(side, ...)
    ans.grid(...)
    ans.bind("<Return>", enter_pressed)
    ...

def enter_pressed(event):
    print(ans.get())
    ans.delete(0, "end")

此外,我建议您查看一些编程教程和 this

这是一种不使用 global 的方法(因为我听说它们是“邪恶的”):

from tkinter import Entry, Tk


def func(event):
    print(event.widget.get())
    event.widget.delete(0, 'end')


def main():
    root = Tk()

    entry = Entry(root)
    entry.pack()
    entry.bind('<Return>', func)

    root.mainloop()


main()