简体   繁体   中英

Python Tkinter - .py file doesn't work when double clicked

I am running Windows 7 and have Python 3.3 64 bit installed. I seem to have a problem importing the tkinter module, I can import it fine through the python IDLE and it will work, but when I save the .py file and double click it, a cmd window will open and say:

Traceback (most recent call last):
File "C:Users\username\Desktop\g.py", line 3, in <module>
from tkinter import *
ImportError: No module named tkinter

I have tried the following:

  1. I have tried import tkinter , from tkinter import * , and import tkinter as tk and they don't seem to work when the .py file is opened directly (double clicked).

  2. I also double checked the path variable and it was set correctly.

  3. I uninstalled python and reinstalled it.

  4. I checked to see if tkinter is in the folder C:\\Python33\\Lib\\, and it is.

  5. I do have a mainloop() in my program.

  6. In my program, tkinter is all lowercase.

  7. I tried a lot of solutions online from other posts, and they didn't work for me.

The top of my code is:

import sys

from tkinter import *

I don't know what I am missing, any suggestions?

I'm going to make this an answer then for anybody in the future.

The problem is that Windows is currently set to run all .py files with a different executable (probably a Python 2.x one) To fix the problem, follow these steps:

  1. Right-click a .py file.

  2. In the menu that pops up, go to Open with .

  3. In the submenu that pops up, click on Choose default program...

  4. A window will then appear. In this window, click on the Browse... button.

  5. Then, go find the Python execuatble. It should be at C:\\Python33\\python3.3.exe . (There might be multiple pythonX.exe files. If one doesn't work, try another.)

  6. Once you select it, click Open .

If done correctly, this procedure will manually reset the default executable for .py files to be the Python 3.x one. Meaning, your script should run fine now.

This is actually a simple solution. You have:

    from tkinter import *

You need:

    from Tkinter import *

Capitalization is very specific!!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM