怎么用python编辑泡泡_python实现桌面气泡提示功能

在写桌面软件时,通常会使用到托盘上的泡泡提示功能,让我们来看看使用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 .

import os

import gtk

import pango

import gobject

# This code is used only on Windows to get the location on the taskbar

# Taken from emesene Notifications (Gpl v3)

taskbarOffsety = 0

taskbarOffsetx = 0

if os.name == "nt":

import ctypes

from ctypes.wintypes import RECT, DWORD

user = ctypes.windll.user32

MONITORINFOF_PRIMARY = 1

HMONITOR = 1

class MONITORINFO(ctypes.Structure):

_fields_ = [

('cbSize', DWORD),

('rcMonitor', RECT),

('rcWork', RECT),

('dwFlags', DWORD)

]

taskbarSide = "bottom"

taskbarOffset = 30

info = MONITORINFO()

info.cbSize = ctypes.sizeof(info)

info.dwFlags = MONITORINFOF_PRIMARY

user.GetMonitorInfoW(HMONITOR, ctypes.byref(info))

if info.rcMonitor.bottom != info.rcWork.bottom:

taskbarOffsety = info.rcMonitor.bottom - info.rcWork.bottom

if info.rcMonitor.top != info.rcWork.top:

taskbarSide

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值