gpt4 book ai didi

python - 如何使用pywin32添加系统托盘图像

转载 作者:行者123 更新时间:2023-11-28 19:23:39 25 4
gpt4 key购买 nike

在代码中,默认的可执行镜像显示在任务栏系统托盘中。如何在任务栏系统托盘中分配我选择的图像?在控制台中打印 iconPathName 后,它显示了图像所在的路径,但是当我运行代码时图像没有显示在任务栏中

from win32api import *
from win32gui import *
import win32con
import sys, os
import struct
import time

class WindowsBalloonTip:
def __init__(self, title, msg):
message_map = {
win32con.WM_DESTROY: self.OnDestroy,
}
# Register the Window class.
wc = WNDCLASS()
hinst = wc.hInstance = GetModuleHandle(None)
wc.lpszClassName = "PythonTaskbar"
wc.lpfnWndProc = message_map # could also specify a wndproc.
classAtom = RegisterClass(wc)
# Create the Window.
style = win32con.WS_OVERLAPPED | win32con.WS_SYSMENU
self.hwnd = CreateWindow( classAtom, "Taskbar", style, \
0, 0, win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT, \
0, 0, hinst, None)
UpdateWindow(self.hwnd)
iconPathName = os.path.abspath(os.path.join( sys.path[0], "cc.png" ))
print iconPathName
icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE
try:
hicon = LoadImage(hinst, iconPathName, \
win32con.IMAGE_ICON, 0, 0, icon_flags)
except:
hicon = LoadIcon(0, win32con.IDI_APPLICATION)
flags = NIF_ICON | NIF_MESSAGE | NIF_TIP
nid = (self.hwnd, 0, flags, win32con.WM_USER+20, hicon, "cloud")
Shell_NotifyIcon(NIM_ADD, nid)
Shell_NotifyIcon(NIM_MODIFY, \
(self.hwnd, 0, NIF_INFO, win32con.WM_USER+20,\
hicon, "Balloon tooltip",msg,200,title))
# self.show_balloon(title, msg)
time.sleep(10)
DestroyWindow(self.hwnd)
def OnDestroy(self, hwnd, msg, wparam, lparam):
nid = (self.hwnd, 0)
Shell_NotifyIcon(NIM_DELETE, nid)
PostQuitMessage(0) # Terminate the app.

def balloon_tip(title, msg):
w=WindowsBalloonTip(title, msg)

if __name__ == '__main__':
balloon_tip("Opening from cloud", "File open")

最佳答案

您的图标可能格式不正确。我相信您必须使用 ICO 文件而不是 PNG。其余代码看起来没问题。

如果您删除 LoadImage 周围的 try/except block ,您应该能够看到这个问题(或者它可能返回 None?)

关于python - 如何使用pywin32添加系统托盘图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18580404/

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