-1

Looking at this link, when I install Python it says Tcl/Tk/Tkinter will also install, but when I run a game in Python, I get an import error. I have my usual import statements as well, but it's still not working. I also looked at this very similar problem, but the answer isn't working for me.

import simplegui
import random
from tkinter import *
Community
  • 1
  • 1
Ryan
  • 109
  • 3
  • 9
  • "I get an import error.". What import error exactly: please provide the proper traceback. –  May 31 '15 at 23:22
  • There are many possible reasons this could have gone wrong: because the wrong Python version was used to run the program, because Tkinter was not installed along with Python (due to the platform and due to choices made during installation), etc. There are a few specific problems, all of which are common duplicates, but there is not enough information here to determine which one applies. – Karl Knechtel Apr 28 '23 at 20:59

2 Answers2

1

Assuming you are using Python 3.x, programs must be run with python3 my-program.py (not python my-program.py).

Torsten_K
  • 19
  • 5
  • 1
    Can you rephrase this to sound less like a question? – Scratte Nov 27 '20 at 16:14
  • This should be a comment, not an answer. – ElectricShadow Nov 27 '20 at 17:24
  • 1
    Okay, Scratte, I rephrased it. @ElectricShadow: I cannot comment the thread starter’s post, because there is a hint saying *You must have 50 reputation to comment*, and my reputation is only 11, unfortunately, – Torsten_K Nov 27 '20 at 19:04
  • @Torsten_K That's just too bad then. You'll get there sooner than you think, but commenting as an answer is just going to slow your progress with downvotes. – ElectricShadow Nov 27 '20 at 21:55
  • 1
    @ElectricShadow: Thanks for your tip, I appreciate it, but nevertheless I hope that after changing my text, it might be seen as an answer, because I experienced exactly the same error message when starting my Python 3 program with *python* instead of *python3* (beginner’s fault, of cause). – Torsten_K Nov 28 '20 at 16:37
0

I am assuming you are using python2.

Try this

from Tkinter import *

You are putting wrong name for module. This will work.

EDIT AFTER COMMENT

For python3 install this python3-tk and your code will work.

shivams
  • 2,597
  • 6
  • 25
  • 47
  • @Ryan You should note for future that modules for python2 and python3 are different and most of the times modules for `python3` will have `python3` in their names. Modules for `python2` mostly come with just `python` in their names. Hope you will look for this in future. – shivams May 31 '15 at 04:22
  • Isn't python3-tk installed during the whole Python installation process? I re-installed Python and made sure everything was checked off, but it still doesn't seem to work. It does seem to be my problem though, because typing `python -m tkinter` into the command line ends in a syntax error. – Ryan May 31 '15 at 04:27
  • @Ryan yeah it will do because what you are checking into is python2 not python3. If you want to check for python3 use this `python3 -m tkinter`. This is working for me and will work for you. NOTE:- python2 and python3 are different and you should see which version you are playing with. – shivams May 31 '15 at 04:29
  • Hmmm.... `python3 -m tkinter` doesn't seem to work for me either (syntax). Clearly I missed something... I'm definitely using Python 3.4.3 though – Ryan May 31 '15 at 04:31
  • @Ryan Did you install `python3-tk` and then checked for this. Also can you give me what is the error coming. – shivams May 31 '15 at 04:32
  • @Ryan which os are you using? – Paul Rooney May 31 '15 at 04:32
  • @PaulRooney Rooney Windows 8. The error I get when I run my program is the title of this question. The error for the command line is `SytaxError: invalid syntax` – Ryan May 31 '15 at 04:34
  • [Here](http://imgur.com/6Mz1ro4) is a screenshot of the installation. Tcl/Tk is default checked off. – Ryan May 31 '15 at 04:38
  • I wonder if this question will help http://stackoverflow.com/questions/20044559/how-to-pip-or-easy-install-tkinter ? – Paul Rooney May 31 '15 at 04:41
  • @Ryan Is python directory added to your system `PATH` environment variable. This might be an issue. – shivams May 31 '15 at 04:41
  • @Ryan Since you are using python on windows. Are you able to open `idle` on your machine. Since `idle` is written using `tkinter`, if you are able to open idle then it is installed. – shivams May 31 '15 at 04:48
  • @PaulRooney The last step, when I put in `tkinter._test()` does not work @shivams I'm sorry, could you dumb that down? I don't understand what you're asking. My Python is installed to the default location in the C drive `C:\python32`. – Ryan May 31 '15 at 04:56
  • @shivams Am able to open idle – Ryan May 31 '15 at 04:58
  • @Ryan If you are able to open `idle` then `tkinter` is installed on system. Python location should be present in windows `PATH` environment variable. This tell windows where is python directory and then python will be able to run. Check this link http://superuser.com/questions/502358/easier-way-to-change-environment-variables-in-windows-8 – shivams May 31 '15 at 05:03
  • Ok. I really appreciate your help, but I'm just going to call it quits. I did navigate to the `PATH`, but I'm exhausted from this problem, and am not willing to do more searching figuring this out. For some reason I also can't open IDLE, so I gotta fix that. Thanks for the help though, learned a lot of new things. – Ryan May 31 '15 at 05:16
  • @Ryan If you are not able to open `IDLE` then tkinter is not installed. Just remove python3 from your machine and install it again and i hope this will work. Also check that `tkinter` is selected. Also your welcome. – shivams May 31 '15 at 05:20
  • Ahh, no I _was_ able to open IDLE, but then it seems I could no longer open it after I installed ActivePython. I just uninstalled ActivePython, and am trying to uninstall Python, but I get...another error. – Ryan May 31 '15 at 05:23
  • @Ryan I think best think for you is just to just uninstall everything and start from scratch. It looks really messy now. – shivams May 31 '15 at 05:30
  • @shivams Yeah, I just repaired it. I'll tackle it this problem again later. – Ryan May 31 '15 at 05:42