1

I want to add a systemtray to an existing python based project: https://github.com/piejanssens/premiumizer

How exactly do I need to do that? I am completely new to python and I am using it for the first time, just because I want to add a little feature to and existing script.

What I want to achieve is that when the script is running that there should be a system tray icon which opens http://localhost:5000 if it is double clicked. And if it is right clicked there should be an Exit/Quit option.

I have researched a bit and I think I could achieve it with one of these two scripts https://github.com/moses-palmer/pystray or with https://github.com/Infinidat/infi.systray (I also read that infi.systray should be used because it is not dependent on pywin32 because it uses the ctypes library because that one is part of the standard Python library).

So I tried to add this code for testing to the premiumizer.py file:

from infi.systray import SysTrayIcon
    def say_hello(systray):
        print "Hello, World!"
    menu_options = (("Say Hello", None, say_hello),)
    systray = SysTrayIcon("icon.ico", "Example tray icon", menu_options)
    systray.start()

But now the Console is closing itself immediately. How can I check what went wrong? Is an error log saved somewhere ?

What do I need to do to make it work? Or is there an easier way for someone "stupid" like me ?

Salexes
  • 187
  • 1
  • 2
  • 20
  • Can you provide a self-contained complete example that exhibits the error behaviour you need help with? – John Lyon Mar 13 '19 at 01:06

1 Answers1

1

Welcome to the world of python!

Let me assume that you copied the script you've posted into a python file and just ran the file, correct? If so, the problem is that once the script is executed the program exits and with it the tray icon.

Start an interactive console by running python or (ipython if you have it installed) in a command window and paste in your code. You'll see that the tray icon appears and stays. It disappears once you close the console. (Remark: the code above uses the python 2.x version of print without the () and will cause an error in python 3.x, there use print("Hello, World!").)

To make this work you need to put this code somewhere in the setup/initialization part of the premiumizer. Without knowing this project I cannot be of further help where to exactly.

GenError
  • 885
  • 9
  • 25
  • Thanks a lot for your message, yes exactly I just tried to put the code inside there. Premiumizer script is Python 3.x based so thanks for the hint on how to use the print command there. Sadly I do not understand much python code yet (I am at the very start of working with it, just started looking into this a few hours ago for the first time) To start the script I need to start this script: https://github.com/piejanssens/premiumizer/blob/master/premiumizer/premiumizer.py So I assume the initialization will happen somewhere in it probably in the beginning of the file. Could you – Salexes Mar 13 '19 at 03:11
  • maybe give me a hint what to look for? (Or maybe if that is not to much to ask to check the linked .py file for the correct line for me to insert this code) – Salexes Mar 13 '19 at 03:12
  • Before line 2355 (which starts the webserver) would be an option. The project uses flask (http://flask.pocoo.org/) as framework, might be worth reading their documentation if you want to dive deeper. – GenError Mar 13 '19 at 03:42
  • Can I just put the "Test" code inside there without it closing immedeatly again ? – Salexes Mar 13 '19 at 03:57
  • Yes, if you paste the code above between line 2354 and 2355 and make the correct indentation the server should start and the task icon appear as long as the server is running. – GenError Mar 13 '19 at 04:01
  • Sadly it does not work and still closes the console directly. I think I am doing something wrong – Salexes Mar 13 '19 at 04:07
  • Try to run the script from a console. In windows open the start menu and type cmd, you will get the console. Start it and navigate to the folder where your premiumizer.py is and run it with `python premiumizer.py`. There should be a bunch of output, check if you get an error message at the end and start from there. – GenError Mar 13 '19 at 04:11