why is the python GUI interface fleeting - python

#coding=utf-8
import wx
class App(wx.App):
def OnInit(self):
frame=wx.Frame(parent=None,title='Bare')
frame.Show()
return Ture
app=App()
app.MainLoop()
runs OK! but the GUI interface is fleeting, just leaving the CMD console in the screen.
a newer for python ,why the outcome of the GUI interface fleeting?
environment:Gvim+WIN7+PYTHON2.7

First, according to help :! command of vim:
:!{cmd} Execute {cmd} with the shell.
vim cause the cmd shell to be executed.
Second, according to Using Python on Windows - Executing scripts:
Python scripts (files with the extension .py) will be executed by
python.exe by default. This executable opens a terminal, which stays
open even if the program uses a GUI. If you do not want this to
happen, use the extension .pyw which will cause the script to be
executed by pythonw.exe by default (both executables are located in
the top-level of your Python installation directory). This suppresses
the terminal window on startup.
So, if you want run the GUI program without cmd console, run the program outside the vim using pythonw.exe. For instance, save the file with .pyw extension, and double click the file.

It is fleeting because you have a NameError in your code. You should be returning True, not Ture, which is undefined.
I am surprised you still see the console window though. If that stays up though, it should be showing the traceback.

Related

I want to display the results from other applications(LabVIEW) in the CMD window

I am programming python code in LabVIEW now. I want to display my results in CMD console, but I don't know how to connect them to cmd console. My bielf code is as follows.
import os
os.system("start /wait cmd")
print("I want to display my results to the CMD window")
This ensures that only the cmd window appears and no print function results appear. Help me
There are two types of programs. Console and graphical. The difference is a flag in the file header. If set as non graphical it will open with an automatic console unless it's parent program is also non graphical then it will inherit it's parent's console. If set as graphical absolutely nothing special happens.
Any program can show windows and any program can start a new console or attach to any existing console.
Your question is a bit weird. CMD is a console program. There is nothing special about it. You don't need CMD to run in a console.
So it depends on how you compile. With a graphical interpreter or a console interpreter.
However you are starting CMD disconnected from your program, which it would be if your program isn't a console program anyway, by using the START command which starts programs in unusual ways.
See start /?. See https://learn.microsoft.com/en-us/windows/console/character-mode-applications.
PS You write to a console not to CMD.

How can I make .py files run using Python Shell by default?

I made a Python GUI using Tkinter, but when I run it directly (double click the file) it opens the black python window but automatically closes by itself in less than half a second. I found a way to make it open the IDLE editor but it just opens the editor and doesn't run it.
I want it to run the way it runs when you open the IDLE editor and press Run Module. This runs it using Python Shell.
Is there a way I can make it automatically run using Python Shell?
Based on Mark Tolonen's comment you should do two things
rename your file to a .pyw from .py to prefer console-less runs
set your system to open .pyw files with pythonw if that's not configured already
Linux: configure xdg-open
Windows: right click and choose an application from the context menu (you may need to find where Python is installed to select the pythonw.exe executable)
Okay, one of the comments on the original question is correct.
As Terry Jan Reedy (user:722804) said,
It is possible that your mygui.py file is missing 'root.mainloop()' or the equivalent to start the GUI. IDLE lets you omit that during development so that one can interact with tkinter to retrieve values and make changes to widgets.
Adding gui.mainloop() to the end of my program worked.

Tkinter program compiled with pyinstaller crash on launch

