from tkinter import *
main_window = Tk ()
main_window.title("Register Form")
frame = Frame (main_window)
frame.pack()
l_name = Label (frame, text = "Name:")
l_name.grid()
e_name = Entry (frame)
e_name.grid(row = 0, column = 1)
def show_welcome_message(event):
l_welcome = Label (frame, text = "Welcome, {}".format(e_name.get()))
l_welcome.grid(row =2, columnspan =2)
e_name.delete(0,END) #apaga o conteúdo de qualquqer entre box
button = Button(frame, text = "Ok")
button.bind("<Return>", show_welcome_message) #<Button-1> comando de clicar com o botão esquerdo do mouse
button.grid(row=1, columnspan =2)
main_window.mainloop()
A run the code, type the text and press Enter
but the welcome message is not apearing.Why this is not working?
Your button needs to have the focus to receive key events. you can set focus to your button to check this. (button.focus()
)
But I think you should be binding <Return>
to Entry
and not the button if you want to show the label when Enter key is pressed after completing the entry.
...
e_name = Entry (frame)
e_name.grid(row = 0, column = 1)
def show_welcome_message(event=None):
l_welcome = Label (frame, text = "Welcome, {}".format(e_name.get()))
l_welcome.grid(row =2, columnspan =2)
e_name.delete(0,END) #apaga o conteúdo de qualquqer entre box
button = Button(frame, text = "Ok", command=show_welcome_message)
e_name.bind("<Return>", show_welcome_message)
#<Button-1> comando de clicar com o botão esquerdo do mouse
button.grid(row=1, columnspan =2)
#button.focus()
main_window.mainloop()