Moving CMD python app to the system tray (windows) - python

I have an python 2.7 application that I would like to move from the CMD window to the system/notification tray (Windows 7). The python script currently listens for particular TCP messages, and upon receipt, logs them to a file.
I have recently found the following system tray code, from another question answered here:
systrayicon
I've been able to run the systrayicon demo correctly by creating the SysTrayIcon.py module. My problem is, when I try to run my TCP application alongside the systrayicon (with some of the demo code intact), it only runs my application, or the system tray application, not both. More specifically, if I list my app's main(sys.argv) before the systrayicon(), it only runs my app without the tray icon. If I have the systrayicon listed before my app, it runs the system tray demo, until I select "Quit", and it then runs my app.
I am able to get my TCP app to run correctly if I plug it into one of the menu selection functions, for example:
def hello(sysTrayIcon):
main(sys.argv)
But I'd like my app to begin listening when the application is opened, without selecting something from the menu to initiate it.
This is likely a very basic problem, and maybe worded poorly, my apologies.

I think the easiest way for you to accomplish this would be to just wrap your script with a small piece of GUI code using Tkinter or wxPython. Then it can reside in the tray. You will probably need to run your script in a thread as it sounds like a long running process that would block the toolkit's main loop. You might need to add some code to the threading code that allows you to kill your script when the GUI exits, but other than that, it should be pretty easy.

Related

Linux Python process with visible console window

I have a question about Python on Linux. I have a Python application that currently runs on Windows. The application controls some hardware, and each hardware control application runs in it's own process, which in turn sometimes start processes of their own. The processes communicate with named pipes, named mutexes, and named memory-mapped files to the main control process, but each application process has its own console window. This allows the user to select the window for one application process, representing one hardware item, and view what it's doing. It also allows a simple "print" to produce debug statements on the window for that process. On Windows this is easy because either os.startfile or subprocess.popen can run a python script in a separate process, in a new console window, and capturing "print" output in that window. The main process starts all the application processes and then minimizes the windows, making it easy for the user to select one (or more) for viewing. The application processes write log files when they are done, but having a console window for each one allows viewing of progress and messages in real time.
I need to make a linux version of this and I'm running into issues. I can't figure out how to make Linux open an application in a separate, visible, process window. If I use subprocess.popen with shell=True, I get a separate process but no visible window. If I set stdout=subprocess.PIPE, the application isn't in a separate process, but uses the main process window for printing, and incidentally hangs the main process until it's done (this is disasterous in my application). I found a workaround where I open the application process with shell=True, and the application process then creates a named pipe and opens its own GUI (using shell=True) for output display. But this means I have to change all the print statements in the application processes to go to the named pipe, which is a huge amount of work. Plus, it would be nice (but not essential) if the Windows and Linux versions looked the same in how the windows appear.
Is there a way, in Python, on Linux, to start a new Python process in an open, visible window that will capture "print" statements? Or am I trying to do something Linux doesn't support? I'd rather not change everything to sockets - it would probably be easier to use the GUI and named pipe method than to do that.
Thanks for any answers or insight.

Running flask app on DigitalOcean: should I keep the ssh console open all the time?

I created a droplet that runs a flask application. My question is when I ssh into the droplet and restart the apache2 server, do I have to keep the console open all the time (that is I should not shut down my computer) for the application to be live?
What if I have a dynamic application that runs scripts in the background, do I have to keep the console open all the time for the dynamic parts to work?
P.S:
there's a similar question in SO about a NodeJs app but some parts of the answer they provided are irrelevant to my Flask app.
You can use the "screen" command to mantain the sesion open.
please see https://www.rackaid.com/blog/linux-screen-tutorial-and-how-to/
In my opinion it is not a good practice to use remote computers for the development stage unless you don't have an other option. If you want to make your application available after logging out from the ssh console, screen works, but it still a workaround.
I would suggest taking a look at this great tutorial on how to daemonize flask applications with Gunicorn+Nginx.
You needn't keep the console on, the app will still running after you close the console on your computer. But you may need to set a log to monitor it.

Upstart - Graphic window not displaying

I've got this python script that opens up a VNC session through a graphical window. It sits there blocking waiting for a particular message to come in and then spawns the graphical window successfully when ran from the command prompt.
But if I place this in an Upstart .conf file so I can use it as a background service, I can see that my program receives the message to pop up a window but it does not display a graphical window.
...
exec /usr/local/bin/vncscript.py # does not display window
...
How would I go about displaying a graphical window using Upstart?
Ended up going with the solution from by setting up a .desktop file:
https://askubuntu.com/questions/107187/how-to-launch-gui-app-with-upstart-script
Initially, I started by a
export DISPLAY=0.0
...and it worked. I could then control it as a service. But as soon as I restarted the computer, it would not work as expected. Could not even stop the service after starting it. I'm not sure why it worked in the first place.

launch a python program on a server

Edit: Please note that it's our first time working with a server, python, and this is my second website, so I know almost nothing about it
We have a project and we need to put our python code on a server
the problem is, we've tried using cgi to run the code and we have two issues:
the program uses Tkinter, and it looks like we can't use program that use Tkinter on a server
when I try to run a script with a loop (our program is a timer, so it will loop), the page loads forever, it only stops loading when the program ends
can we add this python program on a server?
(also, we use a apache server)
You cannot run a tkinter program on a server via CGI. Tkinter and web technologies are incompatible with each other.
Your observation about a program that runs in a loop via CGI is correct -- the program must finish in order for the CGI mechanism to work. The web server will work until that process is finished, and then return what the program returns.

GTK+ Startup Notification Icon

In Gnome, whenever an application is started, the mouse cursor changes from normal to an activity indicator (a spinning wheel type thing on Ubuntu). Is there any way to inform Gnome (through some system call) when the application has finished launching so that the mouse cursor returns to normal without waiting for the usual timeout of 30 seconds to occur.
I have a program in Pythong using GTK+ that is showing the icon even after launching, so what system call do I make?
Normally it happens automatically when you open the application's window.
It may be that the application's launcher just calls an already running instance, in that case it won't be automatically detected. The call you need then is this:
import gtk
gtk.gdk.notify_startup_complete()
Your application can opt out of startup notification by adding
StartupNotify=false
to your application's .desktop file.
Of course, it is friendlier to leave it enabled and participate in startup notification.
I had a similar issue with an application I wrote. I was launching the application through a shell script containing the line
python /path/to/application.py
This launched the application as I expected, but the startup notification did not stop.
It worked correctly once I changed the content of my script to this:
exec "/usr/bin/python" "/path/to/application.py"
Obviously the latter one seems to be the correct way to launch the application, though I don't have enough insight to tell why.
This normally happens automatically when calling the gtk.main() function

Categories

Resources