I'm asking help today because I'm new to Tkinter and Pyinstaller (and python in general) and I'm having troubles with it.
I have a simple app working with sqlite, tkinter and pyinstaller to compile all of this in an executable program, the entrance point of my program is a file named main.py
This file calls all the dependancies (like the sqlite module for python, tkinter and my other files like classes etc...)
I made a very simple interface, with a Hello World in a tkinter label and a button to go to page 2 which displays page2 (also in a label), just to see if I'm capable of making it all run and compile all of these pieces together.
I can run it throught my shell executing it like : python main.py and everything is working fine.
But when I run pyinstaller on my linux machine, and start executing the program, nothing appears, my database.db (sqlite database file) is created but I don't have any interface like when I run it with my shell. The thing is getting even worse on windows where, once I've my .exe it just opens a shell and crash after few seconds, not even creating the database.
What I did is I created a 'log file', in which I write the steps of the program.
As you can see on the following picture, the 2 first prints are wrote in my log file (on linux), so I think it crashes when I try to create the window.
If any of you have an idea on what I do wrong, I would really appreciate help :)
General
From the PyInstaller manual:
Before you attempt to bundle to one file, make sure your app works correctly when bundled to one folder. It is is much easier to diagnose problems in one-folder mode.
As the comments suggested, use a catch-all try/except block to log all exceptions to a file. That is probably the best way to see what is really happening. Make sure that the logfile is created in an existing location where you have the necessary permissions.
I would suggest to take advantage of the built-in logging module instead of creating your own. It can e.g. automatically add from which file a log line was created.
IMHO, it is probable that the failures on Linux and ms-windows have completely different causes. You should probably treat them as different issues.
Linux
When you use single file mode, that file is unpacked into a temporary folder, probably somewhere in /tmp. Some Linux distributions mount the /tmp filesystem with the noexec flag. This is incompatible with PyInstaller.
ms-windows
On windows, there are basically two different Pythons; python.exe and pythonw.exe. Basically it is one of the quirks of windows that this is necessary. The latter is for GUI programs like tkinter programs. A tkinter script should not show a cmd window. So I'm guessing that PyInstaller calls your command with python.exe instead of pythonw.exe. From the manual:
By default the bootloader creates a command-line console (a terminal window in GNU/Linux and Mac OS, a command window in Windows). It gives this window to the Python interpreter for its standard input and output. Your script’s use of print and input() are directed here. Error messages from Python and default logging output also appear in the console window.
An option for Windows and Mac OS is to tell PyInstaller to not provide a console window. The bootloader starts Python with no target for standard output or input. Do this when your script has a graphical interface for user input and can properly report its own diagnostics.
As noted in the CPython tutorial Appendix, for Windows a file extention of .pyw suppresses the console window that normally appears. Likewise, a console window will not be provided when using a myscript.pyw script with PyInstaller.
Also, on windows it can matter which Python distribution you're using. I used to be a fan of Anaconda, but lately I've come to prefer the python.org version because it gives me less headaches. On anaconda Python I had the problem that tkinter programs would not launch without showing a cmd window, whatever I tried. Only switching to python.org Python solved that problem.

python vs pythonw [duplicate]

