pythontkintertkinter-entry

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


So, lately, I was making this game on python Tkinter where the computer gives me a random word and I have to type out the words and letters I see in the word give to me, so the problem here is that, "ans" is an entry widget and I want it to work like once I am done typing the word or letter I see in the given word I should press the enter key and when I press the enter key the computer should see whatever is written inside the widget and take it inside a variable so that I can type that variable as the text of the label present on the right side (a1-a18) of the screen, and on pressing enter it should do all that and also delete everything that was previously written inside the entry widget

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)

if someone has any idea how I can do that and where to add it inside this code pls do tell me.

Thankyou


Solution

  • Try this:

    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")
    

    Also I suggest that you look at some programming tutorials and this.