I am trying to implement a text widget in tkinter
which will allow input text for only a specified time (here 5 secs) and then capture the typed text, without using a submit button calling a function.
I want the time to start as soon as user starts typing and shall prevent user inputting any longer after 5secs. The text that was inputted thus far shall be catured.
I tried the below code which is not working. I tried looking in the documentation and did web search and many stackoverflow
discussion threads. I couldn't find an answer. Appreciate inputs on a solution.
from tkinter import *
my_window = Tk()
type_txt = Text()
type_txt.grid(row=0, column=0)
type_txt.focus()
type_txt.after(5000, type_txt.configure(state=DISABLED))
typed_text = type_txt.get("1.0", END)
print(typed_text)
my_window.mainloop()
You can bind <key>
event to a function, then inside the callback to disable the text box 5 seconds later using .after()
.
from tkinter import *
my_window = Tk()
type_txt = Text()
type_txt.grid(row=0, column=0)
type_txt.focus()
def disable_textbox():
type_txt.configure(state=DISABLED)
typed_text = type_txt.get("1.0", END)
print(typed_text)
def start_typing(event):
# disable <Key> binding
type_txt.unbind('<Key>')
# disable text box 5 seconds later
type_txt.after(5000, disable_textbox)
type_txt.bind('<Key>', start_typing)
my_window.mainloop()