Salexes
Salexes

Reputation: 187

How to add systemtray to python script?

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 ?

Upvotes: 1

Views: 1580

Answers (1)

GenError
GenError

Reputation: 1186

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.

Upvotes: 1

Related Questions