Home > Enterprise >  How does self.tk.call work in the Text widget source code?
How does self.tk.call work in the Text widget source code?

Time:10-24

I am using tkinter for a data structures class and I'd like to use the text widget as part of a project. The prof asked what structure I was using and I realized I didn't know how tkinter built a container around the text widget.

I dug around in the source code a little bit, in __init__.py there's this section:

class Text(Widget, XView, YView):
    """Text widget which can display text in various forms."""

    def __init__(self, master=None, cnf={}, **kw):
        """Construct a text widget with the parent MASTER."""

    """ snipped stuff """

    def get(self, index1, index2=None):
        """Return the text from INDEX1 to INDEX2 (not included)."""
        return self.tk.call(self._w, 'get', index1, index2)

I thought that if I was able to dig into the code I'd be able to figure out what is running under the hood, but I hit a snag. I cannot figure out why everything is running this self.tk.call(self._w, 'whatever', ...) in all the functions.

Why is it calling this other function "call" and where is "call"?

In short answer terms I'm interested in knowing where get is being called from so I can figure out what kind of container is being used for the text, but now I'm also broadly interested in why there's a whole bunch of calls to this "call" function in all of the source code as well. It isn't just the text widget that does this. I couldn't figure out where "call" was going. Appreciate any direction or guidance down that path as well.

CodePudding user response:

Summarized comments, to preserve for future interest:

@jasonharper, pointed out:

NONE of Tkinter is actually implemented in Python - the actual GUI (Tk) is implemented in the Tcl programming language. Tkinter embeds a Tcl interpreter, available as tk from any widget; the .call() method submits command lines to that interpreter, which is basically the only way that anything GUI-related actually happens.

OP, specified his question with:

I was using container to mean the data structure that holds the text in the widget. Whatever data type that member is. Some kind of linked list, or a tree, or whatever.

And the documentation states:

Text is represented internally with a modified B-tree structure that makes operations relatively efficient even with large texts. Tags are included in the B-tree structure in a way that allows tags to span large ranges or have many disjoint smaller ranges without loss of efficiency. Marks are also implemented in a way that allows large numbers of marks. In most cases it is fine to have large numbers of unique tags, or a tag that has many distinct ranges.

The actual B-Tree source code is on github for further examinations

  • Related