pythonpython-3.xtkinterterminalmplayer

tkinter not importable from terminal but works in Python 3 shell on Raspberry Pi


Thank you for your help in advance I've been stuck for nearly a week now!

I am trying to use tkinter on my Pi and the following works ok when run in the python application (shell):

import tkinter
tk = tkinter.Tk()
window = tkinter.Tk()
tk.mainloop

However when I enter the command:

python /home/pi/myfiles/windowtest.py

in the terminal, I get

ImportError: No module named tkinter

So far I have tried sudo apt-get install tkinter, same with updates, dev, tk, -f, python-tkinter and any others I could think of. In fact sudo apt-get install tkinter is unable to locate the package and the same with python-tkinter.

sudo apt-get install python-tk tells me is already the newest version installed.

Im using the latest NOOBS installed it about 10 days ago.

Background - I want to run a bit of python script started by crontab that uses mplayer, at the moment all the mplay blurb pushes my menu off the screen in terminal mode and I can't see it. I was hoping tkinter would open a window in which I can put my menu into and see it.


Solution

  • The problem is that the module is named Tkinter in Python 2 and tkinter in Python 3. In Debian, Raspbian, Ubuntu and so forth, python on command line starts Python 2.x (usually 2.7 by now) whereas python3 is needed to run the Python 3.x interpreter.

    As this is a new project I guess you should only use Python 3 (and you have written your code for Python 3), thus run the command with python3:

    python3 /home/pi/myfiles/windowtest.py
    

    % python
    Python 2.7.6 (default, Mar 22 2014, 22:59:56) 
    [GCC 4.8.2] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import tkinter
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ImportError: No module named tkinter
    >>> import Tkinter
    >>> 
    

    vs

    % python3
    Python 3.4.0 (default, Apr 11 2014, 13:05:11) 
    [GCC 4.8.2] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import tkinter
    >>>