[Tkinter-discuss] LabelFrame question: why do options change when importing from tkFileDialog?

Bryan Oakley bryan.oakley at gmail.com
Tue Feb 28 21:47:04 CET 2012


On Tue, Feb 28, 2012 at 1:22 PM, Lynn Oliver <raycores at gmail.com> wrote:
> I've discovered that 'bd=4' and 'font=...' both work if I import:
> from tkFileDialog import askopenfilename, asksaveasfilename, askdirectory
> from Tkinter import *
> import tkFont
> from ttk import *
>
> And they both fail if I import only:
> from Tkinter import *
> import tkFont
> from ttk import *
>
> Can anyone explain what is going on here?

Both Tkinter and ttk define objects with the same name, such as Button
and Label. These two widgets don't share the same set of options. When
you do "import *", whichever one you import second "wins". Thus, in
one file you might be getting a tk button with one set of options, and
in another you get a ttk button with a different set, even though in
both files you use "Button".

IMHO this is a perfect example why you should _never_ "import *".
Instead, I wholeheartedly recommend always working like this:

    import Tkinter as tk
    import ttk
    ...
    tk.Button(...)
    ttk.Button(...)

With that, it becomes completely obvious which sort of button or label
you are creating. Even if you don't mix tk and ttk widgets I think you
should import this way. It makes the code more self-documenting at the
expense of a tiny bit more typing.


More information about the Tkinter-discuss mailing list