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备份目录及目录下的全部内容的实现方法
Jun 12 Python
通过源码分析Python中的切片赋值
May 08 Python
python链接oracle数据库以及数据库的增删改查实例
Jan 30 Python
CentOS下Python3的安装及创建虚拟环境的方法
Nov 28 Python
python实现AES和RSA加解密的方法
Mar 28 Python
Python 常用模块 re 使用方法详解
Jun 06 Python
Python 循环终止语句的三种方法小结
Jun 24 Python
Python操作MySQL数据库实例详解【安装、连接、增删改查等】
Jan 17 Python
PyQt中使用QtSql连接MySql数据库的方法
Jul 28 Python
详解python对象之间的交互
Sep 29 Python
通过案例解析python鸭子类型相关原理
Oct 10 Python
Python docx库删除复制paragraph及行高设置图片插入示例
Jul 23 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
PHP 循环列出目录内容的函数代码
2010/05/26 PHP
php指定函数参数默认值示例代码
2013/12/04 PHP
php生成数字字母的验证码图片
2015/07/14 PHP
Yii框架表单模型和验证用法
2016/05/20 PHP
[企业公众号]升级到[企业微信]之后发送消息失败的解决方法
2017/06/30 PHP
php 提交表单 关闭layer弹窗iframe的实例讲解
2018/08/20 PHP
Javascript Math ceil()、floor()、round()三个函数的区别
2010/03/09 Javascript
JavaScript中的this关键字使用方法总结
2015/03/13 Javascript
jquery中的工具使用方法$.isFunction, $.isArray(), $.isWindow()
2015/08/09 Javascript
js实现文本框支持加减运算的方法
2015/08/19 Javascript
bootstrap导航、选项卡实现代码
2016/12/28 Javascript
AngularJS的ng-repeat指令与scope继承关系实例详解
2017/01/21 Javascript
AngularJS $http模块POST请求实现
2017/04/08 Javascript
JavaScript实现的冒泡排序法及统计相邻数交换次数示例
2017/04/26 Javascript
es7学习教程之fetch解决异步嵌套问题的方法示例
2017/07/21 Javascript
jQuery实现标签子元素的添加和赋值方法
2018/02/24 jQuery
vue和小程序项目中使用iconfont的方法
2020/05/19 Javascript
通过高德地图API获得某条道路上的所有坐标用于描绘道路的方法
2020/08/24 Javascript
Python实现Windows上气泡提醒效果的方法
2015/06/03 Python
详解python中xlrd包的安装与处理Excel表格
2016/12/16 Python
Pycharm学习教程(3) 代码运行调试
2017/05/03 Python
Python Numpy:找到list中的np.nan值方法
2018/10/30 Python
完美解决python3.7 pip升级 拒绝访问问题
2019/07/12 Python
把django中admin后台界面的英文修改为中文显示的方法
2019/07/26 Python
Pycharm中Python环境配置常见问题解析
2020/01/16 Python
详解Python 循环嵌套
2020/07/09 Python
Python开发.exe小工具的详细步骤
2021/01/27 Python
matplotlib之属性组合包(cycler)的使用
2021/02/24 Python
Stuart Weitzman欧盟:美国奢华鞋履品牌
2017/05/24 全球购物
Vero Moda西班牙官方购物网站:丹麦BESTSELLER旗下知名女装品牌
2018/04/27 全球购物
PUMA澳大利亚官方网站:德国运动品牌
2018/10/19 全球购物
英国现代、当代和设计师家具店:Furntastic
2020/07/18 全球购物
浪费资源的建议书
2014/03/12 职场文书
群众路线教育实践活动实施方案
2014/10/31 职场文书
2014年教务工作总结
2014/12/03 职场文书
校长师德表现自我评价
2015/03/04 职场文书