Reputation: 1
With this below Python code, I wanted to
But post typing "exit" and pressing "Enter" key from keyboard, the "tk window" is not closing.
And the Code is:
import tkinter as tk
window = tk.Tk()
greeting = tk.Label(text = "Test Tk window Frame")
user_input = tk.Text()
user_input.pack()
greeting.pack()
def chat(event=None):
inputmsg = user_input.get(tk.END)
if inputmsg is None or inputmsg == "":
return None
if inputmsg.lower() == "exit"
inputmsg.bind('<Return>', lambda e: window.destroy()) # NOT Working
#window.destroy() # Not Working
return None
user_input.bind("<Return>", chat) # NOT Working out
window.mainloop()
My intention is to Bind ONLY RETURN KEY and not with a Button. As I'm new to Python with tkinter, Can anyone please share any thought/ ref on this?
Thank you.
Upvotes: 0
Views: 430
Reputation: 5949
The source of problem is incorrect fetch of the entry text. So inputmsg
is never exit
after you enter it. You should use:
user_input.get(1.0, "end-1c")
Read this answer and its comments for further details about these parameters
UPDATE: if only the last line is required you can set your parameters in two ways:
user_input.get("end-5c", "end-1c") #extract 4 symbols before the last (which is '\n')
user_input.get("end-1c linestart", "end-1c lineend") #extract the last line from start to end
Upvotes: 1