用Python + PyQT写的一个系统桌面时钟,刚学习Python,写的比较简陋,但是基本的功能还可以。

功能:

①窗体在应用程序最上层,不用但是打开其他应用后看不到时间

②左键双击全屏,可以做小屏保使用,再次双击退出全屏。

③系统托盘图标,主要参考PyQt4源码目录中的PyQt4\examples\desktop\systray下的程序

④鼠标右键,将程序最小化

使用时需要heart.svg放在源代码同级目录下,[文件可在PyQt4示例代码目录下PyQt4\examples\desktop\systray\images找到

运行需要Python2.7 + PyQt4.

__metaclass__ = type

#!coding= utf-8

#http://blog.csdn.net/gatieme/article/details/17659259

#gatieme

import sys

from PyQt4.QtCore import *

from PyQt4.QtGui import *

#--------------------------------------------------------------------------------

class SystemTrayIcon(QSystemTrayIcon):

"""

The systemTrayIcon which uesd to connect the clock

"""

#----------------------------------------------------------------------------

def __init__(self, mainWindow, parent = None):

"""

mainWindow : the main window that the system tray icon serves to

"""

super(SystemTrayIcon, self).__init__(parent)

self.window = mainWindow

self.setIcon(QIcon("heart.svg")) # set the icon of the systemTrayIcon

self.createActions( )

self.createTrayMenu( )

self.connect(self, SIGNAL("doubleClicked"), self.window, SLOT("showNormal"))

#self.connect(self, SIGNAL("activated( )"), self, SLOT("slot_iconActivated"))

def createActions(self):

"""

create some action to Max Min Normal show the window

"""

self.minimizeAction = QAction("Mi&nimize", self.window, triggered = self.window.hide)

self.maximizeAction = QAction("Ma&ximize", self.window, triggered = self.window.showMaximized)

self.restoreAction = QAction("&Restore", self.window, triggered = self.window.showNormal)

self.quitAction = QAction("&Quit", self.window, triggered = qApp.quit)

def createTrayMenu(self):

self.trayIconMenu = QMenu(self.window)

self.trayIconMenu.addAction(self.minimizeAction)

self.trayIconMenu.addAction(self.maximizeAction)

self.trayIconMenu.addAction(self.restoreAction)

self.trayIconMenu.addSeparator( )

self.trayIconMenu.addAction(self.quitAction)

self.setContextMenu(self.trayIconMenu)

def setVisible(self, visible):

self.minimizeAction.setEnabled(not visible)

self.maximizeAction.setEnabled(not self.window.isMaximized())

self.restoreAction.setEnabled(self.window.isMaximized() or not visible)

super(Window, self).setVisible(visible)

def closeEvent(self, event):

#if event.button( ) == Qt.RightButton:

self.showMessage("Message",

"The program will keep running in the system tray. To "

"terminate the program, choose Quit in the "

"context menu of the system tray entry.",

QSystemTrayIcon.Information, 5000)

self.window.hide( )

event.ignore( )

def slot_iconActivated(self, reason):

if reason == QSystemTrayIcon.DoubleClick:

self.wiondow.showNormal( )

#--------------------------------------------------------------------------------

class DigitClock(QLCDNumber):

"""

the DigitClock show a digit clock int the printer

"""

#----------------------------------------------------------------------------

def __init__(self, parent = None):

"""

the constructor function of the DigitClock

"""

super(DigitClock, self).__init__(parent)

pale = self.palette( )

pale.setColor(QPalette.Window, QColor(100, 180, 100))

self.setPalette(pale)

self.setNumDigits(19)

self.systemTrayIcon = SystemTrayIcon(mainWindow = self)

self.dragPosition = None;

self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint | Qt.Popup | Qt.Tool)

self.setWindowOpacity(1)

self.showTime( ) # print the time when the clock show

self.systemTrayIcon.show( ) # show the SystemTaryIcon when the clock show

self.timer = QTimer( )

self.connect(self.timer, SIGNAL("timeout( )"), self.showTime)

self.timer.start(1000)

self.resize(500, 60)

#----------------------------------------------------------------------------

def showTime(self):

"""

show the current time

"""

self.date = QDate.currentDate( )

self.time = QTime.currentTime( )

text = self.date.toString("yyyy-MM-dd") + " " + self.time.toString("hh:mm:ss")

self.display(text)

#----------------------------------------------------------------------------

def mousePressEvent(self, event):

"""

clicked the left mouse to move the clock

clicked the right mouse to hide the clock

"""

if event.button( ) == Qt.LeftButton:

self.dragPosition = event.globalPos( ) - self.frameGeometry( ).topLeft( )

event.accept( )

elif event.button( ) == Qt.RightButton:

self.systemTrayIcon.closeEvent(event)

#self.systemTrayIcon.hide( )

#self.close( )

def mouseMoveEvent(self, event):

"""

"""

if event.buttons( ) & Qt.LeftButton:

self.move(event.globalPos( ) - self.dragPosition)

event.accept( )

def keyPressEvent(self, event):

"""

you can enter "ESC" to normal show the window, when the clock is Maxmize

"""

if event.key() == Qt.Key_Escape and self.isMaximized( ):

self.showNormal( )

def mouseDoubleClickEvent(self, event):

"""

"""

if event.buttons() == Qt.LeftButton:

