Kivy, how to disable console - python

I'm coding some software with python and kivy and want to disable the command window, that opens up in window side by side with the kivy app.
I entered that command in the command window to call the python prog:
python main.py -m console
From now on, I can click once at any kivy widget on the bottom side of my app. Then there is that console showing in the picture below. I can disable it by pressing STRG+E twice (activate, then deactivate).
The bad thing, this console part is there with every program start out of eclipse, direct call by python main.py or also in the compiled version, compiled with pyinstaller.
I tried to reinstall whole python on my system, but it doesn't work.
How can I disable this console thingy?

If you're packaging your app with PyInstaller, you should take a look at the --noconsole command line option. It should get rid of the console in Windows and OS X:
Windows and Mac OS X: do not provide a console window for standard i/o. On Mac OS X this also triggers building an OS X .app bundle. This option is ignored in *NIX systems.

import sys
if sys.platform=="win32":
import ctypes
ctypes.windll.user32.ShowWindow( ctypes.windll.kernel32.GetConsoleWindow(), 0 )

Related

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.

How to printout a help to terminal window from compiled .exe file?

I'd like to show short description about program for users.
I have .py file compiled to .exe by pyinstaller and try to use the following code to show help if any arguments were passed to the .exe file from terminal window.
if len(sys.argv) > 1:
# argv[1] has your filename
sys.stdout.write("Description about program...")
sys.exit("Terminating")
If any arguments passed to .exe the program finishes by sys.exit command, but no printout shown in terminal window.
I used -w flag with pyinstaller when generated .exe.
That was the clue, it suppresses console output even if you launch the program through terminal window.
Thank you everybody.
-w, --windowed, --noconsole
Windows and Mac OS X: do not provide a console window for standard i/o. On Mac OS X this also triggers building an OS X .app bundle. This option is ignored in *NIX systems.

Making a PyInstaller exe do both command-line and windowed

I am writing a Python program that can be used both on the command-line, and as an interactive window. (Is that a bad idea?) If command-line arguments are supplied, it executes a task, then prints "success" or "failure". Otherwise, it launches an interactive window.
PyInstaller doesn't seem to be built to support this. I have two non-optimal options:
Use --console mode: The command-line works great, but if I double-click the exe to show the interactive window, it also shows a console window that I don't want
Use --noconsole mode: There's no console popup, but no output shows when using the command-line.
It seems I either need a way to not pop-up the console in --console mode, or to show print output in --noconsole mode. If neither of those options work, I may need to make a separate command-line version of the program.
Any advice?
This is not a perfect solution, but this workaround did the job for me:
Build gui app in --noconsole --one file mode like this:
pyinstaller --noconsole --onefile hello.py
When you double click on the app from windows it will launch normally (without the console).
Now to see the output, navigate to the executable from the command line and type:
hello.exe | more
The "| more" should send the print statements to the console.
This is a problem with Windows (not PyInstaller), which requires the subsystem to be specified as either CONSOLE or WINDOWS at compilation-time.
https://github.com/pyinstaller/pyinstaller/issues/6244#issuecomment-927131015
The recommended solution is to split your app (eg hello) into two distinct versions:
hellow.exe for the GUI version (windowed) and
hello.exe for the CLI version (console)
In theory, you could also add a wrapper .exe that switches between the two actual binaries above, depending on how it's called..

Cannot run PySide Qt program inside PyCharm IDE. Have to run from shell using pythonw or python.app

I made a simple 'pst.py' (hello world) program inside Pycharm. When I run it, it crashes with:
Qt internal error: qt_menu.nib could not be loaded. The .nib file
should be placed in QtGui.framework/Versions/Current/Resources/ or in
the resources directory of your application bundle.
Inside PyCharm, from an Python Console, all the modules look available:
>>> print(PySide.QtCore.qVersion())
4.7.4
>>> print(PySide.__version__)
1.1.2
So I went to the terminal and tried running the same program from the command line using 'python.app' and 'pythonw.' The program worked fine using both python.app and pythonw If I used just python pst.py, I got the same crash.
vt102:dBaseProject sloter$ python.app pst.py
vt102:dBaseProject sloter$ which python.app
/Users/sloter/anaconda/bin/python.app
vt102:dBaseProject sloter$ pythonw pst.py
vt102:dBaseProject sloter$ which pythonw
/Users/sloter/anaconda/bin/pythonw
I think that PyCharm IDE is using the same path (Users/sloter/anaconda/bin/)
I watched a youtube video where a guy wrote a very similar program inside PyCharm and then ran inside Pycharm and things worked fine.
So any ideas what I need to change to enable me to develop PySide/QT program entirely inside PyCharm?
Thanks
ps here's the code for 'pst.py' that I tried to run
#!/Users/sloter/anaconda/bin/python
# Import PySide classes
import sys
from PySide.QtCore import *
from PySide.QtGui import *
# Create a Qt application
app = QApplication(sys.argv)
# Create a Label and show it
label = QLabel("Hello World")
label.show()
# Enter Qt application main loop
app.exec_()
sys.exit()
Qt internal error: qt_menu.nib could not be loaded. The .nib file should be placed in QtGui.framework/Versions/Current/Resources/ or in the resources directory of your application bundle.
One way to get you going would be this:
mkdir -p application.app/Contents/Frameworks/QtGui.framework/Versions/Current/Resources/
cp -r $(QTLIBDIR)/QtGui.framework/Versions/4/Resources/qt_menu.nib application.app/Contents/Frameworks/QtGui.framework/Versions/Current/Resources/
In general, I would personally suggest to use macdeployqt if you happen to copy these manually.

py2exe - Can no longer Copy/Paste text from other Windows app to my Python app after converting my python script to a .EXE file

I recently tried to convert one of my python scripts (used for file-systems I/O) to a executable file by using py2exe. However, after successfully generating the .exe file from my python script, I am no longer able to copy and paste any text(or anything for that matter) from other windows apps to my python app console (when I run the script/app from the .exe file). When right click the mouse now, the access window does not pop up any longer. Does anyone know how I can get around this issue?
Thanks,
A.L.
Right-clicking a console window is a "special feature" of the command line interpreter of Windows Vista and above. It doesn't work on any other command-line tool, so it doesn't longer works for your converted script. You can still access it using the windows menu (small icon in the title bar or Alt+Space).
You could try to get the right-click feature back by messing around with Windows API calls - but the more convenient possibility would be to enable pasting using Ctrl+V and forget about the window menu. A good candidate is PyReadline - install the package and run this at the beginning of your script:
import readline
readline.parse_and_bind("control-v: paste")

Categories

Resources