Minimize python to system tray in Windows (Vista) - python

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.

Related

(Python) Process Launcher for development purposes

For a distributed system I have several processes that are required to be launched and I am working on (developing) almost all of them at the same time (modify here, adapt there, ...).
I usually run them in several terminal windows, but would prefer some kind of "process launcher" where I can configure the individual processes in a config file, and then preferrably get the log output, maybe colorized, in a single terminal window.
I started to write something on my own, but have not been happy with the result and wonder if there is not a dedicated tool for that purpose.
Reloading (on file change) would be cool, but I could also use watchmedo for that purpose.
Does someone here have a hint for me?
You may be interested in docker-compose, using it you will be able to predefine multiple python processes easily and run them all together, seeing the logs of each process live and colored, restarting all or part of them, etc...

Prevent Python shell close

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.

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

Running a python app alongside the live interpereter

I'm making a drawing program with python and pygame. I am trying to incorporate a script-fu thing in which the program opens a python live interpreter upon startup and allows the user to execute commands in the interpreter alongside the graphical interface.
My current strategy is to run the main loop inside its own thread, then have the application opened using a bash script that does 'python -i main.py'
Is this a safe/effective/ideal way of doing this? How can I use locks to ensure that commands coming in from the interpreter are executed between main loop iterations?
This is my first time using threads, so please explain to me like I am 7.
Thanks :)
The interpreter won't cooperate with locks you set (since it doesn't know about them). Thus, you cannot guarantee when the code entered by the user will execute.
Consider using the code module to build your own interactive console (it's really easy!). Then you can do the locking every time you go to execute user input.
Why are you using a third party live interpreter? Do you realize that pygame comes with one built in? The documentation is here. This will eliminate all of your problems quite easily.

python sleep == IDE lock up

When my script sleeps for 50sec my IDE locks up which is very annoying. I cant switch tabs, look through my source, type code, etc. It happens in pylde and pyscripter, i havent tried other IDEs. What can i do to fix this? i'm actually doing
for i in range(0, timeInSeconds): time.sleep(1)
hoping the IDE will update once per second but it doesnt look that way. What can i do to fix this?
I'm assuming you are running your code from within the IDE?
Your IDE is probably blocking while running your code. Look for a setting of some sort which might control that behaviour, otherwise I think your only choice would be to change IDE. (Or, run your code from outside the IDE)
Can you configure to run your script externally? I don't know about the specific IDEs, but I would try to spawn a different process for the debugged script and not run them under the IDE. If that doesn't help, then it is a problem of the IDEs.
The problem is your IDE not python. I don't use sleep that often, I've just tried it on the Eric IDE and you can use your IDE while your code is running, and sleeping. If can't set your IDE to do so and you need it then consider to change IDE or to run your code from console.
Personally, I think you should never ever ever execute code in the same loop as your IDE. Since most IDEs run a GUI mainloop, blocking this will cause complete freeze of the user interface. It is just asking for trouble, and I would take out bug reports against both those IDEs.
I suspect the problem the IDE is sitting in a loop waiting for the script to finish.
That in itself is not a problem, provided any user generated messages are still processed while the IDE is in this loop.
But what I suspect is going wrong in this case is the IDE is just running the loop without processing and messages and hence the user interface appears to be locked.
The IDE would need to be changed to either process GUI messages while in the loop or alternatively it needs to create a thread to run the the script. The thread would then run in the background and the GUI would remain responsive.
For example the Zeus for Windows IDE uses the background thread approach and it does not have this problem.

Categories

Resources