Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quick and easy: trayicon with python?

I'd just need a quick example on how to easily put an icon with python on my systray. This means: I run the program, no window shows up, just a tray icon (I've got a png file) shows up in the systray and when I right-click on it a menu appears with some options (and when I click on an option, a function is run). Is that possible? I don't need any window at all...

Examples / code snippets are REALLY appreciated! :D

like image 539
Marco Moschettini Avatar asked Jun 17 '11 17:06

Marco Moschettini


People also ask

How do I create an icon tray in python?

To create a System Tray icon, you can follow these steps, Import the required libraries - Pystray, Python PIL or Pillow. Define a function hide_window() to withdraw window and provide the icon to the system tray. Add and define two menu items, "Show" and "Quit".


1 Answers

For Windows & Gnome

Here ya go! wxPython is the bomb. Adapted from the source of my Feed Notifier application.

import wx  TRAY_TOOLTIP = 'System Tray Demo' TRAY_ICON = 'icon.png'   def create_menu_item(menu, label, func):     item = wx.MenuItem(menu, -1, label)     menu.Bind(wx.EVT_MENU, func, id=item.GetId())     menu.AppendItem(item)     return item   class TaskBarIcon(wx.TaskBarIcon):     def __init__(self):         super(TaskBarIcon, self).__init__()         self.set_icon(TRAY_ICON)         self.Bind(wx.EVT_TASKBAR_LEFT_DOWN, self.on_left_down)      def CreatePopupMenu(self):         menu = wx.Menu()         create_menu_item(menu, 'Say Hello', self.on_hello)         menu.AppendSeparator()         create_menu_item(menu, 'Exit', self.on_exit)         return menu      def set_icon(self, path):         icon = wx.IconFromBitmap(wx.Bitmap(path))         self.SetIcon(icon, TRAY_TOOLTIP)      def on_left_down(self, event):         print 'Tray icon was left-clicked.'      def on_hello(self, event):         print 'Hello, world!'      def on_exit(self, event):         wx.CallAfter(self.Destroy)   def main():     app = wx.PySimpleApp()     TaskBarIcon()     app.MainLoop()   if __name__ == '__main__':     main() 
like image 156
FogleBird Avatar answered Oct 01 '22 07:10

FogleBird