#! /usr/bin/env python #-*- coding: utf-8 -*- # Classe permettant l'affichage d'une icone dans la barre des taches # avec menu # avec choix de l'icone ou choix d'une grandeur à monitorer import wx import string class Tray(wx.TaskBarIcon): """ Classe pour l'affichage de l'icone et des menus dans la barre des taches. """ #Entrées du menu TBMENU_CREATE = [] def __init__(self, app): wx.TaskBarIcon.__init__(self) self.app = app self.menu_tmp = [] #liaisons Boutons-Méthodes (actions en dur) self.Bind(wx.EVT_TASKBAR_LEFT_DOWN, self.OnTaskBarActivate) def MenuAddItem(self, nom, action): """ Add an item to the menu and link it to corresponding method """ if action != None : new_item = wx.NewId() self.menu_tmp.append({nom:new_item}) self.Bind(wx.EVT_MENU, action, id=new_item) else: self.menu_tmp.append({nom:None}) def CreatePopupMenu(self): """ Method called when we need to display the menu """ menu = wx.Menu() for item in self.menu_tmp: for nom,action in item.items(): if action!=None: menu.Append(action, nom) else: menu.AppendSeparator() return menu def ChangeIcon(self, imgpath): """ Change taskbar icon """ img = wx.Image(imgpath,wx.BITMAP_TYPE_PNG) bmp = img.ConvertToBitmap() # bmp.SetMask(wx.Mask(bmp, wx.Colour().Alpha())) icon = wx.IconFromBitmap(img.ConvertToBitmap()) icon = wx.EmptyIcon() icon.CopyFromBitmap(bmp) self.SetIcon(icon, "Temp: %d °C" % self.temp) def IconMonitor(self, l=2, r=4): l_off=[128,0,0] # couleurs l_on=[255,0,0] r_off=[0,128,0] r_on=[0,255,0] s_line = "\xff\xff\xff"+"\0"*45 s_border = "\xff\xff\xff\0\0\0" s_point = "\0"*3 sl_off = string.join(map(chr,l_off),'')*6 sl_on = string.join(map(chr,l_on),'')*6 sr_off = string.join(map(chr,r_off),'')*6 sr_on = string.join(map(chr,r_on),'')*6 s=""+s_line for i in range(5): if i<(5-l): sl = sl_off else: sl = sl_on if i<(5-r): sr = sr_off else: sr = sr_on s+=s_border+sl+s_point+sr+s_point s+=s_border+sl+s_point+sr+s_point s+=s_line image = wx.EmptyImage(16,16) image.SetData(s) bmp = image.ConvertToBitmap() bmp.SetMask(wx.Mask(bmp, wx.WHITE)) #sets the transparency colour to white icon = wx.EmptyIcon() icon.CopyFromBitmap(bmp) self.SetIcon(icon, "Temp: %d°C" % self.temp) def SetIconTimer(self): self.icon_timer = wx.Timer(self, ID_ICON_TIMER) wx.EVT_TIMER(self, ID_ICON_TIMER, self.BlinkIcon) self.icon_timer.Start(100) def BlinkIcon(self, event): self.CheckTemp() self.MonitorIcon()