Ich würde gerne wissen, wie man eine System-Tray-Popup-Nachricht mit Python erstellen. Ich habe diese in vielen Soft-ware gesehen, aber immer noch schwierig, Ressourcen zu finden, um es leicht mit jeder Sprache zu tun. Kennt jemand eine Bibliothek dafür in Python?Wie erstellt man eine System-Tray-Popup-Nachricht mit Python? (Windows)
Antwort
Mit Hilfe der pywin32
library Sie den folgenden Beispielcode I here gefunden verwenden:
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")
Sie benötigen eine 3rd-Party-Python-GUI-Bibliothek oder die pywin32 Bibliothek verwenden. TkInter, das GUI-Toolkit, das mit Python ausgeliefert wird, unterstützt keine Systemtray-Popups.
Multi neutrale Bibliotheken, die mit dem System-Tray unterstützen das Arbeiten:
- WxPython
- PyGTK
- PyQT
Windows-spezifische Bibliothek, die mit dem System-Tray unterstützt Arbeits:
- py win32
Information/Beispiel für System-Tray-Pop-ups mit wxpython an Fenstern:
Ok, irgendein Beispiel, wie es mit WxPython zu tun? –
@RomanRdgz Werfen Sie einen Blick auf [ToasterBox] (http://xoomer.virgilio.it/infinity77/main/freeware.html#toasterbox), die ich in http://stackoverflow.com/questions/2240674/cross-platform gefunden habe -desktop-notifier-in-python. – halex
habe ich vor kurzem die Plyer Paket-Benachrichtigungen Cross-Plattform ohne Schmerzen zu schaffen, die unter Verwendung von Notification
Fassade (es gibt viele andere interessante Dinge, die einen Blick wert sind).
recht einfach zu bedienen:
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'
)
Wenn ich diese lib verwende, habe ich einen Fehler von 'python27_x64 \ lib \ site-packages \ plyer \ platforms \ win \ libs \ balloontip.py ", Zeile 145, in balloon_tip", der Methode 'WindowsBalloonTip (** kwargs)', lautet die Fehlermeldung: 'TypeError: __init __() hat ein unerwartetes Schlüsselwort-Argument 'ticker'. Es ist windows7 mit python 2.7. – FaithReaper
... und das Überprüfen des Repos, ein ähnliches Problem wird bereits veröffentlicht und der Autor schlägt vor, die neueste Entwicklerversion direkt von Github zu installieren, und das löst das Problem. Großartig! 'pip install -I https://github.com/kivy/plyer/zipball/master' – FaithReaper
in Linux-System, können Sie notify-send
eingebauten Befehl verwenden.
ntfy
Bibliothek kann zum Senden von Push-Benachrichtigungen verwendet werden.
click here for ntfy documentation
Installation:
sudo pip install ntfy
Beispiele:
ntfy send "your message!"
ntfy send -t "your custom title" "your message"
Ich würde hinzufügen, dass dies ein Python-Programm ist, das von der Kommandozeile aufgerufen wird und kein interner Python-Befehl – tc88
Works, aber ich mag es mehr "modern", verschiedener Look & Feel sein. So können Sie beispielsweise ein Bild in das Popup einfügen. Ist das mit dieser Apprache möglich? –
habe nicht versucht, aber möglicherweise geben den Pfad zum Symbol anstelle der IconPathName könnte funktionieren –