pythontkintertix

Difference between working with tkinter and tix (as tkinter?)


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.


Solution

  • 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.