Any of those help A simple solution that works most of the time would be to put a binding on . That will enable a function to be called whenever the user is typing. This won't trigger the callback whenever data is pasted with the mouse, or inserted via other means (such as a toolbar button). A more robust solution is to set up a proxy for the widget, so that an event is generated whenever anything is inserted or deleted in the widget. This proxy can look at what is being done with the widget (insert, delete, selection changed, etc) and generate an event. You can then bind to this event to do whatever you want.
import tkinter as tk
def __init__(self, *args, **kwargs):
"""A text widget that report on internal widget commands"""
tk.Text.__init__(self, *args, **kwargs)
# create a proxy for the underlying widget
self._orig = self._w + "_orig"
self.tk.call("rename", self._w, self._orig)
def _proxy(self, command, *args):
cmd = (self._orig, command) + args
result = self.tk.call(cmd)
if command in ("insert", "delete", "replace"):
import tkinter as tk
# ... import of definition of CustomText goes here ...
root = tk.Tk()
label = tk.Label(root, anchor="w")
text = CustomText(root, width=40, height=4)
text.pack(side="top", fill="both", expand=True)
chars = len(event.widget.get("1.0", "end-1c"))
label.configure(text="%s chars" % chars)
wish helps you I am writing a Python program in TKinter on Ubuntu to import and print the name of files from particular folder in Text widget. It is just adding filenames to the previous filnames in the Text widget, but I want to clear it first, then add a fresh list of filenames. But I am struggling to clear the Text widget's previous list of filenames. , I checked on my side by just adding '1.0' and it start working
How to get the contents of last line in tkinter text widget (Python 3)
it fixes the issue You can apply modifiers to the text widget indicies, such as linestart and lineend as well as adding and subtracting characters. The index after the last character is "end". Putting that all together, you can get the start of the last line with "end-1c linestart".
hop of those help? You can use the count method to get the number of pixels between the first and last characters. This method takes two indexes, and then one or more strings representing what you want to count: chars, displaychars, displayindices, displaylines, indices, lines, xpixels, or ypixels. The return value is a tuple containing the count of each requested item. For example, to count the pixels from the top of the first character to the bottom of the last character you could do this:
ypixels = text.count("1.0", "end", "ypixels")
Python/Tkinter: How to set text widget contents to the value of a variable?