首页 > 解决方案 > 动态系统托盘文本(Python 3)

问题描述

我正在尝试在系统托盘中显示一个动态文本(这将是 2 个数字(从 1 到 100)每 2 分钟更改一次)。

我发现这个脚本是一个起点(但我没有承诺!)。

但我得到这个错误:

TypeError: Image.SetData(): arguments did not match any overloaded call:
  overload 1: argument 1 has unexpected type 'str'
  overload 2: argument 1 has unexpected type 'str'
OnInit returned false, exiting...

代码的相关部分是:

def Get(self,l,r):
        s=""+self.s_line
        for i in range(5):
            if i<(5-l):
                sl = self.sl_off
            else:
                sl = self.sl_on

            if i<(5-r):
                sr = self.sr_off
            else:
                sr = self.sr_on

            s+=self.s_border+sl+self.s_point+sr+self.s_point
            s+=self.s_border+sl+self.s_point+sr+self.s_point
            s+=self.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)

        return icon

我通过添加import wx.adv和替换 2wx.TaskBarIcon来添加更新脚本wx.adv.TaskBarIcon

我在使用 Python 3.6 的 Windows 10 上

标签: pythonpython-3.xwxpythonsystem-tray

解决方案


我找到了另一种使用Pillowinfi.systray

# text to image : Pillow (https://pillow.readthedocs.io/en/latest/handbook/tutorial.html - simple code sample:  https://code-maven.com/create-images-with-python-pil-pillow)
# icon in systray : infi.systray (https://github.com/Infinidat/infi.systray and https://stackoverflow.com/a/54082417/3154274)

# inspired by https://www.reddit.com/r/learnpython/comments/a7utd7/pystray_python_system_tray_icon_app/

# install PIL :  pip install Pillow
# install infi.systray : pip install infi.systray

from infi.systray import SysTrayIcon
from PIL import Image, ImageDraw,ImageFont
import time

image= "pil_text.ico"
n=1
while True:
    # create image
    img = Image.new('RGBA', (50, 50), color = (255, 255, 255, 90))  # color background =  white  with transparency
    d = ImageDraw.Draw(img)
    d.rectangle([(0, 40), (50, 50)], fill=(39, 112, 229), outline=None)  #  color = blue

    #add text to the image
    font_type  = ImageFont.truetype("arial.ttf", 25)
    a= n*10
    b = n*20
    d.text((0,0), f"{a}\n{b}", fill=(255,255,0), font = font_type)

    img.save(image)


    # display image in systray 
    if n==1:
        systray = SysTrayIcon(image, "Systray")
        systray.start()
    else:
        systray.update(icon=image)
    time.sleep(5)
    n+=1
systray.shutdown()

在此处输入图像描述


推荐阅读