python实现桌面气泡提示功能

时间:2021-05-22

在写桌面软件时,通常会使用到托盘上的泡泡提示功能,让我们来看看使用python如何实现这个小功能。

一、Linux系统

在Linux上,实现一个气泡提示非常简单,使用GTK实现的pynotify模块提供了些功能,我的环境是Ubuntu,默认安装此模块,如果没有,下载源文件编译安装一个。实现代码如下:

#!/usr/bin/python#coding:utf-8 import pynotify pynotify.init ("Bubble@Linux")bubble_notify = pynotify.Notification ("Linux上的泡泡提示", "看,比Windows上实现方便多了!")bubble_notify.show ()

效果:

二、Windows下的实现

Windows下实现是比较复杂的,没有pynotify这样一个模块,找到了一个还算不错的模块(地址),这个类有些语法上的小问题,至少在python2.6下如此,需要修改一下,如下是修改后的代码),基本可用,代码如下:

#!/usr/bin/env python# -*- coding: utf-8 -*- #gtkPopupNotify.py## Copyright 2009 Daniel Woodhouse # modified by NickCis 2010 http://github.com/NickCis/gtkPopupNotify# Modifications:# Added: * Corner support (notifications can be displayed in all corners# * Use of gtk Stock items or pixbuf to render images in notifications# * Posibility of use fixed height# * Posibility of use image as background# * Not displaying over Windows taskbar(taken from emesene gpl v3)# * y separation.# * font description options# * Callbacks For left, middle and right click##This program is free software: you can redistribute it and/or modify#it under the terms of the GNU Lesser General Public License as published by#the Free Software Foundation, either version 3 of the License, or#(at your option) any later version.##This program is distributed in the hope that it will be useful,#but WITHOUT ANY WARRANTY; without even the implied warranty of#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the#GNU Lesser General Public License for more details.##You should have received a copy of the GNU Lesser General Public License#along with this program. If not, see <http://bine_mask(mask, 0, 0) self.window.set_back_pixmap(pixmap, False) return True def onClick(self, widget, event): if event.button == 1 and self.leftclickCB != None: self.leftclickCB() self.hide_notification() if event.button == 2 and self.middleclickCB != None: self.middleclickCB() self.hide_notification() if event.button == 3 and self.rightclickCB != None: self.rightclickCB() self.hide_notification() if __name__ == "__main__": #example usage def notify_factory(): color = ("green", "blue") image = "logo1_64.png" notifier.bg_color = gtk.gdk.Color(color[0]) notifier.fg_color = gtk.gdk.Color(color[1]) notifier.show_timeout = True notifier.edge_offset_x = 20 notifier.edge_offset_y = 30 notifier.new_popup("Windows上的泡泡提示", "NND,比Linux下复杂多了,效果还不怎么样", image=image) return True def gtk_main_quit(): print "quitting" gtk.main_quit() notifier = NotificationStack(timeout=1) gobject.timeout_add(4000, notify_factory) gobject.timeout_add(8000, gtk_main_quit) gtk.main()

效果如下:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章