Python program using tkinter closes immediately in Windows 10 - python

Code in question:
from tkinter import *
root = Tk()
test_label = Label(root, text = "Hello")
test_label.pack()
root.mainloop()
I can only run this python code from the IDLE, if I run it any other way the window flashes on the screen and closes immediately. I've tried:
-adding an "input" line to keep it from closing
-running from the windows powershell
-compiling the code into an EXE with pyinstaller
and now I can't find any other suggestions. I tried making a simple 1-line program that just asks for input, and that works normally and stays open fine. The tkinter program works fine in IDLE but not any other circumstance.
What's happening here?
EDIT: If I run the program from the command line instead of windows 10 powershell, I get the following output:
Traceback (most recent call last):
File "C:\Users\Cam\Desktop\CSCI Notes\Programs\test.py", line 1, in
<module>
import tkinter
ImportError: No module named tkinter
However, the tkinter file is in the python library on my computer, and importing tkinter in python shell or IDLE works fine.

When I run python on command prompt, it shows python 2.7, so I changed tkinter to Tkinter and the program worked by importing the .py file
Using cmd:
Pyinstaller works(through canopy cmd prompt):
the first line is what I put into my IDLE's cmd prompt
Which populates the folder when executed:
And inside dist, I run test.exe which shows the window
NOTE: my system command prompt is using python 2.7 while the canopy(python environment) command prompt uses 3.5 with pyinstaller

Okay, I think I've solved this one! I read that windows can sometimes try to open .py files with python 2.7 instead, even if they're written in 3.6. I uninstalled python 2.7 from my computer, and now the file runs normally.
So for anybody having this problem, try to make sure your computer is opening python 3, not python 2.

I have also had this problem and as far as I can see no one has a proper solution.
The best I have found is putting -i before your filename. For example:
python -i yourfile.py
this will start the IDLE in the command line

This could also happen if you are using a modulo that isn't installed in your computer even if you have installed it using your IDLE. Try installing it with the command "pip install modulo" else try installing it manually.

Related

Compiled Python code doesn't run in terminal

I have a python script that I compiled into a Ubuntu Executable. However when I run the executable nothing happens I'm using pyinstaller, and I know its working because another project (That uses tkinter) I did executes in the tkinter window, I guess the question I have is how do I get the compiled python code to run in the terminal instead of a tkinter window.
I compiled both projects the normal way with pyinstaller, but the does that used a tkinter window runs just fine, but the code that should print to the terminal (after asking for the location of a file via askopenfilename) doesn't run at all.

Why do I get different results when I run the same program in pycharm and IDLE?

I want run another python file in my code.It's simple:
import os
command = "python3 简易时钟.py"
os.system(command)
When I use Pycharm, it will run normally.
When I use IDLE, nothing happened. Why?
macOS 10.15.7
python 3.7.9
This works in idle(Windows) for me. Try using the exact path of the file.
Like
command = r"C:\Users\(your_username)\Documents\python3 简易时钟.py"
or whatever the path of the file is.
Or if the python3 简易时钟.py file doesn't generate any output file the python3 简易时钟.py file will run and get closed in the background once it is completed execution.

How to run python in exe window

I have a python code file named testcode.py and I have open a python.exe window open in order to run the code. I read online that I would run the code by putting
python testcode.py
into the exe window but it is saying that a syntax error and points to the name of my py file. Is there a reason why this is happening? Does the py file need to be saved in a certain place?
You're getting an error because you're trying to run shell code in the Python interpreter. Open a CMD or PowerShell window and run python testcode.py there.

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().

python script exceutes bat file: commands not recogned, identical bat file executed by user runs without issue

I am trying to get python27 to execute a very simple bat file (this code is simplified/striped down to a single line). The bat file contains:
C:/Users/dave/Desktop/vlc-2.2.6/vlc.exe
the python code to create and run the bat file is
import subprocess
f = open('myfile.bat', 'w')
f.write('C:/Users/dave/Desktop/vlc-2.2.6/vlc.exe')
f.close()
subprocess.Popen('C:/Users/dave/Desktop/myfile.bat', stdout=subprocess.PIPE)
Running the python script using idle, vlc opens.
Double clicking the bat file, vlc opens.
Double clicking the python script the cmd window opens and closes instantly, vlc command is not executed
I have caught the error once by pressing the pause break button and it stated vlc is not a recognised internal or external command, obviously it is when the bat file is called via idle or by myself.
I am perplexed and new to this style of scripting.
Any adivce would be greatly appreciated.
Many thanks
Dave
SOLVED well sort of
I decided to uninstall all python versions and reinstall python27 64bit.
Without restart;
The commands are being executed I only get 5 identical errors stating:
The process tried to write to a nonexistent pipe
My code is obviously wrong!
But, vlc opens!

Categories

Resources