tray icon in Windows

Gerhard Häring gerhard.haering at opus-gmbh.net
Wed Jan 22 08:00:43 EST 2003


fama1979 <fama1979 at email.it> wrote:

> I'm sorry if i'm newbie,
> how can i have a icon to the trayicon (near timeclock) of windows?

You can use wxPython to do that. Or, alternatively, you can use the win32api
module from win32all (but doing it the win32api way is a major pain in the
ass).

wxPython less so ;-)

Here's some rough code for a biff-like IMAP mail checker I've once written
using wxPython (I mainly adapted the notify icon stuff from the wxPython demo):

This is not the easiest problem to solve, espeically for a newbie.

HTH,

Gerhard

Ugly code follows:

#!/bin/env python
import sys, os, time, imaplib
from   wxPython.wx import *
import images

NOMAIL = 0
GOTMAIL = 1

class MainWindow(wxFrame):
    def __init__(self, parent, id, title):
        wxFrame.__init__(self, parent, -1, title, size = (800, 600),
                         style=wxDEFAULT_FRAME_STYLE|wxNO_FULL_REPAINT_ON_RESIZE) #|wxFRAME_NO_TASKBAR )

        #icon = wxIconFromXPMData(images.getMondrianData())
        #self.SetIcon(icon)

        # setup a taskbar icon, and catch some events from it
        icon = wxIcon("normal.ico", wxBITMAP_TYPE_ICO)
        self.tbicon = wxTaskBarIcon()
        self.tbicon.SetIcon(icon, "Mailwatcher")
        EVT_TASKBAR_LEFT_DCLICK(self.tbicon, self.OnTaskBarActivate)
        EVT_TASKBAR_RIGHT_UP(self.tbicon, self.OnTaskBarMenu)
        EVT_MENU(self.tbicon, self.TBMENU_SHOW, self.OnTaskBarActivate)
        EVT_MENU(self.tbicon, self.TBMENU_HIDE, self.OnTaskBarHide)
        EVT_MENU(self.tbicon, self.TBMENU_CLOSE, self.OnTaskBarClose)
        ID_Start = wxNewId()
        self.b = wxButton(self, ID_Start, ' Panel A ')
        ID_TIMER = wxNewId()
        self.timer = wxTimer(self, ID_TIMER)
        EVT_TIMER(self, ID_TIMER, self.OnTimer)

        EVT_BUTTON(self, ID_Start, self.OnStart)
        self.timer.Start(5000)
        
        self.status = NOMAIL
        
        self.imap = imaplib.IMAP4("myhost")
        self.imap.login("myuser", "mypass")
        self.imap.select()

        self.Show(false)

    def OnStart(self, event):
        pass
        #wxBell()

    def GotMail(self):
        if self.status != GOTMAIL:
            icon = wxIcon("gotmail.ico", wxBITMAP_TYPE_ICO)

            self.tbicon.SetIcon(icon, "Mailwatcher - Got Mail")
            self.status = GOTMAIL
            wxBell()

    def SetNormal(self):
        if self.status != NOMAIL:
            icon = wxIcon("normal.ico", wxBITMAP_TYPE_ICO)
            self.tbicon.SetIcon(icon, "Mailwatcher - No Mail")
            self.status = NOMAIL
    
    def CheckMail(self):
        typ, data = self.imap.search(None, '(UNSEEN UNDELETED)')
        if len(data[0].split()) > 0:
            # New mail - let's filter first
            os.spawnl(os.P_WAIT, r'D:\cygwin\bin\bash.exe',
                r'D:\cygwin\bin\bash.exe', '--login -c "cd /home/Gerhard.Haering/src/imapfilter; ./imapfilter.py"')

        self.imap.expunge()
        typ, data = self.imap.search(None, '(ALL)')
        size = len(data[0].split())

        return size > 0

    def OnTimer(self, event):
        if self.CheckMail():
            self.GotMail()
        else:
            self.SetNormal()
        wxGetApp().ProcessIdle()
    
    def OnCloseWindow(self, event):
        self.dying = true
        self.window = None
        self.mainmenu = None
        if hasattr(self, "tbicon"):
            del self.tbicon
        self.Destroy()


    def OnTaskBarActivate(self, evt):
        #if self.IsIconized():
        #    self.Iconize(false)
        #if not self.IsShown():
        #    self.Show(true)
        os.startfile(r'mutt.lnk')
        self.SetNormal()
        wxGetApp().ProcessIdle()
        #self.Raise()

    TBMENU_SHOW = 1000
    TBMENU_HIDE = 1001
    TBMENU_CLOSE = 1002

    def OnTaskBarMenu(self, evt):
        menu = wxMenu()
        #menu.Append(self.TBMENU_HIDE, "&Hide Mailwatcher")
        #menu.Append(self.TBMENU_SHOW, "&Show Mailwatcher")
        menu.Append(self.TBMENU_CLOSE,   "&Close")
        self.tbicon.PopupMenu(menu)
        menu.Destroy()
        wxGetApp().ProcessIdle()

    def OnTaskBarClose(self, evt):
        self.Close()

        # because of the way wxTaskBarIcon.PopupMenu is implemented we have to
        # prod the main idle handler a bit to get the window to actually close
        wxGetApp().ProcessIdle()

    def OnTaskBarHide(self, evt):
        self.Show(False)

        # because of the way wxTaskBarIcon.PopupMenu is implemented we have to
        # prod the main idle handler a bit to get the window to actually close
        wxGetApp().ProcessIdle()

    def OnIconfiy(self, evt):
        self.Show(false)
        evt.Skip()

    def OnMaximize(self, evt):
        evt.Skip()

class MyApp(wxApp):
    def OnInit(self):
        wxInitAllImageHandlers()
        frame = MainWindow(None, -1, "Mailwatcher")
        #frame.Show(true)
        #self.SetTopWindow(frame)

        return true

def main():
    app = MyApp(0)
    app.MainLoop()

if __name__ == '__main__':
    main()





More information about the Python-list mailing list