Next / Previous / Contents

28.1. Importing ttk

There are different ways to import the ttk module.

  • If you prefer that all the widgets and other features of Tkinter and ttk be in your global namespace, use this form of import:

    from Tkinter import *
    from ttk import *

    It is important to do these two imports in this order, so that all the widget types from ttk replace the equivalent widgets from Tkinter. For example, all your Button widgets will come from ttk and not Tkinter.

  • In more complex applications, where you are using more than one imported module, it can greatly improve the readability of your code if you practice safe namespace hygiene: import all your modules using the “import modulename” syntax. This requires just a bit more typing, but it has the great advantage that you can look at a reference to something and tell where it came from.

    We recommend this form of import:

    import ttk

    So after this import, ttk.Label is the Label widget constructor, ttk.Button is a Button, and so on.

    If you need to refer to items from the Tkinter module, it is available as ttk.Tkinter. For example, the anchor code for “northeast” is ttk.Tkinter.NE.

    You may instead import Tkinter separately in this way:

    import Tkinter as tk

    After this form of import, the code for “northeast” is tk.NE.