python - 动态系统托盘文本(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 上
解决方案
我找到了另一种使用Pillow
和infi.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()
推荐阅读
- android - 如何在地图视图(Android)中显示纬度和经度?
- php - 如何防止 Quickbooks Online API 中的重复发票?
- arrays - 如何使用另一个相同大小的数组索引 numpy 数组
- azure - 使用 Connect-AzAccount 时如何防止“选择帐户”交互式工作流
- mongodb - 为什么我的 Getter 函数不能与 Mongoose 一起使用?
- multithreading - cuda内核'volta_sgemm_128x32_nn'是什么意思?
- logstash - 改进我的多行 javastack 日志的工作 grok 模式
- php - PHP练习函数($result)不能调用
- laravel - Laravel 8 Vue 包找不到声明模块
- angular - Angular 和基于 Spring 的 Web 应用程序中的验证码实现