A Notify Icon

From IronPython Cookbook

Two examples of using the Windows Forms NotifyIcon class to inform users of events. Both need an 'icon.ico' file to show in the system tray.


First Example

The first example, for a "PopUp-Balloon for event watching" comes from Marc, The Powershell Guy.

A NotifyIcon

This code just uses a Thread.Sleep(9000), so that it exits automatically after nine seconds. The NotifyIcon code is launched on another thread.

import clr
clr.AddReference("System.Windows.Forms")
clr.AddReference("System.Drawing")

from System.Windows.Forms import (
    Form, FormWindowState,
    NotifyIcon, ToolTipIcon
)

from System.IO import FileSystemWatcher

from System.Drawing import Icon

from System.Threading import (
    ApartmentState, Thread, ThreadStart
)


path = r"c:\path_to_watch"

def thread_proc():
    f = Form()
    f.ShowInTaskbar = False
    f.WindowState = FormWindowState.Minimized
    
    n = NotifyIcon()
    n.Icon = Icon(r"C:\path_to_icon\icon.ico")
    n.Text = "Watching" + path
    
    w = FileSystemWatcher()
    w.Path = path
    def handle(w, a): 
        n.ShowBalloonTip(10, str(a.ChangeType), 
                         a.FullPath, ToolTipIcon.Warning)
    w.Changed += handle
    w.Created += handle
    w.Deleted += handle
    w.EnableRaisingEvents = True
    
    n.Visible = True
    def handle(*args): 
        f.Activate()
    f.Shown += handle
    f.ShowDialog()


t = Thread(ThreadStart(thread_proc))
t.ApartmentState = ApartmentState.STA
t.Start() 

# Sleep for 90 seconds so that you can 
# see the notify icon in action
Thread.Sleep(90000)

# Then die...
t.Abort()

Second Example

The second example, perhaps a more conventional windows forms example comes from Andrzej Krzywda. This NotifyIcon has a ContextMenu, so that you can exit it.

His blog entry has an explanation of the code:


import clr

clr.AddReference("System.Windows.Forms")
clr.AddReference("System.Drawing")
from System.Drawing import Icon
from System.Windows.Forms import (Application, ContextMenu, 
                                  MenuItem, NotifyIcon, Timer)


class Main(object):

    def __init__(self):
        self.initNotifyIcon()
        timer = Timer()
        timer.Interval = 60000
        timer.Tick += self.onTick
        timer.Start()
    

    def initNotifyIcon(self):
        self.notifyIcon = NotifyIcon()
        self.notifyIcon.Icon = Icon("test.ico")
        self.notifyIcon.Visible = True
        self.notifyIcon.ContextMenu = self.initContextMenu()

    
    def onTick(self, sender, event):
        self.notifyIcon.BalloonTipTitle = "Hello, I'm IronPython"
        self.notifyIcon.BalloonTipText = "Who are you?"
        self.notifyIcon.ShowBalloonTip(1000)

        
    def initContextMenu(self):
        contextMenu = ContextMenu()
        exitMenuItem = MenuItem("Exit")
        exitMenuItem.Click += self.onExit
        contextMenu.MenuItems.Add(exitMenuItem)
        return contextMenu
        

    def onExit(self, sender, event):
        self.notifyIcon.Visible = False
        Application.Exit()     
   
        
if __name__ == "__main__":
    main = Main()
    Application.Run()


Back to Contents.

TOOLBOX
LANGUAGES