0

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.

Ross
  • 13
  • 1
  • 4

2 Answers2

2

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
>>>
  • Thank you for the responce, however I'm not using the PC. Everything I'm trying to do is on the PI. I'm using Python 3 on the PI please continue to help! – Ross Feb 13 '15 at 21:36
  • Thank you so much, it was a simple answer that I have been searching for and using too much of my brain power thinking about. I can sleep easy tonight. Next I need to figure how to get crontab to run it so I can see the windows. – Ross Feb 13 '15 at 22:12
  • Notice that if you are using crontab, you need to explicitly set the DISPLAY=:0 environment variable so that it knows on what display to run... and if you think I solved your problem then please tick my answer as the accepted one. – Antti Haapala -- Слава Україні Feb 13 '15 at 22:13
  • Thank you crontab for another day now. Yours solution has worked perfectly, I tested it already. Thank you so much . . . – Ross Feb 13 '15 at 22:19
0

Run the script with:

python3 main.py
Malik Brahimi
  • 16,341
  • 7
  • 39
  • 70