tatmius
tatmius

Reputation: 143

How Tkinter font.families() function gets a list of available font?

I'm making GUI application using Tkinter and I'm using tkinter.font.families() for making pull down list of selecting fonts.

Now, I'm wondering how this function works
I tried reading the code but I can't understand it because of my lack of enough knowledge...

def families(root=None, displayof=None):
    "Get font families (as a tuple)"
    if not root:
        root = tkinter._default_root
    args = ()
    if displayof:
        args = ('-displayof', displayof)
    return root.tk.splitlist(root.tk.call("font", "families", *args))

This is a code of the function(https://github.com/python/cpython/blob/c4928fc1a853f3f84e2b4ec1253d0349137745e5/Lib/tkinter/font.py#L180).

my qeustion is

Thank you for reading my question.

Upvotes: 0

Views: 368

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385970

What is the meaning of args = ('-displayof', displayof)?

Tkinter is a small python wrapper around a tcl interepreter which has the tk package installed. All of the actual work of creating widgets, querying fonts, etc, is done by the tcl/tk libraries.

Tcl/tk commands use a leading dash to represent an option name, and the option is typically followed by a value. This code is preparing to call a tcl function, so it is building up a list of arguments required by tcl.

In this specific case, it's adding the -displayof option if the displayof argument was passed to the function.

Where does tk.call() function came from?

It comes from a C-based library. It is defined in a file named _tkinter.c. More specifically, it's the function Tkapp_Call. It is a small wrapper that allows python to execute commands within an embedded tcl interpreter.

How does tk.call("font", "families", *args) function works?

The tk library has a command named font. From within a tcl interpreter you would call it with something like this:

font families

The code tk.call("font", "families", *args) is simply sending that command to the tcl interpreter.

The underlying tcl/tk library has platform-specific functions for dealing with fonts. See tkMacOSXFont.c, tkUnixFont.c, and tkWinFont.c.

Upvotes: 1

Related Questions