0

How can I find out the file path and correct script containing a particular function or class of a module?


Example: Say I want to take a look at the tkinter source code for tkinter.Button().

I found where the __init__.py file is located for tkinter using:

>>>import tkinter
>>>tkinter.__file__
'C:\\Users\\Simon\\AppData\\Local\\Programs\\Python\\Python36-32\\lib\\tkinter\\__init__.py'

I searched through that folder but was unable to locate any function or class named Button. My guess that it is contained inside the run-times and not a .py file but I am not definite and want to be sure of this.

I could not find anything useful in the Python documentation special attributes either.

Any suggestions are welcome but please note that this post is not set only on the tkinter module but most packages or modules, tkinter just being an example.

Xantium
  • 11,201
  • 10
  • 62
  • 89
  • Not all classes in Python are implemented in as Python code, some things are implemented in natively compiled extensions. `tkinter` objects are exposed in Python from a C library called `Tk`. – Martijn Pieters Feb 24 '18 at 19:14
  • @MartijnPieters Thank you. So I would need to look at the C source and not the Python code for the `Button()` attribute? – Xantium Feb 24 '18 at 19:15

2 Answers2

3

The tkinter objects are not implemented as Python source code. See the tkinter documentation:

The Tk interface is located in a binary module named _tkinter. This module contains the low-level interface to Tk, and should never be used directly by application programmers. It is usually a shared library (or DLL), but might in some cases be statically linked with the Python interpreter.

See the Tcl/Tk documentation the tkinter library documentation links to. The Tcl/Tk documentation in turn links to https://core.tcl.tk/, the central hub for development of that project (which is external to Python).

Following links from there you can find the GitHub mirror of the project source code, which includes the Tk source, where you can find the tkButton.c source file; this is the core button widget implementation.

There is some Python code involved as well, but the class definitions there are mostly there to just pass on commands to the native Tk library.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • So if I want to look at the `Button()` class I need to go lower level. Thank you very much for your detailed post. – Xantium Feb 24 '18 at 19:40
1

Any suggestions are welcome but please note that this post is not set only on the >tkinter module but most packages or modules, tkinter just being an example.

This pretty much answers your question (for general cases) How do I get the filepath for a class in Python?

One can also see that Class Button(Widget) is defined in the init, but this is a wrapper as @Martijn Pieters wrote.

ec2604
  • 501
  • 3
  • 11
  • Thank you for your answer, it was helpful to me so I have up-voted it and since it does answer my second question, but have accepted that of @Martijn Pieters because it describes why it is not possible with `tkinter` and where `Button()` is actually located. Thank you once again. – Xantium Feb 24 '18 at 19:37