4

What exactly is the difference between starting the program with

from tkinter import *

and

from tkinter import tix as tk

Because with the latter one I'm having problems, hence the question. I've changed all tkinter elements from plain 'Button' to 'tk.Button', but that doesn't seem to be the only difference that it makes.

Particularly I have a problem in the following code bit:

class OptionMenus(OptionMenu):
    def __init__(self, master, status, *fonts):
        self.var = StringVar(master)
        self.var.set(status)
        OptionMenu.__init__(self, master, self.var, *fonts,
                            command = update_config)
        self.config(width = "9", height = "1")

or

class OptionMenus(tk.OptionMenu):
    def __init__(self, master, status, *fonts):
        self.var = tk.StringVar(master)
        (self.var).set(status)
        (tk.OptionMenu).__init__(self, master, self.var, *fonts,
                                 command = update_config)
        self.config(width = "9", height = "1")

(and just in case how I call it)

fonts_menu = OptionMenus(buttons, strings[17], *fonts)

The latter one produces an error: "TypeError: init() takes from 2 to 3 positional arguments but 4 were given." The first one works just fine.

makaveli
  • 69
  • 1
  • 9
  • 1
    It's probably worth pointing out that `tkinter.OptionMenu` and `tkinter.tix.OptionMenu` are completely different classes, even though they have the same name. For example, their initialization signatures are different: `(self, master, variable, value, *values, **kwargs)` and `(self, master, cnf={}, **kw)`, respectively. – Kevin Apr 08 '15 at 14:23
  • Thank you very much, I'll look into that right away. Does the tix one has more functions and options or what? Why has it been doubled? – makaveli Apr 08 '15 at 14:35

1 Answers1

6

tix and tk are two separate modules. They have some widgets that have similar names, but they are not interchangeable libraries.

If you want to use tix, you should import it separately from tkinter, and you should not do global imports because it leads to ambiguity, which is likely why you're having problems.

Import them like this:

import tkinter as tk
from tkinter import tix

Then, use the widgets like this:

tk.Button(...)
tix.OptionMenu(...)

It then becomes crystal clear which widgets come from which libraries.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • I tried importing both of them separately, but then my tkinter elements stopped working properly. How should I import them both, so that both are separately usable? – makaveli Apr 08 '15 at 14:53
  • @wanaryytel: that all depends on what you mean by "stopped working properly". As for how to import them, you should _not_ do global imports. Import them as `import tkinter` (or `import tkinter as tk`) and `import tix`. Do not do `import tix as tk` because that would be extremely confusing. – Bryan Oakley Apr 08 '15 at 14:55
  • Thanks, I'll keep that in mind. Now that I try using `import tkinter`, all the tkinter elements start giving me errors in the following manner: NameError: name 'OptionMenu' is not defined. When I give it a try with `import tkinter as tk`, then it appears to be working (at least no errors when I tested with the first couple of elements). Any ideas as to why doesn't `import tkinter` work as it's supposed to? – makaveli Apr 08 '15 at 15:03
  • I don't have a module named tix, I can only import tix from tkinter. – makaveli Apr 08 '15 at 15:06
  • @wanaryytel: `import tkinter` _does_ work as it's supposed to. I think you misunderstand how importing works. When you do `import tkinter`, it imports _one thing_ - a module named `tkinter`. You can then access all the widgets by prefixing them with the module name (`tkinter.Button`, `tkinter.Label`, etc). This is how all python packages work. When you do `from tkinter import *` you don't have to use the prefix. This method is not recommended; see https://www.python.org/dev/peps/pep-0008/#imports – Bryan Oakley Apr 08 '15 at 15:41
  • I solved my problems using what Bryan suggested. To inform all the tix+tkinter rookies (like me) - tix widgets don't work with tkinter frame (main thingie, `tk`). However, tkinter widgets work just fine with a tix frame. ;) – makaveli Apr 15 '15 at 16:57
  • @wanaryytel: Your comment doesn't make much sense. Make sure you use the right terminology -- I think you are referring to modules, not frames. Frames are widgets, and tix widgets work perfectly well in tkinter frames. – Bryan Oakley Apr 15 '15 at 17:05
  • Isn't `tix` deprecated? I mean, there aren't much references and documentation online. – Ethan Chan Sep 10 '22 at 00:13