Script works in IDLE, but .py-file doesn't work - python

I have a tkinter script, which runs just fine in IDLE. However, when I double click the .py-file from Windows Explorer, the console window flashes half a second and then it exits.
I was able to screenprint the console window. It says:
...etc.etc...
NameError: global name 'simpledialog' is not defined
simpledialog is a module in tkinter which I use in my script. As I do from tkinter import *, there is no need to explicitly write tkinter.simpledialog.
It works in IDLE, why not as .py?

IDLE uses Tkinter as its graphical environment. It is possible that your code is relying on a side effect of an import by IDLE itself. This is especially true if you use IDLE without a subprocess.
The simpledialog module does not import when using from tkinter import *.
Try adding this to your code:
import tkinter.simpledialog as simpledialog

Have you updated your PATH environment variable so that your Python executable is found? You can find more information on how to do here - Using Python on Windows
But you basically need to make sure that the folder containing python.exe (e.g. C:\Python32) is displayed when you type the following command from a prompt:
echo %PATH%

I had exactly the same problem with one of my scripts utilizing Tkinter.
Adding call to mainloop() fixed the issue.
See this tutorial for an example: [http://sebsauvage.net/python/gui/#import1
In my case, in the init function I have
def __init__(self,Width=400, Height=400):
# Create GUI window ------------------------------
win = Tk()
...
in the end of init I added:
win.mainloop()
Now it works by just running the file.
Hope this helps

Similar trouble for me just now, in my first week with python. But I dimly remembered a similar problem with a simple early test script and thought the trouble then was # comments.
So I tried that with my Tkinter infused .py script. It ran fine in IDLE as you say, then only flashed when clicked in windows. But there were a couple # commented lines at the top of file.
I took them all out and it now runs no sweat directly in windows. Have a look .. for #.
Sorry, can't seem to delete this post. Now the files work #comments included. Don't know what's up with that. ..

I found that changing the executable py file to a file.pyw fixed the problem. This tells python to execute it using the pythonw.exe which runs the script without the terminal/console in the background.
Not sure why this works, perhaps some screwed up environment variables from a previous python installation.

Changing the file's extension to pyw instead of py might solve the problem

Related

Hide a python caused window?

I searched for a way to hide a python caused window, for example a console or a ui window, so that you can't even see the open tab from this running application.So its like all actions of the python-program never have happend.
For example you do this:
print("Hello World")
and the user souldn't see anything of this:
But I found nothing, so does anybody know how this works?
Change the file extension of your python script file to '.pyw'. So, rename asdf.py to asdf.pyw.
This will make your script to be interpreted by a different executable than normal (precisely, pythonw.exe instead of python.exe).
Note that I do not know whether this works on other operating systems than Windows.
I finally found out a way to do this by using two modules:
*This works for cmd- and GUI-windows
import win32console # first module
import win32gui # secound one
win = win32console.GetConsoleWindow()
win32gui.ShowWindow(win, 0) # Hides the window
win32gui.ShowWindow(win, 1) # Shows the window
Edit: I found a second way to hide a terminal windows that is caused py python: All you have to do is to install pyinstaller with pip(3). Then write your sript, go to the command prompt and type in:
pyinstaller --onefile -w <sriptname>
-w = don't shows any console window (windows-less)

How to run python script on atom?

I'm new to using atom and was wondering how to run a python script on it. I have code written at the moment which works perfectly fine in normal python shell, using tkinter, however when I run it through the command line, it says:
import tkinter as tk
ImportError: No module named tkinter
How do I fix this? In my environment variables I have already added python.exe, the file directory to the actual script I'm running and to the python download itself. How do I fix this?
The best way is to load the Jupyter plugin. It's called Hydrogen. Then under the packages menu, you can select Hydrogen/Run all and it will run your python code. there is a keyboard shortcut for doing this which speeds up the process. You can easily check your code as you write it by using the hydrogen option to run a line and go to next line.
As for your Tkinter problem have you loaded Tkinter? You can do this using pip install Tkinter. After that try running your code again.
In python 2.7 and below, the modules's name is Tkinter, while in 3.0 and above, it is tkinter.
Atom (whatever this is) seems to use a 2.7 or below version of python interpreter.

How to stop console from poping up when command is called from python GUI?

I have made a GUI for my application. All scripts are in Python (2.7) and the GUI is created with Tkinter. I work on Linux, but I needed this app to be executable on Windows. So I've used py2exe, to create an executable. After a while it is working almost perfectly, but I have the following problem:
Somewhere in the application, I need to call external programs, namely ImageMagick and LaTeX. I use the commands convert, pdflatex, simply by importing os module and running os.system(build), where build = 'convert page.pdf page.gif'etc. When those commands are called from the *.exe application the console pops up (meaning a console window opens up for a split of a second and closes again). Is there a way, to prevent this behaviour?
It does not interrupt the application, but it is ugly and not a desired behaviour.
[Note] I chose not to add any samples, since there are lots of files and other content, which, I think, is not related to the problem. I could however try to post minimal (not)working example. But maybe it is not needed.
Thanks!
import subprocess
subprocess.Popen("application.exe", shell = True)

Importing tkinter duplicates printed output

I am trying start learning Tkinter to make a small gui application, however every time I do import tkinter all the print statements are duplicated. The problem exists with the absolute simplest script.
I am running windows 8.1 and python 3.5, and I am writing and building the application in Sublime Text 3 with a customized build system.
It is however unlikely that ST3 has anything to do with it since the problem also exists when running the script from the command line py -3 tkinter.py
Here is an example of my problem. This is all of the code.
import tkinter
print("Hello")
Always outputs
Hello
Hello
But code consisting of just print("Hello") outputs, as expected
Hello
I am truly lost with this problem and it would be of great help if any of you could point me to the right direction
You see two prints as your named your script tkinter.py so the print shows when you run it with py -3 tkinter.py and you see another as you import again in the script. You are not importing from the tkinter lib but from your own script whose name shadows the tkinter lib.
To fix, rename your script and delete any .pyc files in the directory

How do I run a .py(GUI) file outside of IDLE in order to use sys.exit

How do I run a .py file outside of IDLE in order to use sys.exit? I'm using command=sys.exit and seems to crash inside of tcl & tk when the button is pushed. The exit is working cause it returns SystemExit in the Shell. I've found that I need to run this outside the IDLE to have this work properly. Oh, I'm working from the book "Programming Python" tutorial. How do I run a .py(GUI) file outside of IDLE to see sys.exit work?
import sys
from tkinter import *
widget = Button(None, text='Hello widget world', command=sys.exit)
widget.pack()
widget.mainloop()
1.- In windows double click on the file.
2.- For linux, if not already there, add a shebang first, telling where python is.
Shebangs are like:
#!/usr/bin/python
or
#!/usr/bin/env python
also remember the dot-slash if you call the program from console:
./myscript.py
3.- In OSX follow same as in linux. Also see these specific instructions and these other ones

Categories