Running wxpython app in cygwin/X - python

I have installed and tried both wxpython-3.0 and wxpython-2.8 for python2.7 from the standard cygwin repos (64-bit, Win 7). However when I start the Cygwin X server and try to run the most simple "Hello World" script from wxPython tutorials:
# test.py
import wx
app = wx.App(False) # Create a new app, don't redirect stdout/stderr to a window.
frame = wx.Frame(None, wx.ID_ANY, "Hello World") # A Frame is a top-level window.
frame.Show(True) # Show the frame.
app.MainLoop()
I get a Gtk-WARNING **: Screen for GtkWindow not set which eventually ends in a segmentation fault.
The DISPLAY variable is set to :0 (export DISPLAY=:0) and corresponds to the started X server.
Is wxPython broken in cygwin or is some other procedure necessary prior to launching a script using wxPython?

I tested this and got it working for my cygwin64 installation. I used
python v2.7.14 and
cygwin package python-2-wx version 3.0.2.0-3
Both installed via the cygwin installer.
I also have the python-2-wx2.8 in my cygwin install, and I can select which one is to be used by creating the file /usr/lib/python2.7/site-packages/wx.pth containing only one line specifying the relative path to the package to be imported by the "import wx" line:
wx-3.0-gtk3
or
wx-2.8-gtk2-unicode
When I use the later (wx-2.8-gtk2-unicode) I get the same error as you mention, "assertion 'GDK_IS_DISPLAY (display)' failed", but when I use wx-3.0-gtk3 the errors disappear and the script runs fine and I can see the graphics in my X-window.

Try starting your X server with the startxwin command instead of xinit.
startxwin &
export DISPLAY=:0.0
./test.py
I was able to get your test code running with this sequence.

Related

Python app not working after using pyinstaller but doesn't give any errors

So I made an app using python and kvlang, and I was trying to get all the files into a one standalone "exe" file. I needed to include the ".kv" file and my main script. I was using pyinstaller and wrote this command:
pyinstaller --onefile -w --icon=download.ico --add-data filefinder.kv;. filefinder.py
And it all went well - no errors or anything but when I launch the app I just get a quick flash of a white window and then it closes. I have determined that the error must be because of some issue with the ".kv" file but I am not able to fix it cause there's no errors, Nothing! I checked and the app works with the "onedir" option but I need to make it smaller in size. I also tried the "auto-py-to-exe" but it gives the same result. I am happy to provide any more info should you need it to help me resolve this issue. Cheers!
Additional info:
System: Windows 10 pro
Python: 3.9.1
kivy: 2.0.0
Pyinstaller: 4.2
Not sure why it doesn't work, but run the exe in command prompt and then when it fails the error message will not disappear.
Add lots of logs to your application, these can be print statements, as those will always end up on stdout.
i.e. on the first entrypoint, print("Running main")
When you call your first function:
print('calling function_name()')
Once that has finished
print('function_name() complete')
And so on and so forth until you find where exactly the program stops functioning.
Start -> cmd -> navigate to your file using cd -> type in the name of the exe to run it.

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.

Kivy, how to disable console

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 )

Passing shell command to a terminal application from an App in Mac

I have frozen a python based GUI script using Py2app successfully but I run into trouble using this app on Mac. This app is supposed to send arguments/parameters to Clustal, a terminal-based application, but it instead returns an error non-zero exit status 127, '/bin/sh: clustal: command not found'.
I found that my frozen app can send shell command successfully when I execute the same app from Frozen_apl.app>Contents>MacOS>Frozen_apl (which is a UNIX executable file).
Why do these shell commands get blocked when they are passed directly from app? How can I get around this problem?
Note: Clustal is properly installed and its path is properly set. I use OS X 10.9. I have same script frozen for Ubuntu and Windows and they work just fine.
[Based on the discussion in comments] This isn't a problem with the arguments, it's due to the spawned shell not being able to find the clustal executable. I'm not sure why this is, since it's in /usr/local/bin/clustal, and since /usr/local/bin is in OS X's default PATH (it's listed in /etc/paths). Using the full path to the executable worked, so it appears the frozen app is spawning a shell with a non-default PATH.
Including the full path (/usr/local/bin/clustal) in the frozen app isn't really an optimal solution; it'd be better to figure out how to get a normal PATH in the spawned shell. But I'm now familiar enough with Py2app to know how to do this. (JeeYem: please give the workaround you came up with in a comment or another answer.)
Quoting from Py2app 0.6.4 minor feature release:
Issue #15: py2app now has an option to emulate the shell environment you get by opening a window in the Terminal.
Usage: python setup.py py2app --emulate-shell-environment
This option is experimental, it is far from certain that the implementation works on all systems.
Using this option with Py2app solved the problem of blocked communication between Py2app-frozen app and Os X shell.

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.

Categories

Resources