Prevent Python shell close - python

How can I prevent a Python shell/commandline/terminal window from beeing closed, if the user clicks the close button of the Window? I've tried to catch SystemExit exceptions and overriding sys.exit(), no success.
I know, I can do this with a GUI toolkit/framework like PyQt, but importing that (and including it in the final binary of my program) would be no option while it's too big.

It is not possible to do this in the general case.
The terminal window is (generally speaking) a separate process from Python itself (YMMV with IPython and other such things, but this is true if you're running regular Python inside a regular terminal). When the terminal window decides to go away, it is not possible (under most reasonable operating systems and window managers) to prevent it from doing so, because it's a totally different program which just happens to be displaying your input and output.
You can, however, keep your program running after the terminal closes. The terminal will still go away, but the program will keep running in the background. On Unix systems, this is generally done by catching or ignoring the SIGHUP signal using signal.
Depending on your operating system and choice of terminal emulator, it may or may not be possible to configure the terminal to not close. Some terminal emulators under Linux will, by default, warn the user that there is a process running under the terminal when the user tries to close it. This is highly platform-dependent and can't be configured (portably or perhaps at all) from within Python.

Related

When the console is selected, how do I get the main window to come to the front?

I have no idea if this problem has the solution I want but here goes.
I'm making a PyQt4 program that apparently needs the console window to run properly. However, whenever I activate another window, sending the program I'm working on to the back, the only way I can get back to it is by closing all the windows in front of said window. I can't just click on the taskbar because the only thing that comes back is the console window.
I'm curious. Is there a way to have the GUI window activate along with, or independent of, the console window without having to go through the annoying process of closing (minimizing) potentially all the rest of your windows?
Edit: I just realized my question is pretty vague. Let me elaborate.
I'm compiling said program using pyinstaller.
The reason it needs the console window to work properly (I have tried using the .pyw file as well, to no avail) is because there's another program that's the core of this one that prints out to it in a way I can only describe as violently.
Apparently it won't be happy unless it has the console to record it's outbursts.
That being said, I need the console window. However, as I mentioned before, that is the only thing that comes up when the pyinstaller icon is clicked.
There is a gui attached to the console, but there's no way to get it back even after the user would minimize it because the pyinstaller icon insists it doesn't exist.
Maybe it has something to do with how I defined the window while programming it, but I don't see why that would be the case. Is there something in particular pyinstaller doesn't like that would make it act like this?
How are you launching the PyQt application?
If you're launching it with the python executable, it will create a console.
python my_application.py
Instead, launch it with the GUI version of python -- pythonw:
pythonw my_application.py
If the python path isn't in the system path, you may need to specify the whole path to the executable:
C:\python27\pythonw.exe C:\path\to\my_application.py

How to have a natural MacOSX .app of a complex python application (including custom interpreter) via a shell initialization script?

I am trying to integrate a complex python application (with a custom python interpreter shipped along) for OSX. In order to handle a set of issues due to cross platform requirements, I created a .app bundle pointing at a shell script with its CFExecutable entry in Info.plist. This works, and the invoked shell script starts up the actual application binary. However, I have the following problems:
The .app icon bounces endlessly on the dock, never reaching the "activated" status. I guess it's because the shell script does not terminate. This dock entry has the correct "application icon"
When the binary executable is invoked by the script, a new Dock entry appears with a generic python icon. This icon successfully starts up and stops bouncing as the application starts up.
When I try to kill the first Dock entry via Force quit, the actual application still keeps running, as it's clearly controlled by the second entry on the dock.
Is there a way to have this setup behave more naturally? Do I need to ditch shell script for an objective C wrapper? If I have to use a obj-C wrapper (instead of a shell script) to spawn my application, how can I prevent the same spawning of a secondary icon to happen?
Edit: note, I am not running a python script. I am running a custom made python interpreter. py2app is not what I need.
It seems like many use py2app for this purpose with success.
If that doesn't work for you, the Java equivalent I'm famailar with, appbundler, does create a mach-o executable stub to fire things off. You might check out how they do things, particularly their main.m.
I solved the problem, and in hindsight it was rather trivial. In the shell script, I need to invoke my binary with exec, so that the running bash process is replaced (a la execve()) rather than spawning a new process. The only problem is that my interpreter now replaces the icon with the stock one, but I have only one icon in the dock now, and behaves naturally.

Command window flashing only when os.system is being used

I have a weird issue on a friends machine where when they run my script, it only flashes the command window when it hits a line that does something with os.system. It seems to not show the command window at all otherwise. Not showing is fine, but my script does a large process and it causes the window to flash a lot.
Normally on my machine, when I open the script (that launches a gui with tkinter) the command window will open as well in the back. This is what I am trying to get my friends to do, but I do not know what would cause the command window to not show. Is there a setting within python somewhere that doesn't show the command window (unless of course it needs to run a process in it)?
Edit: Ok I did a really lame hack to fix this. Apparently all he .py files got associated with pythonw. I was unable to get this association changed (even going through the command prompt ASSOC) So I just did a silly renamed of the pythonw to old_pythonw, and renamed the python.exe to pythonw.exe. Yes, really silly but no time to argue with windows about file associations.

Is there a way to run two or more python modules simultaneously from Eclipse(pyDev)?

I am in the process of building a system in python that centralizes the compilation of our code to a set of machines. I have all three programs written, running and working; however I'm still trying to weed out some of the more elusive bugs. I have been mostly testing over the localhost interface and therefore run all of the components on my machine.
Is there a way to run all the components simultaneously in one Eclipse session so that I can flip between them and terminate if needed?
I have been using multiple terminal windows, but since the code is still immature, it's not always possible to exit cleanly from the program.
Yes - just run them as normal and use the Console menu to flip between them. If you run them under the debugger, you can also use the Debug view in the Debug perspective to terminate them - in either case, using the red square icon to do the terminating.

Minimize python to system tray in Windows (Vista)

I would like to know if it is possible to minimise my python program to the system tray.
Basically what it does is at every hour it takes a screenshot but I don't want it to stay on the task bar taking space. Could I make it go to the system tray area next to the clock but keep it running?
I am not using tkinter or anything like that.
Since you are running on Windows, you may simply want to rename your script to have a .pyw extension, so there is no console window. If you try to make a system tray application, you will need to pick a GUI toolkit like you've suggested, and your simple script will become a LOT bigger and far more complicated.
Seems like some rather helpful folk here...
I was curious myself, and am poring over this persons PyWin32 app which might help your cause, Matthew:
http://www.brunningonline.net/simon/blog/archives/SysTrayIcon.py.html
As opposed to having the program running continuously, why don't you use your system's scheduler? (Cron under *nix, Task Scheduler under windows). There might be a bit more overhead since it has to spin up Python each time, but it would be easier than hooking up a notification icon.
Since you refer to it as the task bar, I assume Windows. To have a notification icon, you would have to have a hidden window, with a running message pump, so Windows has somewhere to send messages to for the icon.
So, in short, much simpler just running a scheduled job.

Categories

Resources