0

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

vatsal
  • 3
  • 6
  • First of all `ans = tk.Entry(...).grid(...)` doesn't do what you think it does. Is saves whatever `.grid` returns into `ans` instead of the actual `tkinter.Entry` widget. So change it to `ans = tk.Entry(...)` and `ans.grid(...)`. Also you should add `global ans` at the start of that function and later can just call `ans.get()` – TheLizzard Apr 22 '21 at 19:18
  • You could use: `widget.bind('', func)` – Matiiss Apr 22 '21 at 19:22
  • @TheLizzard I am new to tkinter and I was just learning about it, so it would be really helpfull if you could explain it in a bit more detailed way.. – vatsal Apr 22 '21 at 19:36
  • @vatsal Look at [this](https://stackoverflow.com/questions/1101750/tkinter-attributeerror-nonetype-object-has-no-attribute-attribute-name). Also that's just how python works. It isn't anything special with tkinter. – TheLizzard Apr 22 '21 at 19:40
  • when i run this ans.bind `("", print(print_entry))` it gives me this error `Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\Vatsa\AppData\Local\Programs\Python\Python36\lib\tkinter_init_.py", line 1699, in call return self.func(*args) File "C:\Users\Vatsa\OneDrive\Desktop\New folder\all main py files\The Minion game.py", line 44, in word ans.bind("", print(print_entry)) AttributeError: 'NoneType' object has no attribute 'bind'` – vatsal Apr 22 '21 at 19:50
  • @vatsal First of all you can get that error only if you didn't split the `ans = tk.Entry(...).grid(...)` into `ans = tk.Entry(...)` and `ans.grid(...)`. Second of all you have to pass in a function so use `ans.bind("", lambda event: print(ans.get()))` – TheLizzard Apr 22 '21 at 19:50
  • `def print_entry(event): print(ans.get()) ans.delete(0,END)` i split it like u told now it does not give the error but on running instead of printing what I type it prints this `` and also it does not delete the thing written in the widget – vatsal Apr 22 '21 at 19:53
  • @vatsal Did you change the `ans.bind("", print(print_entry))` into `ans.bind("", lambda event: print(print_entry))`? – TheLizzard Apr 22 '21 at 19:56
  • yes it still prints the same thing `print_entry at 0x0000023AAE469620>` and doesn't erase the thing written In the widget – vatsal Apr 22 '21 at 19:57
  • so what should i do now?? i changed it like you have told me to but it still doesn't print the thing written inside the widget – vatsal Apr 22 '21 at 20:04
  • @vatsal You have like three answer to look at, also I suggest looking at tutorials, since it would really help You if You learned how to create widgets in a loop – Matiiss Apr 22 '21 at 20:09

3 Answers3

1

To bind the return/enter key, use 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()
Tkinter Lover
  • 835
  • 3
  • 19
1

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.

TheLizzard
  • 7,248
  • 2
  • 11
  • 31
  • thanks for the help and sure i will look at some tutorials' – vatsal Apr 22 '21 at 20:11
  • 1
    Why not `event.widget.get()`. But that would make it similar to the answer below, but it would remove `global`. – Delrius Euphoria Apr 22 '21 at 22:26
  • 1
    @CoolCloud I am trying to teach OP the basics of how python works because most of the time people start with functional programming and for that you need to know how global variables work. But it isn't a bad idea. Feel free to edit my answer. – TheLizzard Apr 23 '21 at 07:32
1

Here is a way of doing this without the use of global (since I have heard they are "evil"):

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()
Matiiss
  • 5,970
  • 2
  • 12
  • 29
  • You don't need the lambda. The event object has a reference to the entry widget. You can use `event.widget.get()` inside your function. – Bryan Oakley Apr 22 '21 at 20:09
  • @BryanOakley oh, thanks for the reminder, completely forgot about that, edited the code now – Matiiss Apr 22 '21 at 20:11