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开发的nosql数据库CodernityDB介绍和使用实例
Oct 23 Python
详解Python中的装饰器、闭包和functools的教程
Apr 02 Python
简介Django框架中可使用的各类缓存
Jul 23 Python
在Python dataframe中出生日期转化为年龄的实现方法
Oct 20 Python
Python cv2 图像自适应灰度直方图均衡化处理方法
Dec 07 Python
Django实现学员管理系统
Feb 26 Python
python3 打印输出字典中特定的某个key的方法示例
Jul 06 Python
python中如何使用insert函数
Jan 09 Python
Python使用urllib模块对URL网址中的中文编码与解码实例详解
Feb 18 Python
Keras使用ImageNet上预训练的模型方式
May 23 Python
Pycharm 跳转回之前所在页面的操作
Feb 05 Python
Python使用华为API为图像设置多个锚点标签
Apr 12 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中webservice实现的简单架构方法及实例
2015/02/03 PHP
PHP实现发送微博消息功能完整示例
2019/12/04 PHP
ASP小贴士/ASP Tips javascript tips可以当桌面
2009/12/10 Javascript
javascript 进阶篇2 CSS XML学习
2012/03/14 Javascript
jquery 中多条件选择器,相对选择器,层次选择器的区别
2012/07/03 Javascript
Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法总结
2013/11/14 Javascript
javascript实现文本域写入字符时限定字数
2014/02/12 Javascript
jQuery插件Skippr实现焦点图幻灯片特效
2015/04/12 Javascript
nodejs爬虫抓取数据乱码问题总结
2015/07/03 NodeJs
举例讲解JavaScript中将数组元素转换为字符串的方法
2015/10/25 Javascript
JavaScript中获取纯正的undefined的方法
2016/03/06 Javascript
JavaScript_object基础入门(必看篇)
2016/06/13 Javascript
JavaScript String(字符串)对象的简单实例(推荐)
2016/08/31 Javascript
完美解决jQuery 鼠标快速滑过后,会执行多次滑出的问题
2016/12/08 Javascript
jQuery插件FusionWidgets实现的AngularGauge图效果示例【附demo源码】
2017/03/23 jQuery
JavaScript算法教程之sku(库存量单位)详解
2017/06/29 Javascript
vue结合axios与后端进行ajax交互的方法
2018/07/06 Javascript
vue实现的微信机器人聊天功能案例【附源码下载】
2019/02/18 Javascript
[55:45]DOTA2上海特级锦标赛D组败者赛 Liquid VS COL第一局
2016/02/28 DOTA
[57:50]DOTA2上海特级锦标赛主赛事日 - 4 胜者组决赛Secret VS Liquid第二局
2016/03/05 DOTA
python实现批量获取指定文件夹下的所有文件的厂商信息
2014/09/28 Python
Python简单的制作图片验证码实例
2017/05/31 Python
Python2.7读取PDF文件的方法示例
2017/07/13 Python
python 监听salt job状态,并任务数据推送到redis中的方法
2019/01/14 Python
python分布式计算dispy的使用详解
2019/12/22 Python
django日志默认打印request请求信息的方法示例
2020/05/17 Python
巧用 CSS3的webkit-box-reflect 倒影实现各类动效
2021/03/05 HTML / CSS
HTML5超文本标记语言的实现方法
2020/09/24 HTML / CSS
SQL注入攻击的种类有哪些
2013/12/30 面试题
解决方案设计综合面试题
2015/08/31 面试题
Servlet如何得到服务器的信息
2015/12/22 面试题
laravel使用redis队列实例讲解
2021/03/23 PHP
办理信用卡工作证明
2014/09/30 职场文书
2015年建筑工作总结报告
2015/05/04 职场文书
信仰观后感
2015/06/03 职场文书
大型强子对撞机再次重启探索“第五种自然力”
2022/04/29 数码科技