I'm trying to create an app where a part of it involves a label that dynamically changes and displays the number of characters typed within the textbox every time it gets updated. I couldn't find any information regarding this and unsure whether this is even possible.
The most foolproof way to get notified when the text widget changes is to intercept the low-level insert, delete, and replace commands. When they occur, an event can be generated, which you can then bind to.
This involves a tiny bit of tcl voodoo, but it works, and works whether you type into the widget, paste using the mouse, or directly call the insert or delete methods.
Here is a complete example:
import tkinter as tk
class CustomText(tk.Text):
def __init__(self, *args, **kwargs):
"""A text widget that supports a <<TextChanged>> event"""
tk.Text.__init__(self, *args, **kwargs)
self.tk.eval('''
proc widget_proxy {widget widget_command args} {
# call the real tk widget command with the real args
set result [uplevel [linsert $args 0 $widget_command]]
# if the contents changed, generate an event we can bind to
if {([lindex $args 0] in {insert replace delete})} {
event generate $widget <<TextModified>> -when tail
}
# return the result from the real widget command
return $result
}
''')
# this replaces the underlying widget with the proxy
self.tk.eval('''
rename {widget} _{widget}
interp alias {{}} ::{widget} {{}} widget_proxy {widget} _{widget}
'''.format(widget=str(self)))
def update_char_count(event):
count = event.widget.count("1.0", "end-1c")
# count is a tuple; the character count is the first element
count = 0 if not count else count[0]
label.configure(text=f"Characters: {count}")
root = tk.Tk()
text = CustomText(root)
label = tk.Label(root, anchor="w")
label.pack(side="bottom", fill="x")
text.pack(fill="both", expand=True)
text.bind("<<TextModified>>", update_char_count)
root.mainloop()
If you want a simpler solution that requires a bit more work by the caller, look at this:
import tkinter as tk
def callback(event):
# After 1 ms call `_callback`
# That is to make sure that tkinter has handled the keyboard press
root.after(1, _callback)
def _callback():
# The `-1` is there because when you have `text_widget.get(..., "end")`
# It adds a "\n" character at then end
number_of_chars = len(text_widget.get("1.0", "end")) - 1
print(number_of_chars)
root = tk.Tk()
text_widget = tk.Text(root)
text_widget.pack()
text_widget.bind("<Key>", callback)
root.mainloop()
This basically binds all keyboard presses to the callback
function. I can't think of any way to change the text in a text widget without pressing a keyboard button.
Please note that it will only run when a key is pressed so it wouldn't work if you called text_widget.insert
or text_widget.delete
(@acw1668 found that problem). It is the caller's responsibility to call _callback
just after they call .insert
/ .delete
.
An even simpler answer is always to use what someone has already created:D
# `idlelib` is part of the standard library
from idlelib.redirector import WidgetRedirector
import tkinter as tk
def my_insert(*args):
original_insert(*args)
update_label()
def my_delete(*args):
original_delete(*args)
update_label()
def my_replace(*args):
original_replace(*args)
update_label()
def update_label():
number_of_chars = len(text.get("1.0", "end")) - 1
print(number_of_chars)
root = tk.Tk()
text = tk.Text(root)
text.pack()
redir = WidgetRedirector(text)
original_insert = redir.register("insert", my_insert)
original_delete = redir.register("delete", my_delete)
original_replace = redir.register("replace", my_replace)
root.mainloop()
This solution works even if you use text.insert(...)
For more info on how it works just use help(WidgetRedirector)
.
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.