import tkinter as tk
win = tk.Tk()
win.resizable('False', 'False')
#functions
def entered(): #Receives input by the user and deletes what it has written.
value = entry.get()
entry.delete(0,'end')
def delete_insert(event):#Deletes what it's written by default in the entry widget.
entry.delete(0, 'end')
entry.config(fg = 'black')
def add_insert(event):
entry.insert(0, 'Click Here')
frame = tk.Frame(win)
frame.grid(column = 0, row = 0)
button_entry = tk.Button(frame)
button_entry.pack(side = 'right')
button_entry.config(text = 'Buscar', command = entered)
button_entry.bind('Control-Enter', entered) #Keep this line in mind please.
entry = tk.Entry(frame)
entry.pack(side = 'right', padx = 5)
entry.insert(0,'Click Here') #Default text in entry widget.
entry.bind('<FocusIn>', delete_insert)
entry.bind('<FocusOut>', add_insert)
win.mainloop()
The line of code which binds button_entry
with entered()
subprogramn when pressing Enter
doesn't seem work.
button_entry.bind('<Control-Enter>', entered)
Why not? When entered()
subprogramn is run, it deletes the text inserted in entry
widget
def entered(event):
....
entry.delete(0, 'end')
....
entry.insert(0,'Click here')
Thus, my goal is, when the button button_entry
is activated by Enter
key, it runs subprogram entered()
. Therefore, it deletes the text inserted in entry
by the user.
I add the parameter event
to entered()
as follows entered(event)
. Like with the other functions so the binding would work. Unfortunately, when you click button_entry
you get the following error:
TypeError: entered() missing 1 requiered positional argument: 'event'
Although, If you click Enter
, the key on your keyboard, nothing happens, or atleast it seems like it because the subprogram I've bound to button_entry
isn't executed
What I'm trying to accomplish is similar to what you do when you google things , you input what you want in the search engine and then click enter on your keyboard.
Two mistakes, your binding key is wrong and your function call also seems wrong. Plus you are binding to the button, then it requires the button to have focus, only then bind
will work. So your code would be:
def entered(event=None):
value = entry.get()
entry.delete(0,'end')
entry.bind('<Return>',lambda event: entered()) # Bind the enter key to the entry
# OR win.bind(same thing above)
event
is set to None
so that when the button tries to call this function and no args are passed, it wont throw an error at you.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.