Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make an menu bar (system tray) app for OSX in Python?

Tags:

After having spent quite some time looking at ways to an app for the menu bar we're close to admit defeat.

We are basically just looking for an example/pointer on how to create an app that will put itself in the menu bar (the small icons next to the clock), and have a menu. Nothing fancy at all.

It feels like something that should be very easy to do, but we haven't been able to find an example that works.

Maybe it's not possible with Python? Does anyone know how others do it?

like image 955
freeall Avatar asked Dec 17 '11 12:12

freeall


People also ask

How do I create a system tray application in Python?

To create a System Tray icon of a tkinter application, we can use pystray module in Python. It has many inbuilt functions and methods that can be used to configure the system tray icon of the application. To install pystray in your machine, you can type "pip install pystray" command in your shell or command prompt.


2 Answers

An option would be to use rumps which provides a level of abstraction on top of PyObjC. I wrote it specifically for quickly generating these types of simple status bar apps.

I hope that this could help a few people out there looking for a simple, semantic solution!

A short example snippet follows. Decorators are used for registering functions as callbacks for click events and timers. There is also support for 10.8 notifications.

import rumps  class AwesomeStatusBarApp(rumps.App):     def __init__(self):         super(AwesomeStatusBarApp, self).__init__("Awesome App")         self.menu = ["Preferences", "Silly button", "Say hi"]      @rumps.clicked("Preferences")     def prefs(self, _):         rumps.alert("jk! no preferences available!")      @rumps.clicked("Silly button")     def onoff(self, sender):         sender.state = not sender.state      @rumps.clicked("Say hi")     def sayhi(self, _):         rumps.notification("Awesome title", "amazing subtitle", "hi!!1")  if __name__ == "__main__":     AwesomeStatusBarApp().run() 

pic

like image 64
Jared Avatar answered Nov 17 '22 00:11

Jared


wxPython won't be able to add a taskbar item. You can do this by instead using PyObjC like so:

from AppKit import NSStatusBar status_item = NSStatusBar.systemStatusBar().statusItemWithLength_(-1) #NSVariableStatusItemLength status_item.setImage_(<NSImage instance to status icon>) 

Just refer to the NSStatusItem class reference to do stuff to the item, e.g. add a menu, change the highlight image, etc.

like image 45
maranas Avatar answered Nov 17 '22 01:11

maranas