3

I receive following Traceback:

Traceback (most recent call last):
  File "tkinter_basic_frame.py", line 4, in <module>
    from Tkinter import Tk, Frame, BOTH
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 42, in 
    raise ImportError, str(msg) + ', please install the python-tk package'
ImportError: No module named _tkinter, please install the python-tk package

This is the demoscript I'm trying to run:

#!/usr/bin/python
# -*- coding: utf-8 -*-

from Tkinter import Tk, Frame, BOTH


class Example(Frame):

    def __init__(self, parent):
        Frame.__init__(self, parent, background="white")   

        self.parent = parent

        self.initUI()

    def initUI(self):

        self.parent.title("Simple")
        self.pack(fill=BOTH, expand=1)


def main():

    root = Tk()
    root.geometry("250x150+300+300")
    app = Example(root)
    root.mainloop()  


if __name__ == '__main__':
    main()  

From my knowledge Tkinter should be included in Python 2.7. Why do I receive the traceback? Doesn't ubuntu contain the standard-python-distribution?

This is solved. I had to install it manually in synaptic (got the hint in the meantime from another forum), see here:

enter image description here

Wikipedia says: "Tkinter is a Python binding to the Tk GUI toolkit. It is the standard Python interface to the Tk GUI toolkit1 and is Python's de facto standard GUI,2 and is included with the standard Windows and Mac OS X install of Python." - Not good, that it isn't included in Ubuntu as well.

Tkinter on Wikipedia

empedokles
  • 3,843
  • 15
  • 44
  • 68

2 Answers2

1

Just install the tkinter

sudo apt-get install python-tk

or if you choose python3

sudo apt-get install python3-tk

http://tkinter.unpythonic.net/wiki/How_to_install_Tkinter

Xweque
  • 1,103
  • 1
  • 9
  • 28
Lin Xu
  • 11
  • 1
0

Do what the script says:

ImportError: No module named _tkinter, please install the python-tk package

Tkinter is not part of standard python on Linux based OS'es. It's a widget extension for GUI Creation. From the Python Wiki:

Tkinter is Python's de-facto standard GUI (Graphical User Interface) package. It is a thin object-oriented layer on top of Tcl/Tk.

On top of usually denotes an extra package. Anyhow, here is a link to the python-tk package.

eyoung100
  • 610
  • 3
  • 16
  • @E Carter Young "Tkinter is a Python binding to the Tk GUI toolkit. It is the standard Python interface to the Tk GUI toolkit[1] and is Python's de facto standard GUI,[2] and is included with the standard Windows and Mac OS X install of Python." See: http://en.wikipedia.org/wiki/Tkinter – empedokles Aug 21 '14 at 21:37
  • Ubuntu is neither Windows nor OSX, therefore the need to install the package. See Bolded Text – eyoung100 Aug 21 '14 at 21:44
  • @E Carter Young Well, of course nobody forces Ubuntu to care about Python conventions and its official distributions. In the German Lema it says that these Tkinter GUIs work under Linux though.. – empedokles Aug 21 '14 at 21:48
  • Unfortunately, Internet writers have a tendency to skip over some of the nuances regarding requirements. Some distros include tkinter as a part of Python, some resolve it as a dependency, and some forget it all together. Gentoo also leaves it as an extra package. – eyoung100 Aug 21 '14 at 21:54
  • @E Carter Young: That's very unfortunate, as I can't use a GUI that works on any system easily. – empedokles Aug 21 '14 at 21:58
  • If you're building a package, include tkinter as a RUNTIME DEPENDENCY of the package. As long as you use proper dependency checking, the OS is irrelevant, ie in Windows, you include TCL/TK in your exe and in linux you let the package manager resolve it – eyoung100 Aug 21 '14 at 22:03
  • @E Carter Young I'm not as advanced yet, to build an .exe. It would have been convenient using it for just a single .py-file. – empedokles Aug 21 '14 at 22:08