if self.isMaximized( ):

self.showNormal( )

else:

self.showMaximized( )

if __name__ == "__main__":

app = QApplication(sys.argv)

digitClock = DigitClock( )

digitClock.show( )

sys.exit(app.exec_( ))

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

本文标题: python+PyQT实现系统桌面时钟

本文地址: http://www.cppcns.com/jiaoben/python/245058.html

python透明的桌面时钟_python+PyQT实现系统桌面时钟相关推荐

  1. python程序的基本框架_Python PyQt学习随笔:PyQt主程序的基本框架

    在完成UI设计将UI通过PyUic转成Py文件后,由于这个生成的文件每次通过PyUic生成时都会被覆盖,因此应用的主程序必须另外单独编写py文件.需要将UI生成的文件import到主程序的py文件中. ...

  2. 计算机出现假桌面怎么解决办法,Win10系统桌面频繁假死的解决方法

    很多用户在操作win10系统的过程中,发现apphangxprocb1会使explorer.exe文件损坏出现卡顿情况,导致电脑毫无反应,过一会电脑就会自动重启运行explorer.exe,面对这种桌 ...

  3. 计算机桌面出现家庭组,win10系统桌面突然多出一个家庭组图标的修复步骤

    win10系统使用久了,好多网友反馈说win10系统桌面突然多出一个家庭组图标的问题,非常不方便.有什么办法可以永久解决win10系统桌面突然多出一个家庭组图标的问题,面对win10系统桌面突然多出一 ...

  4. 计算机桌面出现家庭组,win7系统桌面突然多出一个家庭组图标怎么办?

    我们都知道家庭组是家庭网络上可以共享文件和打印机的一组计算机,可以共享视频或文件,但是有些用户说 win7旗舰版64位系统 桌面突然多出一个家庭组图标,也不知道什么原因,看起来也比较奇怪,平时也比较少 ...

  5. 怎样能让计算机桌面软件变小,win7系统桌面图标如何变小?win7系统把桌面图标变小的4种方法...

    win7系统桌面图标如何变小?有些用户升级win7系统后发现桌面图标很大,与之前xp系统有很大区别,看着不舒服,win7系统桌面图标如何变小?其实把桌面变小有很多方法,下面小编一一向大家win7系统把 ...

  6. 计算机桌面出现家庭组,win7系统桌面突然多出一个家庭组图标怎么办

    我们都知道家庭组是家庭网络上可以共享文件和打印机的一组计算机,可以共享视频或文件,但是有些用户说win7旗舰版64位系统桌面突然多出一个家庭组图标,也不知道什么原因,看起来也比较奇怪,平时也比较少用到 ...

  7. python任务栏通知区域_python+pyqt实现右下角弹出框

    本文实例为大家分享了pyqt实现右下角弹出框的具体代码,供大家参考,具体内容如下 构造函数中: self.desktop=QDesktopWidget() self.move((self.deskto ...

  8. python代码桌面壁纸_Python实现设置windows桌面壁纸代码分享

    每天换一个壁纸,每天好心情. # -*- coding: UTF-8 -*- from __future__ import unicode_literals import Image import d ...

  9. python代码桌面壁纸_Python实现设置windows桌面壁纸代码

    Python实现设置windows桌面壁纸代码 发布于 2015-04-07 16:59:42 | 122 次阅读 | 评论: 0 | 来源: 网友投递 Python编程语言Python 是一种面向对 ...

最新文章

  1. Nginx搭建flv视频点播服务器
  2. 三大运营商借5G消息围攻微信,就问张小龙怕不怕
  3. ejabberd登陆不成功的解决方案
  4. 【Makefile由浅入深完全学习记录6】Makefile中变量的高级主题上
  5. 11月4日,上海开源基础设施峰会,不见不散!
  6. 股票型基金和期货有什么区别?
  7. JAVA 入门PDF
  8. Nginx面试题(总结最全面的面试题!!!)
  9. 最常用的scrum工具、敏捷开发工具、看板工具
  10. centos 7之firewalld防火墙配置IP伪装和端口转发案例详解
  11. 小红书口碑营销推广方式有哪些?
  12. 小白学统计|面板数据分析与Stata应用笔记(一)
  13. 技术研究:DOOM3网络模型的演化与网络架构
  14. 免费领百度网盘会员,12月31日截止
  15. 人工智能教学中的功利文化视野
  16. 企业微信有权限查看通讯录吗?
  17. 上市公司杰创智能携手甄云,启动供应链采购数字化升级
  18. 五分之四的不同表达式_五分之四表示什么?还可以表示什么?
  19. python动态爬取实时_python爬取动态数据实战---猫眼专业版-实时票房(二)
  20. fetch和XHR的区别

热门文章

  1. 用python登录WeChat 教你微商自动回复(非常详细)
  2. 换友情链接的几个小标准
  3. 下载子弹短信后,我以为半个小时会卸载它。
  4. Windows Server2012R2 利用powershell安装 .NET Framework 3.5.1
  5. 2022年二级建造师机电工程常用材料及工程设备测试题及答案
  6. 如何找到文件夹选项?
  7. 问与答——怎么看哑舍
  8. 基于主要成分分析的人脸二维码识别系统-含Matlab代码
  9. python对象包括哪些,Python | 对象和类
  10. const ... of 与 const ... in 区别