pythonpython-3.xtkintercomboboxtix

tix ComboBox issue with Python 3


Using the following Python 3 code :

import tkinter
from tkinter import tix

class App(tkinter.Tk):
    def __init__(self):
        tkinter.Tk.__init__(self)
        cmbbx = tix.ComboBox()
        cmbbx.pack()
        self.mainloop()

if __name__ == "__main__":
    application = App()

I'm trying to create a simple window containing an empty combobox. When I run the program with the python3 command, I get the following error message :

Traceback (most recent call last):
  File "combo.py", line 12, in <module>
    application = App()
  File "combo.py", line 7, in __init__
    cmbbx = tix.ComboBox()
  File "/usr/lib/python3.5/tkinter/tix.py", line 583, in __init__
    cnf, kw)
  File "/usr/lib/python3.5/tkinter/tix.py", line 315, in __init__
    self.tk.call(widgetName, self._w, *extra)
_tkinter.TclError: invalid command name "tixComboBox"

After some searches, I find that I need to replace each tkinter with tix in my code to make it work, but this gives me another error message :

Traceback (most recent call last):
  File "combo.py", line 12, in <module>
    application = App()
  File "combo.py", line 6, in __init__
    tix.Tk.__init__(self)
  File "/usr/lib/python3.5/tkinter/tix.py", line 218, in __init__
    self.tk.eval('package require Tix')
_tkinter.TclError: can't find package Tix

Another link showed that I must use root.tk.eval('package require Tix'), but it doesn't seem to work.

I'm a bit lost between all those tkinter and tix, and I think that the previous solutions only work with Python 2, as it talks about Tix and not tix.

Do you have any idea to make it work ?


Solution

  • This page states that tix is deprecated in Python 3 and that ttk must be used. My new code is :

    import tkinter
    from tkinter import ttk
    
    class App(tkinter.Tk):
        def __init__(self):
            tkinter.Tk.__init__(self)
            cmbbx = tkinter.ttk.Combobox()
            cmbbx.pack()
            self.mainloop()
    
    if __name__ == "__main__":
        application = App()