gpt4 book ai didi

python - 动态系统托盘文本(Python 3)

转载 作者:行者123 更新时间:2023-12-05 09:13:53 27 4
gpt4 key购买 nike

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

我找到了这个 script作为起点(但我不 promise !)。

但是我得到这个错误:

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 并将 2 wx.TaskBarIcon 替换为 wx.adv.TaskBarIcon 来更新脚本.

我在 Windows 10 和 Python 3.6 上

最佳答案

我找到了另一种使用 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()

enter image description here

关于python - 动态系统托盘文本(Python 3),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55381039/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com