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

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:
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).
I also double checked the path variable and it was set correctly.
I uninstalled python and reinstalled it.
I checked to see if tkinter is in the folder C:\Python33\Lib\, and it is.
I do have a mainloop() in my program.
In my program, tkinter is all lowercase.
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:
Right-click a .py file.
In the menu that pops up, go to Open with.
In the submenu that pops up, click on Choose default program...
A window will then appear. In this window, click on the Browse... button.
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.)
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!!!

Related

Tkinter GUI, add large .py file for a user. What if user does not have python?

I worked at a GUI and made it into a executable.
But when I run the executable which should do:
os.system('python Test.py x)
It can't do this because the user doesn't have python installed and the command prompt doesn't understand the line: python Test.py
The way I might fix it is to make one big function in tkinter which has the entire Test.py code in it. This will costs me a lot of work to put all this code within a function / tkinter button.
Is there a simple way to do this? (I have over 15 functions and 3000+ lines of code)

py2exe - single file exe-tkinter program not executing

I used below code to make execute of my program:
from distutils. core import *
import py2exe,sys,os
sys.argv.append('py2exe')
setup(options={'py2exe':{bundle_files':1,'compressed':True,'includes':["Tkinter",]}}'windows=['trial.py'],zipfile=None,)
It is creating single file .exe, but not executing.
same code with bundle_file 3 (without single file) is working fine.
trial.py:
import Tkinter
Tkinter.Tk()
mainloop()
Please help me to create a single .exe file for GUI program
PyInstaller is a much easier option that can be used from the console. Just pip install pyinstaller, enter your program's directory, and pyinstaller yourscript.py.
I am confused that you say trial.py is working fine when I get an error on my machine. mainloop() needs to be replaced by Tkinter.mainloop().

Running Python file from command line does not load modules

I have a Python script that contains the following modules:
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
When I run the code in IDLE by pressing F5 the script runs fine and starts my app.
However, when I go to the command prompt and type
python ScannerApp.py
I get the following error:
File "tkinterTest.py", line 1, in <module>
from tkinter import *
ImportError: No module named tkinter
How do I get rid of this error? The ultimate goal being to make this script into a .exe.
One thought is that python is not added to my environmental variables under Path, it is added as it's own variable. Could that be causing the issue?
My question does not pertain to the difference between Tkinter and tkinter. My question was about why when I ran code through the command line I was getting an error. The issue happened to be that my environmental variable python was set to run python 2.7 instead of the necessary python 3.6 (which uses tkinter).
Try adding this for cross compatibility instead of your previous import code.: (Hoping that's the problem)
try:
from tkinter import *
from tkinter import ttk,filedialog
except:
from Tkinter import *
from Tkinter import ttk,filedialog
The solution to my problem was to change the environmental variable python to run version 3.6 instead of 2.7.
The issue was a cross compatibility issue and I found it easier to change the variable instead of trying to have it try both Tkinter and tkinter modules depending on the particular version.
Your issue might be that python3 doesn't use Tkinter (with a capital T) but tkinter. That is if you're using pyhton3 of course ^^
https://stackoverflow.com/a/17843652/9368855

askopenfilename doesn't work for files in library folder

I'm trying to make a UI that requires selecting pictures, so I decided to use askopenfilename to get the path, but when I run this code.
from tkFileDialog import askopenfilenames
files = askopenfilenames()
print "Files:",files
and go to my "Pictures" library it prints out (' ',)
However if I go to the pictures folder through the "This PC" button then it prints out a proper file path.
A search found this bug report but it seems to be closed and old yet still not fixed. Does anyone know a solution or if there's any intent to fix this? I'm running Python 2.7 and Windows 10.

Pygame won't import to python

When i type import pygame, i get an error that reads:
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import pygame
File "C:\Python27\lib\site-packages\pygame\__init__.py", line 95, in <module>
from pygame.base import *
ImportError: DLL load failed: The specified module could not be found.
>>>
Does anybody know what this means?
It means that you need to look at pygame\base.pyd in Dependency Walker and see which DLL it can't find.
I made this mistake too. The problem is that you are using the Idle Shell to attempt to build a Python program. Here is how I get out of the shell:
Save the Idle Shell as a .py in a location of your choice
Afterward, find the .py you created.
Left click, then Right click to display the context submenu (the menu with copy/paste)
Select "Edit with Idle"
This will open the .py file in the Editor Window. Delete all extra content to create a blank screen
Now you can import your modules and begin programming:
import os, sys
import pygame
from pygame.locals import *
[...]
This will give you the head start to work with pygame.
This question at Game Developement (another Stack Exchange Site) should answer your question:
Installing the right version of PyGame
This user can't make Python to know that PyGame is there so he asks that question. His question should be similar to yours, making it convinient for you. I hope this helps you!
In the command prompt, write pip install pygame if you have a Windows PC, or pip3 install pygame if you have Linux or MacOS.

Categories

Resources