-3

I'm trying to understand some aspects of the Python TKinter package.

The package root cpython/Lib/tkinter is found on GitHub. I'm looking at the file cpython/Lib/tkinter/ttk.py. At line 512, this file defines a Widget class:

28    import tkinter
...
512   class Widget(tkinter.Widget):
      """Base class for Tk themed widgets."""

Figuring out what this is doing requires identifying this imported tkinter package. According to the Python docs, a directory containing a file __init__.py constitutes a regular package. Since [cpython/Lib/tkinter/__init__.py] exists, the directory cpython/Lib/tkinter/ is a regular package. My understanding is that when interpreting import tkinter, the current directory is one of the first places Python will look for a package (though that understanding is difficult to verify from the "Searching" portion of the docs). If true, then what's imported by the import tkinter line of cpython/Lib/tkinter/ttk.py is the folder cpython/Lib/tkinter/ itself.

Since the file cpython/Lib/tkinter/ttk.py is the only place where a Widget class is defined in this directory (at least as far as I can tell from the GitHub search function), then it appears that the code in cpython/Lib/tkinter/ttk.py

28    import tkinter
...
512   class Widget(tkinter.Widget):
      """Base class for Tk themed widgets."""

defines a class that extends itself.

Surely there's something I don't understand. What is going on here?

3
  • ttk.Widget extends tkinter.Widget and you can also create own class Widget which can extend tkinter.Widget (class Widget(tkinter.Widget)) or ttk.Widget (class Widget(ttk.Widget)) or not use any of them (class Widget()) Commented Sep 17 at 21:59
  • Hello Darien, let's start with the basics: it is very bad practice for the Widget class to be extended by a class called Widget, Although it may work correctly, it makes the code difficult to read and understand, and is a potential source of errors. For example, using the original class by invoking the methods of the class that extends it (which it does not have). Apart from this, there is nothing unusual about extending a class to add functionality. Commented Sep 18 at 12:56
  • 1
    @MarcePuente OP did not write this class - this is from the built-in ttk module. That said, you're not technically incorrect, but this is part of the standard library. Commented Sep 18 at 13:04

2 Answers 2

4

The base Widget class* tkinter.Widget is defined in tkinter's __init__.py file (line 2646) as follows:

class Widget(BaseWidget, Pack, Place, Grid):
    """Internal class.

    Base class for a widget which can be positioned with the geometry managers
    Pack, Place or Grid."""
    pass

ttk's Widget class is inheriting from (i.e. "extending") that class (ttk.py line 503):

class Widget(tkinter.Widget):  # <-- from "import tkinter"
    """Base class for Tk themed widgets."""

    def __init__(self, master, widgetname, kw=None):
        ...  # docstring removed for brevity
        master = setup_master(master)
        tkinter.Widget.__init__(self, master, widgetname, kw=kw)

ttk (or "Themed Tk") is built on top of tkinter, which you can infer from the fact that you import it a la: from tkinter import ttk - ttk exists to extend the functionality (and particularly the style) of tkinter widgets.

So even though these two classes share a name, they are not the same - one inherits from the other!


*not to be confused with tkinter's internal BaseWidget class...words are hard

Sign up to request clarification or add additional context in comments.

Comments

4

short answer: ttk.Widget is not the same as tkinter.Widget.

ttk is a widget library that builds on top of the tkinter widgets. ttk's generic Widget adds the ttk-specific functionality to tkinter's generic Widget; ttk's Widget is then the base for all of ttk's specific widgets, just like tkinter's Widget is the base for all the tkinter's specific widgets.

And to answer the actual question that I just realized is in the post :)

tkinter's Widget class is defined in tkinter/__init__.py.

/c/Program Files/Python312/Lib/tkinter/__init__.py:class Widget(BaseWidget, Pack, Place, Grid):

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.