python+PyQT实现系统桌面时钟


Posted in Python onJune 16, 2020

用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 <b>Quit</b> 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 相关文章推荐
Python socket C/S结构的聊天室应用实现
Nov 30 Python
python中的tcp示例详解
Dec 09 Python
django orm 通过related_name反向查询的方法
Dec 15 Python
DRF跨域后端解决之django-cors-headers的使用
Jan 27 Python
python笔记之mean()函数实现求取均值的功能代码
Jul 05 Python
python使用 zip 同时迭代多个序列示例
Jul 06 Python
Python 支持向量机分类器的实现
Jan 15 Python
Python Tkinter Entry和Text的添加与使用详解
Mar 04 Python
Python内置函数locals和globals对比
Apr 28 Python
python的json包位置及用法总结
Jun 21 Python
Python 打印自己设计的字体的实例讲解
Jan 04 Python
python数字图像处理之图像自动阈值分割示例
Jun 28 Python
Windows 8.1 64bit下搭建 Scrapy 0.22 环境
Nov 18 #Python
Window环境下Scrapy开发环境搭建
Nov 18 #Python
Python中安装easy_install的方法
Nov 18 #Python
win7 x64系统中安装Scrapy的方法
Nov 18 #Python
python实现简易数码时钟
Feb 19 #Python
python爬取淘宝商品销量信息
Nov 16 #Python
python爬取网易云音乐评论
Nov 16 #Python
You might like
Oracle Faq(Oracle的版本)
2006/10/09 PHP
php时间不正确的解决方法
2008/04/09 PHP
Uchome1.2 1.5 代码学习 common.php
2009/04/24 PHP
php 获取可变函数参数的函数
2009/08/26 PHP
php数据库连接时容易出错的特殊符号问题
2010/09/01 PHP
js 加载时自动调整图片大小
2008/05/28 Javascript
基于jQuery的表格操作插件
2010/04/22 Javascript
jQuery validate 中文API 附validate.js中文api手册
2010/07/31 Javascript
jQuery效果 slideToggle() 方法(在隐藏和显示之间切换)
2011/06/28 Javascript
javascript 用函数语句和表达式定义函数的区别详解
2014/01/06 Javascript
Nodejs进程管理模块forever详解
2014/06/01 NodeJs
jQuery修改CSS伪元素属性的方法
2014/07/30 Javascript
JavaScript中提前声明变量或函数例子
2014/11/12 Javascript
DOM节点删除函数removeChild()用法实例
2015/01/12 Javascript
JavaScript从数组的indexOf()深入之Object的Property机制
2016/05/11 Javascript
创建基于Bootstrap的下拉菜单的DropDownList的JQuery插件
2016/06/02 Javascript
关于javascript作用域的常见面试题分享
2017/06/18 Javascript
在Vue项目中使用d3.js的实例代码
2018/05/01 Javascript
微信小程序getLocation 需要在app.json中声明permission字段
2020/03/03 Javascript
Python 列表list使用介绍
2014/11/30 Python
Python实现两个list对应元素相减操作示例
2017/06/09 Python
详细解读tornado协程(coroutine)原理
2018/01/15 Python
对python数据切割归并算法的实例讲解
2018/12/12 Python
python为什么要安装到c盘
2020/07/20 Python
利用Python将图片中扭曲矩形的复原
2020/09/07 Python
详解Scrapy Redis入门实战
2020/11/18 Python
萌新HTML5 入门指南(二)
2020/11/09 HTML / CSS
印度尼西亚综合购物网站:Lazada印尼
2016/09/07 全球购物
马来西亚航空官方网站:Malaysia Airlines
2017/07/28 全球购物
教育英语专业毕业生的求职信
2014/03/13 职场文书
红色经典电影观后感
2015/06/18 职场文书
结婚十年感言
2015/07/31 职场文书
党员反腐倡廉学习心得体会
2015/08/15 职场文书
大学自主招生自荐信(2016精选篇)
2016/01/28 职场文书
详解Python requests模块
2021/06/21 Python
Python sklearn分类决策树方法详解
2022/09/23 Python