Please help me understand Tk / tkinter

Hello all,

I’m writing a small program and I’ve been looking for an event driven programming framework to give it a GUI. I’ve played around with pyglet a bit in the past but since I’m looking to create a GUI that is controlled via widgets, I don’t think pyglet is the best fit.

Naturally I’ve been considering using the tkinter module, but to be honest I’m struggling to work out what Tk actually is. Like many such things any description of it is full of jargon and doesn’t really answer the simple question.

Am I right in thinking that Tk is just a library / libraries containing code that can obtain a window, render widgets to that window, monitor events related to the window and call routines defined in the user’s application in response to those events?

tkinter I understand is just a Python package that contains modules providing Python language bindings for the Tk framework.

It’s frustrating because the Tcl/Tk official site describes Tk as…

a free and open-source, cross-platform widget toolkit that provides a library of basic elements of GUI widgets for building a graphical user interface in many programming languages

This is confusing for a few reasons; it says Tk provides “a library of basic elements of GUI widgets”, are the GUI widgets not the basic elements themselves? Either this is poor English or I’m not a programmer (at least one of these things is definitely true).

Secondly, if Tk does contain code that can obtain a window from the OS and render to it, plus code that constitutes an event monitor, it does more than just provide widgets. A directory of .jpeg files could be considered a library of widgets.

And finally, apart from being developed in tandem by the same core team, what does Tk have to do with Tcl in the context of Python? The Git Hub page for Tk shows it’s approximately 10% Tcl and roughly 85% C; does that mean it’s written in Tcl but leans heavily on a library of C functions?

I appreciate one doesn’t need to understand all this just to use tkinter but I’m finding it a bit confusing! Any info would be much appreciated :slightly_smiling_face:

Tcl is a string and command based language with an interpreter written in C. Commands can have subcommands and even subsubcommands. Tk is a GUI extension package mostly written in C with tcl used as needed. The widget commands (about 2/3rds of them) become Python classes and subcommands become methods. A widget in an interactive structure represented on the screen by a rectangular array of pixels.

Forget ‘basic elements’ and look at the linked tk doc. A few parts of tkinter, such as after and update methods, come from tcl commands. In same cases, the tcl or tk docs are the best tkinter doc available if one can translate to python and tkinter.

Does this help? I’m the main IDLE maintainer and this is about as much as I know.

Thanks, yes that is helpful. If I’m right in my thinking, from the style of the IDLE GUI it uses Tk? In which case I suppose that you yourself are proof that tkinter can be used effectively, without the need for an extensive knowledge of the underlying software. Nevertheless I did find a couple of quotes from the tkinter python documentation that are quite explanatory:

Tk is a Tcl package implemented in C that adds custom commands to create and manipulate GUI widgets [ … ] when your Python application uses a class in Tkinter, e.g., to create a widget, the tkinter module first assembles a Tcl/Tk command string. It passes that Tcl command string to an internal _tkinter binary module, which then calls the Tcl interpreter to evaluate it. The Tcl interpreter will then call into the Tk and/or Ttk packages, which will in turn make calls to Xlib, Cocoa, or GDI.

Of course what you’d already said agrees with that description :slightly_smiling_face:

As you said, you wanted more than needed to use tkinter. I gave you my summary of that ‘more’. I use this plus the tk command reference above. IDLE’s Shell makes interactive exploration of a gui design easy by running update in the background. I otherwise learned by working on IDLE.