【发布时间】:2014-12-19 20:13:24
【问题描述】:
我有一个 Python 2.7 Windows 应用程序,它带有一些 Wx (3.0) gui 功能,可以在 Windows 7 中完美运行......但我无法让系统托盘图标气球提示在 Windows XP 中工作。我的公司仍在更新大约 15,000 台 PC,并且要到明年年中才能完成,所以我想在此之前将我的软件推出多个版本。
我已经搜索了大约一个星期,似乎无法找到与我自己相同的情况,没有完全拆除我的整个程序并重新开始(也许这就是我需要做的?)......另一个解决方案涉及从 win32gui 重新创建所有窗口,而不是应用程序基础的 wx 构建。 (我有一个弹出菜单,以及基于系统托盘菜单选择打开的多个面板)。我已经仔细检查了我的所有模块都是 32 位的,但也许我还缺少其他东西?
我从我在这里找到的其他示例中提取了一些元素,以使其在 W7 中运行:
def ShowBalloon(self, title, text, msec = 15, flags = 0):
"""
Show Balloon tooltip
@param title - Title for balloon tooltip
@param msg - Balloon tooltip text
@param msec - Timeout for balloon tooltip, in milliseconds
@param flags - one of wx.ICON_INFORMATION, wx.ICON_WARNING, wx.ICON_ERROR
"""
self.icon = wx.Icon(os.getcwd() + "\Icons\Main.ico", wx.BITMAP_TYPE_ICO)
if WIN32 and self.IsIconInstalled():
try:
self.__SetBalloonTip(self.icon.GetHandle(), title, text, msec, flags)
except Exception as e:
pass # print(e) Silent error
def __SetBalloonTip(self, hicon, title, msg, msec, flags):
# translate flags
print flags
infoFlags = 0
self.icon = wx.Icon(os.getcwd() + "\Icons\Main.ico", wx.BITMAP_TYPE_ICO)
if flags & wx.ICON_INFORMATION:
infoFlags |= win32gui.NIIF_INFO
elif flags & wx.ICON_WARNING:
infoFlags |= win32gui.NIIF_WARNING
elif flags & wx.ICON_ERROR:
infoFlags |= win32gui.NIIF_ERROR
# Show balloon
lpdata = (self.__GetIconHandle(), # hWnd
99, # ID
win32gui.NIF_MESSAGE|win32gui.NIF_INFO|win32gui.NIF_ICON, # flags: Combination of NIF_* flags
0, # CallbackMessage: Message id to be pass to hWnd when processing messages
hicon, # hIcon: Handle to the icon to be displayed
'', # Tip: Tooltip text
msg, # Info: Balloon tooltip text
msec, # Timeout: Timeout for balloon tooltip, in milliseconds
title, # InfoTitle: Title for balloon tooltip
infoFlags # InfoFlags: Combination of NIIF_* flags
)
win32gui.Shell_NotifyIcon(win32gui.NIM_MODIFY, lpdata)
self.SetIcon(self.icon, self.tooltip) # Hack: because we have no access to the real CallbackMessage value
def __GetIconHandle(self):
"""
Find the icon window.
This is ugly but for now there is no way to find this window directly from wx
"""
if not hasattr(self, "_chwnd"):
try:
for handle in wx.GetTopLevelWindows():
if handle.GetWindowStyle():
continue
handle = handle.GetHandle()
if len(win32gui.GetWindowText(handle)) == 0:
self._chwnd = handle
break
if not hasattr(self, "_chwnd"):
raise Exception
except:
raise Exception, "Icon window not found"
return self._chwnd
我在 XP 中没有收到任何错误或异常,但气球提示不会显示。根据收到的传入 TCP 消息调用提示,并显示传入消息的详细信息。我是新手,先谢谢了!
【问题讨论】:
-
我认为问题可能出在超时值上,它的行为在 Vista 之前有所不同。查看 MSDN 的备注部分:msdn.microsoft.com/en-us/library/windows/desktop/…
标签: python windows python-2.7 winapi