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的chardet库获得文件编码并修改编码
Jan 22 Python
浅谈使用Python变量时要避免的3个错误
Oct 30 Python
Python用imghdr模块识别图片格式实例解析
Jan 11 Python
Python编程求质数实例代码
Jan 31 Python
Python爬虫实现获取动态gif格式搞笑图片的方法示例
Dec 24 Python
python 读取dicom文件,生成info.txt和raw文件的方法
Jan 24 Python
python把ipynb文件转换成pdf文件过程详解
Jul 09 Python
pymysql模块的操作实例
Dec 17 Python
python如何查看安装了的模块
Jun 23 Python
Python正则re模块使用步骤及原理解析
Aug 18 Python
python 用struct模块解决黏包问题
Nov 07 Python
Python爬虫过程解析之多线程获取小米应用商店数据
Nov 14 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
8个必备的PHP功能实例代码
2013/10/27 PHP
Laravel 5框架学习之表单验证
2015/04/08 PHP
php求一个网段开始与结束IP地址的方法
2015/07/09 PHP
PHP判断数组是否为空的常用方法(五种方法)
2017/02/08 PHP
php读取出一个文件夹及其子文件夹下所有文件的方法示例
2017/06/15 PHP
jQuery数据缓存用法分析
2015/02/20 Javascript
JavaScript知识点总结(六)之JavaScript判断变量数据类型
2016/05/31 Javascript
js数组去重的hash方法
2016/12/22 Javascript
详解jquery validate实现表单验证 (正则表达式)
2017/01/18 Javascript
JS中的作用域链
2017/03/01 Javascript
JS正则表达式常见用法实例详解
2018/06/19 Javascript
微信小程序学习笔记之表单提交与PHP后台数据交互处理图文详解
2019/03/28 Javascript
简谈创建React Component的几种方式
2019/06/15 Javascript
JsonProperty 的使用方法详解
2019/10/11 Javascript
基于vue+echarts 数据可视化大屏展示的方法示例
2020/03/09 Javascript
js观察者模式的弹幕案例
2020/11/23 Javascript
[01:32]TI珍贵瞬间系列(一)
2020/08/26 DOTA
Python中的pprint折腾记
2015/01/21 Python
详解Django模版中加载静态文件配置方法
2019/07/21 Python
Python 开发工具PyCharm安装教程图文详解(新手必看)
2020/02/28 Python
Python 创建TCP服务器的方法
2020/07/28 Python
Python使用cn2an实现中文数字与阿拉伯数字的相互转换
2021/03/02 Python
深入浅析HTML5中的SVG
2015/11/27 HTML / CSS
美国孕妇装品牌:Destination Maternity
2018/02/04 全球购物
Nixon手表英国官网:美国尼克松手表品牌
2020/02/10 全球购物
How to spawning asynchronous work in J2EE
2016/08/29 面试题
装修致歉信
2014/01/15 职场文书
仓库规划计划书
2014/04/28 职场文书
好的促销活动方案
2014/08/21 职场文书
交通事故协议书范本
2014/11/18 职场文书
2014年接待工作总结
2014/11/26 职场文书
会议通知格式范文
2015/04/15 职场文书
清明节主题班会
2015/08/14 职场文书
个人工作失误的保证书怎么写?
2019/06/21 职场文书
Django如何创作一个简单的最小程序
2021/05/12 Python
解决IDEA翻译插件Translation报错更新TTK失败不能使用
2022/04/24 Python