« »
2008-11-14Python

232

Python:WxPython实现windows托盘图标(tray)

  1. import wx
  2. import string
  3. import sys
  4.  
  5. ID_ICON_TIMER = wx.NewId()

##
# The IconBar class
#

  1. class IconBar:
  2.  
  3.     ##
  4.     # \brief the constructor default left: red, default right: green
  5.     #
  6.     def __init__(self,l_off=[128,0,0],l_on=[255,0,0],r_off=[0,128,0],r_on=[0,255,0]):
  7.         self.s_line = "\xff\xff\xff"+"\0"*45
  8.         self.s_border = "\xff\xff\xff\0\0\0"
  9.         self.s_point = "\0"*3
  10.         self.sl_off = string.join(map(chr,l_off),'')*6
  11.         self.sl_on = string.join(map(chr,l_on),'')*6
  12.         self.sr_off = string.join(map(chr,r_off),'')*6
  13.         self.sr_on = string.join(map(chr,r_on),'')*6
  14.  
  15.     ##
  16.     # \brief gets a new icon with 0 <= l,r <= 5
  17.     #
  18.     def Get(self,l,r):
  19.         s=""+self.s_line
  20.         for i in range(5):
  21.             if i<(5-l):
  22.                 sl = self.sl_off
  23.             else:
  24.                 sl = self.sl_on
  25.  
  26.             if i<(5-r):
  27.                 sr = self.sr_off
  28.             else:
  29.                 sr = self.sr_on
  30.  
  31.             s+=self.s_border+sl+self.s_point+sr+self.s_point
  32.             s+=self.s_border+sl+self.s_point+sr+self.s_point
  33.             s+=self.s_line
  34.  
  35.         image = wx.EmptyImage(16,16)
  36.         image.SetData(s)
  37.  
  38.         bmp = image.ConvertToBitmap()
  39.         bmp.SetMask(wx.Mask(bmp, wx.WHITE)) #sets the transparency colour to white
  40.  
  41.         icon = wx.EmptyIcon()
  42.         icon.CopyFromBitmap(bmp)
  43.  
  44.         return icon
  45.  
  46. ##
  47. # The TaskBarIcon class
  48. #
  49. class MyTaskBarIcon(wx.TaskBarIcon):
  50.  
  51.     l = 0
  52.     r = 0
  53.  
  54.     ##
  55.     # \brief the constructor
  56.     #
  57.     def __init__(self, frame):
  58.         wx.TaskBarIcon.__init__(self)
  59.         self.frame = frame
  60.         self.IconBar = IconBar((127,127,0),(255,255,0),(0,127,127),(0,255,255))
  61.         self.SetIconBar(self.l,self.r)
  62.  
  63.     ##
  64.     # \brief sets the icon timer
  65.     #
  66.     def SetIconTimer(self):
  67.         self.icon_timer = wx.Timer(self, ID_ICON_TIMER)
  68.         wx.EVT_TIMER(self, ID_ICON_TIMER, self.BlinkIcon)
  69.         self.icon_timer.Start(100)
  70.  
  71.     ##
  72.     # \brief blinks the icon and updates self.l and self.r
  73.     #
  74.     def BlinkIcon(self, event):
  75.         self.SetIconBar(self.l,self.r)
  76.         self.l += 1
  77.         if self.l > 5:
  78.             self.l = 0
  79.             self.r += 1
  80.             if self.r > 5:
  81.                 self.r = 0
  82.     ##
  83.     # \brief sets the icon bar and a message
  84.     #
  85.     def SetIconBar(self,l,r):
  86.         icon = self.IconBar.Get(l,r)
  87.         self.SetIcon(icon, "L:%d,R:%d"%(l,r))
  88.  
  89. ##
  90. # The task bar application
  91. #
  92. class TaskBarApp(wx.Frame):
  93.  
  94.     ##
  95.     # \brief the constructor
  96.     #
  97.     def __init__(self, parent, id, title):
  98.         wx.Frame.__init__(self, parent, -1, title, size = (1, 1),
  99.             style=wx.FRAME_NO_TASKBAR|wx.NO_FULL_REPAINT_ON_RESIZE)
  100.  
  101.         self.tbicon = MyTaskBarIcon(self)
  102.         self.tbicon.SetIconTimer()
  103.  
  104.         self.Show(True)
  105.  
  106. ##
  107. # The main application wx.App class
  108. #
  109. class MyApp(wx.App):
  110.     def OnInit(self):
  111.         frame = TaskBarApp(None, -1, ' ')
  112.         frame.Center(wx.BOTH)
  113.         frame.Show(False)
  114.         return True
  115.  
  116. def main(argv=None):
  117.     if argv is None:
  118.         argv = sys.argv
  119.  
  120.     app = MyApp(0)
  121.     app.MainLoop()
  122.  
  123. if __name__ == '__main__':
  124.     main()

您还可能感兴趣的内容

日志信息 »

该日志于2008-11-14 05:06由 admin 发表在Python分类下, 你可以发表评论。除了可以将这个日志以保留源地址及作者的情况下引用到你的网站或博客,还可以通过RSS 2.0订阅这个日志的所有评论。

没有评论

发表评论 »


返回顶部