Rom*_*dgz 34 python system-tray popup
我想知道如何使用python创建系统托盘弹出消息.我已经在很多软件中看到了这些软件,但很难找到资源来轻松地使用任何语言.有人知道在Python中这样做的一些库吗?
hal*_*lex 43
在pywin32库的帮助下,您可以使用我在此处找到的以下示例代码:
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], "balloontip.ico" ))
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, "tooltip")
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("Title for popup", "This is the popup's message")
Run Code Online (Sandbox Code Playgroud)
Epo*_*poc 22
我最近使用Plyer包来创建跨平台通知,没有痛苦,使用Notification外观(它有许多其他值得一看的有趣的东西).
非常好用:
from plyer import notification
notification.notify(
title='Here is the title',
message='Here is the message',
app_name='Here is the application name',
app_icon='path/to/the/icon.png'
)
Run Code Online (Sandbox Code Playgroud)
视窗
现在有一种使用Python/Winrt实现这一目标的官方方法,github 解释了如何将 UWP API 映射到 python API。
通过遵循官方 UWP 文档,我成功地显示了一个小通知,该通知也出现在 Windows 通知中心中:
import winrt.windows.ui.notifications as notifications
import winrt.windows.data.xml.dom as dom
#create notifier
nManager = notifications.ToastNotificationManager
notifier = nManager.create_toast_notifier();
#define your notification as string
tString = """
<toast>
<visual>
<binding template='ToastGeneric'>
<text>Sample toast</text>
<text>Sample content</text>
</binding>
</visual>
</toast>
"""
#convert notification to an XmlDocument
xDoc = dom.XmlDocument()
xDoc.load_xml(tString)
#display notification
notifier.show(notifications.ToastNotification(xDoc))
Run Code Online (Sandbox Code Playgroud)
设置仅限于库的安装
pip install winrt
Run Code Online (Sandbox Code Playgroud)
要求
Windows 10 2018 年 10 月更新或更高版本
适用于 Windows 的 Python,版本 3.7 或更高版本
pip,版本 19 或更高版本
奖励 macOS
我还找到了一种在 macOS 中使用 AppleScript 执行此操作的方法,以下代码的目标是构建将通过 python 执行的 AppleScript 代码os.system
import os
def displayNotification(message,title=None,subtitle=None,soundname=None):
"""
Display an OSX notification with message title an subtitle
sounds are located in /System/Library/Sounds or ~/Library/Sounds
"""
titlePart = ''
if(not title is None):
titlePart = 'with title "{0}"'.format(title)
subtitlePart = ''
if(not subtitle is None):
subtitlePart = 'subtitle "{0}"'.format(subtitle)
soundnamePart = ''
if(not soundname is None):
soundnamePart = 'sound name "{0}"'.format(soundname)
appleScriptNotification = 'display notification "{0}" {1} {2} {3}'.format(message,titlePart,subtitlePart,soundnamePart)
os.system("osascript -e '{0}'".format(appleScriptNotification))
Run Code Online (Sandbox Code Playgroud)
使用as:
displayNotification("message","title","subtitle","Pop")
Run Code Online (Sandbox Code Playgroud)
最后的笔记
我将前面的所有代码总结为两个要点
小智 7
这是使用python在Windows 10上显示通知的简单方法:module win10toast。
要求:
安装:
>> pip install win10toast
Run Code Online (Sandbox Code Playgroud)
范例:
from win10toast import ToastNotifier
toaster = ToastNotifier()
toaster.show_toast("Demo notification",
"Hello world",
duration=10)
Run Code Online (Sandbox Code Playgroud)
您将需要使用第三方python GUI库或pywin32库.与python捆绑在一起的GUI工具包TkInter不支持系统托盘弹出窗口.
支持使用系统托盘的多形式中性库:
支持使用系统托盘的Windows特定库:
在Windows上使用wxpython弹出系统托盘的信息/示例:
小智 5
在 Linux 系统中,您可以使用内置命令notify-send。
ntfy 库可用于发送推送通知。
安装:
sudo pip install ntfy
Run Code Online (Sandbox Code Playgroud)
例子:
ntfy send "your message!"
ntfy -t "your custom title" send "your message"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
38326 次 |
| 最近记录: |