tuts fun
tuts fun

Reputation: 117

How to use textfield key press event using python

if i write some thing on the textfield and press enter key on the keyboard.what i entered on the textfield display messagebox. this point get error if(format(k=event.char(13)))) : if i set the enter key code. i added full code below.

from tkinter import *

root = Tk()

root.geometry("800x800")
global e1

def callback(event):
    if(format(k=event.char(13)))):
           
    msg = e1.get()
    
    print(msg)


Label(root, text="Student Name").place(x=140, y=40)

e1 = Entry(root)
e1.place(x=140, y=10)
e1.bind('<Key>',callback)



root.mainloop()

Upvotes: 0

Views: 164

Answers (1)

Delrius Euphoria
Delrius Euphoria

Reputation: 15088

Try this out

from tkinter import *

root = Tk()
root.geometry("800x800")

def callback(event):           
    msg = e1.get()
    print(msg)

Label(root, text="Student Name").place(x=140, y=40)

e1 = Entry(root)
e1.place(x=140, y=10)
e1.bind('<Return>',callback) #<Return> is equivalent to your Enter key

root.mainloop()

When you click on Enter key, on the entry widget, then the function gets called and the output will be printed out. I also removed the global as it makes no sense to use it in outside functions.

Hope it helped you out.

Cheers

Upvotes: 1

Related Questions