Long story short: pythonw.exe does nothing, python.exe accepts nothing (which one should I use?)
test.py:
print "a"
CMD window:
C:\path>pythonw.exe test.py
<BLANK LINE>
C:\path>
C:\path>python.exe test.py
File "C:\path\test.py", line 7
print "a"
^
SyntaxError: invalid syntax
C:\path>
Please tell me what I'm doing terrible wrong.
To summarize and complement the existing answers:
python.exe is a console (terminal) application for launching CLI-type scripts (console applications).
Unless run from an existing console window, python.exe opens a new console window.
Standard streams sys.stdin, sys.stdout and sys.stderr are connected to the console window.
Execution is synchronous when launched from a cmd.exe or PowerShell console window: See eryksun's 1st comment below.
If a new console window was created, it stays open until the script terminates.
When invoked from an existing console window, the prompt is blocked until the script terminates.
pythonw.exe is a GUI app for launching GUI/no-UI-at-all scripts.
NO console window is opened.
Execution is asynchronous:
When invoked from a console window, the script is merely launched and the prompt returns right away, whether the script is still running or not.
Standard streams sys.stdin, sys.stdout and sys.stderr are NOT available.
Caution: Unless you take extra steps, this has potentially unexpected side effects:
Unhandled exceptions cause the script to abort silently.
In Python 2.x, simply trying to use print() can cause that to happen (in 3.x, print() simply has no effect).
To prevent that from within your script, and to learn more, see this answer of mine.
Ad-hoc, you can use output redirection:Thanks, #handle.
pythonw.exe yourScript.pyw 1>stdout.txt 2>stderr.txt
(from PowerShell:
cmd /c pythonw.exe yourScript.pyw 1>stdout.txt 2>stderr.txt) to capture stdout and stderr output in files.
If you're confident that use of print() is the only reason your script fails silently with pythonw.exe, and you're not interested in stdout output, use #handle's command from the comments:
pythonw.exe yourScript.pyw 1>NUL 2>&1
Caveat: This output redirection technique does not work when invoking *.pyw scripts directly (as opposed to by passing the script file path to pythonw.exe). See eryksun's 2nd comment and its follow-ups below.
You can control which of the executables runs your script by default - such as when opened from Explorer - by choosing the right filename extension:
*.py files are by default associated (invoked) with python.exe
*.pyw files are by default associated (invoked) with pythonw.exe
If you don't want a terminal window to pop up when you run your program, use pythonw.exe;
Otherwise, use python.exe
Regarding the syntax error: print is now a function in 3.x
So use instead:
print("a")
See here: http://docs.python.org/using/windows.html
pythonw.exe "This suppresses the terminal window on startup."
If you're going to call a python script from some other process (say, from the command line), use pythonw.exe. Otherwise, your user will continuously see a cmd window launching the python process. It'll still run your script just the same, but it won't intrude on the user experience.
An example might be sending an email; python.exe will pop up a CLI window, send the email, then close the window. It'll appear as a quick flash, and can be considered somewhat annoying. pythonw.exe avoids this, but still sends the email.
I was struggling to get this to work for a while. Once you change the extension to .pyw, make sure that you open properties of the file and direct the "open with" path to pythonw.exe.
In my experience the pythonw.exe is faster at least with using pygame.

Hide the console of an .exe file created with PyInstaller

I want to make my program executable.
I used TkInter to write the GUI, and I read somewhere that you have to save your file as .pyw to hide the console when the program is executed.
The problem is that after making it an executable with PyInstaller, the console shows up again, even though the file converted was .pyw.
How can I hide the console also in the .exe file?
Did you try --windowed command line flag ?
You can just save the file with .pyw extension and you can just use pyinstaller --onefile Filename.pyw
I think you renamed it. You save it as .pyw don't rename it.
The screenshot are below:
Output:
But it takes a while to open.
Thank you
-Levers
What are you using to make the executable?
If you use py2exe and you use:
setup(windows=[pythonscriptnamehere])
in the setup script instead of:
setup(console=[pythonscriptnamehere])
it will run the executable without launching a terminal in the background.
From The PyInstaller Documentation:
Using a Console Window
By default the bootloader creates a command-line console (a terminal
window in GNU/Linux and Mac OS, a command window in Windows). It gives
this window to the Python interpreter for its standard input and
output. Your script’s use of print and input() are directed here.
Error messages from Python and default logging output also appear in
the console window.
An option for Windows and Mac OS is to tell PyInstaller to not provide
a console window. The bootloader starts Python with no target for
standard output or input. Do this when your script has a graphical
interface for user input and can properly report its own diagnostics.
As noted in the CPython tutorial Appendix, for Windows a file
extention of .pyw suppresses the console window that normally appears.
Likewise, a console window will not be provided when using a
myscript.pyw script with PyInstaller.
Rather than saving/renaming/changing the extension to .pyw, you can just add --noconsole to the command line and use the standard .py file with it and this would hide that console window.
Example:
We have a file named GUI_name.py in our folder that uses Tkinter. Now we could easily create .exe file that doesn't show the console window by typing pyinstaller --onefile --noconsole GUI_name.py in cmd.

Categories

Resources