2013-04-10 139 views

回答

37

的幫助下將pywin32 library您可以使用下面的示例代碼,我發現here

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") 
+0

作品,但我想它是更 「現代」,不同的外觀和感覺。例如,可以將圖像添加到彈出窗口中。這個評價可能嗎? – 2013-04-10 09:58:57

+0

還沒有嘗試過,但也許給路徑圖標代替iconPathName可能工作 – 2016-12-07 07:24:00

4

您需要使用第三方的Python的GUI庫或pywin32庫。 TkInter,與python捆綁的GUI工具包不支持系統托盤彈出窗口。

  • wxPython的
  • PyGTK的
  • PyQt的

,它支持在系統托盤工作Windows專用庫:

支持與系統托盤工作

多種實現形式中立庫

  • py Win32的

信息/系統托盤彈出窗口的使用在Windows wxPython的例子:

+0

好吧,任何如何使用wxPython做到這一點的例子? – 2013-04-10 09:56:33

+1

@RomanRdgz看看[ToasterBox](http://xoomer.virgilio.it/infinity77/main/freeware.html#toasterbox),我在http://stackoverflow.com/questions/2240674/cross-platform中找到-desktop-通知功能於蟒蛇。 – halex 2013-04-10 10:02:54

11

我最近使用的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' 
) 
+0

當我使用這個庫時,我從'python27_x64 \ lib \ site-packages \ plyer \ platforms \ win \ libs \ balloontip中得到一個錯誤。python「,方法'WindowsBalloonTip(** kwargs)'的第145行,在balloon_tip'中,錯誤信息是:'TypeError:__init __()得到了一個意想不到的關鍵字參數'ticker''它是Windows7的python 2.7。 – FaithReaper 2018-01-08 10:14:59

+2

...並檢查回購,已經發布了類似的問題,作者建議直接從Github安裝最新的dev版本,並解決了這個問題。太棒了!'pip install -I https://github.com/kivy/plyer/zipball/master' – FaithReaper 2018-01-08 10:24:06

1

在Linux系統中,你可以使用內置的命令notify-send

ntfy庫可用於發送推送通知。

click here for ntfy documentation

安裝:

sudo pip install ntfy 

例子:

ntfy send "your message!" 
ntfy send -t "your custom title" "your message" 
+0

我想補充一點,這是一個從命令行調用的python程序,而不是一個內部的Python命令 – tc88 2017-10-26 05:49:40