0

Good day. I installed python 2 and python 3 in my laptop. And i'm using python 3 interpreter in writing my codes. Here is my code.

#! /usr/bin/python3

from tkinter import *

root = Tk()

theLabel = Label(root, text ="This is too easy")
theLabel.pack()

root.mainloop()

But when I double clicked the save file icon. It will say no module name tkinter. Can some one help me please?

Ismael Padilla
  • 5,246
  • 4
  • 23
  • 35
John Cruz
  • 127
  • 8
  • what do you mean "save file icon"? If you run this code, you should not be getting errors if python is installed correctly. Also, which platform are you using? please edit the question – Andrew Dec 01 '15 at 07:14
  • What exactly did you do when you tried to install tkinter for python3? – decltype_auto Dec 01 '15 at 07:34
  • @Andrew the icon of the program I save. – John Cruz Dec 01 '15 at 11:22
  • @Andrew - the icon of the saved program. When I save the program. I double click the icon of my program. Sorry for my english – John Cruz Dec 01 '15 at 11:38
  • @decltype_auto I didn't do anything. I just install python 3 normally. I didn't install any tkinter. – John Cruz Dec 01 '15 at 11:39
  • 1
    Possible duplicate of [Which tkinter modules were renamed in Python 3?](https://stackoverflow.com/questions/673174/which-tkinter-modules-were-renamed-in-python-3) – Stevoisiak Mar 30 '18 at 19:30

2 Answers2

4

python 2 and python 3 use tkinter in a different way.

Note: Tkinter has been renamed to tkinter in Python 3. The 2to3 tool will automatically adapt imports when converting your sources to Python 3.

The above lines are from python documentation. Not sure if python is loading tkinter using python 2 or python 3..May be internal PYTHONPATH is messed up Rather try this,

try:
  import tkinter as tk
except ImportError:
  import Tkinter as tk

Note: In these situations where you use multiple versions of same modules, try using virualenv

Virtual Env

Mayukh Sarkar
  • 2,289
  • 1
  • 14
  • 38
  • I thought adding a shebang line would tell my computer that use the python version 3. Sorry for my english. – John Cruz Dec 01 '15 at 11:48
  • No problem..Please if the answer helps, then select is as the answer. – Mayukh Sarkar Dec 01 '15 at 11:51
  • But how can I fix it without the try and except? This problem didn't show up before I reformat my laptop and reinstalled python 2 and python 3. – John Cruz Dec 01 '15 at 11:59
  • You yourself answered your question. You should not have installed `Python 2` and `Python 3` on the same PC. You can keep any number of `Python` version you want but you have to put then in some `virtual` environment...go to the website link in my answer. Most professionals use `virtualenv` – Mayukh Sarkar Dec 01 '15 at 12:04
  • Are you using Linux? Because if you are, tkinter doesn't come with Python 3. – kettle Sep 13 '20 at 00:02
2

you need to check the module name or package name before using it, do this

from Tkinter import *
sameera sy
  • 1,708
  • 13
  • 19