how can I see the Tkinter library code in python

0
  

I would like to know how the interfaces are programmed (in raw) in python, in this case how is Tkinter built

    
asked by djarte 11.08.2018 в 05:52
source

1 answer

2

Since version 3.5, the online python documentation includes links to the sources of each module.

Thus, if you look at the tkinter documentation you will see at the beginning of it a link that says: Source code: Lib/tkinter/__init__.py and it takes you to the repository on GitHub where the code of that module.

However, in the particular case of deciphering the tkinter code, you face a titanic task , and it is very doubtful that you will find what you are looking for, whatever it is (maybe you could clarify it in the question). The reasons why tkinter is especially complex are:

  • The python tkinter module is actually what is called a wrapper , that is, the real functionality that accesses the windows system is not written in python, but python does just "wrapper", providing an object-oriented syntax and python, on the library that really does the job of creating the windows system and interacting with the user.
  • You'll see that the first thing that tkinter does is a import _tkinter . This second module is the one that does the real work.
  • The _tkinter module is written in C. The source code is part of the Python interpreter, and you can see it in Modules / _tkinter.c
  • The thing does not end here, because that C module is itself a wrapper to provide an interface with Python from the tcl / tk library, which Python depends on.
  • The Tcl / Tk library is a very old thing, prior to Python. Based on the language Tcl , which was a scripting language, a bit like python but less friendly in my opinion, and that It had its moment of success in the 90's by providing the Tk library that allowed creating graphical user interfaces without having to program in C (which was the only way to do it then), but using Tcl. Due to the success of Tk, Python decided to include it as a mechanism so that, too, it could offer a simple (or easier than C) method of creating graphical user interfaces.

In short, Tk is a thing inherited from the 90s. And its architecture is, for historical reasons, too complex. Recapitulating, but the other way around (from bottom to top):

  • Using C, Tcl was written as a scripting language.
  • Using Tcl (and parts in C), Tk was written as a library to create GUIs with Tcl.
  • Using C, the _tkinter module is created that allows Python to communicate with Tcl and with Tk
  • Using Python, you create the tkinter module that imports _tkinter and gives you an object-oriented interface.
  • Using Python, your application imports tkinter and creates the user interface.

So when you say "how are raw interfaces programmed" at what level do you mean? If you look at the source of tkinter you do not get to see anything "raw" yet. I suppose you should go all the way down, to the implementation of Tk in TCL or to the own implementation in C of TCL. And even then, you would reach a point where it delegates the creation of windows and user events in calls to the operating system itself (which will be different in Windows than in Unix, where X-Window is used).

To the complexity of all this, the inherent complexity of the programming of GUIs is added, be it in the language that is, since it is an event-oriented programming in which things do not happen in the order that dictates your program, but in the order dictated by user events (mouse movements, keystrokes ...)

Good luck! : -)

    
answered by 11.08.2018 / 11:23
source