0

I get the error ModuleNotFoundError: No module named 'Tkinter' in Python 3. I am trying to run this piece of code.

from swampy.TurtleWorld import *
import Tkinter

world = TurtleWorld()
bob = Turtle()
fd(bob, 100)
lt(bob)
fd(bob, 100)
print (bob)
wait_for_user()
jdehesa
  • 58,456
  • 7
  • 77
  • 121
takin
  • 11
  • 1
  • 1

2 Answers2

1

It doesn't look like your code is using Tkinter at all, so you could just remove the line import Tkinter. In any case, you should be able to import Tkinter in Python always, because it is built into the standard library; the problem is that the module is named tkinter in lowercase, not Tkinter, so it should be:

import tkinter

But again, if you are not going to use the module it would be clearer to remove that import statement.

jdehesa
  • 58,456
  • 7
  • 77
  • 121
1

The way you are importing Tkinter uses the capitalization for Python 2. In Python 3 Tkinter has a lower case 't'. So for Python 3 you would write it as:

import tkinter

To make their programs work in both Python 2 and Python 3 I have seem many people write their code in the following manner:

try:
    import Tkinter
except:
    import tkinter

With the above you will have the correct import for whether you or not you are using Python 2 or Python 3. I'd also recommend setting up as value for tkinter such as:

import tkinter as tk

This way while you are programming instead of writing tkinter.Frame() you can shorten it to tk.Frame(). It makes it a lot quicker to code Tkinter programs.

I am assuming you are planning on implementing Tkinter later in your code as currently your code makes no use of it, so I hope this helps. If you are not going to add anything using Tkinter, I would recommend removing the import.

Skitzafreak
  • 1,797
  • 7
  • 32
  • 51