tm48
Posts: 19
Joined: Mon May 26, 2014 12:07 pm

No Module Named Tkinter

Wed Jul 23, 2014 12:26 pm

Hi,
I am new to the raspberry Pi and Python but I have exprience programming. I am getting errors running the following program:

Code: Select all

from tkinter import * 
root = tk.Tk()
canvas = tkinter.Canvas(root)
canvas.grid(row = 0, column = 0)
photo = tkinter.PhotoImage(file = './blah.jpg')
canvas.create_image(0,0, image=photo)
root.mainloop()
But I get the following error:
Traceback (most recent call last):
File "/home/pi/tkinterpy1.py", line 1, in <module>
from tkinter import *
ImportError: No module named tkinter
How can I fix this? Is it possible to delete and re-install tkinter on the pi? I am using Debian.

User avatar
AndyD
Posts: 2334
Joined: Sat Jan 21, 2012 8:13 am
Location: Melbourne, Australia
Contact: Website

Re: No Module Named Tkinter

Wed Jul 23, 2014 12:37 pm

You need to install python-tk

Code: Select all

sudo apt-get install python-tk

gkreidl
Posts: 6279
Joined: Thu Jan 26, 2012 1:07 pm
Location: Germany

Re: No Module Named Tkinter

Wed Jul 23, 2014 4:53 pm

import Tkinter (capital T) in Python 2.7 ..., tkinter in Python3
Which version of Python are you using?
It should be installed by default.
Minimal Kiosk Browser (kweb)
Slim, fast webkit browser with support for audio+video+playlists+youtube+pdf+download
Optional fullscreen kiosk mode and command interface for embedded applications
Includes omxplayerGUI, an X front end for omxplayer

User avatar
AndyD
Posts: 2334
Joined: Sat Jan 21, 2012 8:13 am
Location: Melbourne, Australia
Contact: Website

Re: No Module Named Tkinter

Wed Jul 23, 2014 10:43 pm

gkreidl wrote:It should be installed by default.
Are yes ... sorry my mistake!

tm48
Posts: 19
Joined: Mon May 26, 2014 12:07 pm

Re: No Module Named Tkinter

Thu Jul 31, 2014 8:19 am

gkreidl wrote: Which version of Python are you using?
I am using Python 3.2.3. So, I just ran the same program I had before and got a different error this time:
Traceback (most recent call last):
File "/home/pi/tkinterpy1.py", line 3, in <module>
canvas = tkinter.Canvas(root)
NameError: name 'tkinter' is not defined

gkreidl
Posts: 6279
Joined: Thu Jan 26, 2012 1:07 pm
Location: Germany

Re: No Module Named Tkinter

Thu Jul 31, 2014 10:13 am

replace
from tkinter import *
to
import tkinter

Then your code should work.
Minimal Kiosk Browser (kweb)
Slim, fast webkit browser with support for audio+video+playlists+youtube+pdf+download
Optional fullscreen kiosk mode and command interface for embedded applications
Includes omxplayerGUI, an X front end for omxplayer

tm48
Posts: 19
Joined: Mon May 26, 2014 12:07 pm

Re: No Module Named Tkinter

Thu Jul 31, 2014 10:36 am

gkreidl wrote:replace
from tkinter import *
to
import tkinter

Then your code should work.
I replaced it. But now I am getting the following error:
Traceback (most recent call last):
File "/home/pi/tkinterpy1.py", line 2, in <module>
root = Tk()
NameError: name 'Tk' is not defined

gkreidl
Posts: 6279
Joined: Thu Jan 26, 2012 1:07 pm
Location: Germany

Re: No Module Named Tkinter

Thu Jul 31, 2014 10:43 am

root = tkinter.Tk()

using "from module import *"
imports everything into the local name space. You have to decide what you are doing. Mixing it will not work.
Minimal Kiosk Browser (kweb)
Slim, fast webkit browser with support for audio+video+playlists+youtube+pdf+download
Optional fullscreen kiosk mode and command interface for embedded applications
Includes omxplayerGUI, an X front end for omxplayer

tm48
Posts: 19
Joined: Mon May 26, 2014 12:07 pm

Re: No Module Named Tkinter

Thu Jul 31, 2014 12:52 pm

Code: Select all

photo = tkinter.PhotoImage(file = '/home/pi/blah.jpg') 
This line of code gave me the following error:
Traceback (most recent call last):
File "/home/pi/tkinterpy1.py", line 5, in <module>
photo = tkinter.PhotoImage(file = '/home/pi/blah.jpg')
File "/usr/lib/python3.2/tkinter/__init__.py", line 3255, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "/usr/lib/python3.2/tkinter/__init__.py", line 3211, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't recognize data in image file "/home/pi/blah.jpg"
Does this not read JPGs? Should I save my image as a PNG or another file type instead?

DirkS
Posts: 10307
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: No Module Named Tkinter

Thu Jul 31, 2014 2:00 pm

According to the docs https://docs.python.org/3.0/library/tki ... photoimage:
PhotoImage can be used for GIF and PPM/PGM color bitmaps.

Return to “Beginners”