Python实现系统桌面时钟

简介:

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

功能:

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

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

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

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

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

运行需要Python2.7 + PyQt4.

[python] view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. __metaclass__ = type  
  2. #!coding= utf-8  
  3. #http://blog.csdn.net/gatieme/article/details/17659259  
  4. #gatieme  
  5.   
  6.   
  7. import sys  
  8. from PyQt4.QtCore import *  
  9. from PyQt4.QtGui import *  
  10.   
  11.   
  12. #--------------------------------------------------------------------------------  
  13. class SystemTrayIcon(QSystemTrayIcon):  
  14.     """ 
  15.     The systemTrayIcon which uesd to connect the clock 
  16.     """  
  17.     #----------------------------------------------------------------------------  
  18.     def __init__(self, mainWindow, parent = None):  
  19.         """ 
  20.         mainWindow : the main window that the system tray icon serves to 
  21.         """      
  22.         super(SystemTrayIcon, self).__init__(parent)  
  23.         self.window = mainWindow  
  24.         self.setIcon(QIcon("heart.svg"))   # set the icon of the systemTrayIcon  
  25.           
  26.         self.createActions( )  
  27.         self.createTrayMenu( )  
  28.           
  29.         self.connect(self, SIGNAL("doubleClicked"), self.window, SLOT("showNormal"))  
  30.         #self.connect(self, SIGNAL("activated( )"), self, SLOT("slot_iconActivated"))  
  31.           
  32.   
  33.     def createActions(self):  
  34.         """ 
  35.         create some action to Max Min Normal show the window 
  36.         """  
  37.         self.minimizeAction = QAction("Mi&nimize"self.window, triggered = self.window.hide)  
  38.         self.maximizeAction = QAction("Ma&ximize"self.window, triggered = self.window.showMaximized)  
  39.         self.restoreAction = QAction("&Restore"self.window, triggered = self.window.showNormal)  
  40.         self.quitAction = QAction("&Quit"self.window, triggered = qApp.quit)  
  41.                   
  42.   
  43.     def createTrayMenu(self):  
  44.          self.trayIconMenu = QMenu(self.window)  
  45.          self.trayIconMenu.addAction(self.minimizeAction)  
  46.          self.trayIconMenu.addAction(self.maximizeAction)  
  47.          self.trayIconMenu.addAction(self.restoreAction)  
  48.          self.trayIconMenu.addSeparator( )  
  49.          self.trayIconMenu.addAction(self.quitAction)  
  50.   
  51.          self.setContextMenu(self.trayIconMenu)  
  52.       
  53.     def setVisible(self, visible):  
  54.         self.minimizeAction.setEnabled(not visible)  
  55.         self.maximizeAction.setEnabled(not self.window.isMaximized())  
  56.         self.restoreAction.setEnabled(self.window.isMaximized() or not visible)  
  57.         super(Window, self).setVisible(visible)  
  58.   
  59.   
  60.   
  61.     def closeEvent(self, event):  
  62.         #if event.button( ) == Qt.RightButton:  
  63.         self.showMessage("Message",  
  64.                 "The program will keep running in the system tray. To "  
  65.                 "terminate the program, choose <b>Quit</b> in the "  
  66.                 "context menu of the system tray entry.",   
  67.                 QSystemTrayIcon.Information, 5000)  
  68.         self.window.hide( )  
  69.         event.ignore( )  
  70.   
  71.     def slot_iconActivated(self, reason):  
  72.         if reason == QSystemTrayIcon.DoubleClick:  
  73.             self.wiondow.showNormal( )  
  74.   
  75.   
  76.   
  77. #--------------------------------------------------------------------------------  
  78. class DigitClock(QLCDNumber):  
  79.     """ 
  80.     the DigitClock show a digit clock int the printer 
  81.     """  
  82.       
  83.     #----------------------------------------------------------------------------  
  84.     def __init__(self, parent = None):  
  85.         """ 
  86.         the constructor function of the DigitClock 
  87.         """  
  88.         super(DigitClock, self).__init__(parent)  
  89.         pale = self.palette( )  
  90.   
  91.         pale.setColor(QPalette.Window, QColor(100180100))  
  92.         self.setPalette(pale)  
  93.           
  94.         self.setNumDigits(19)  
  95.         self.systemTrayIcon = SystemTrayIcon(mainWindow = self)  
  96.   
  97.           
  98.         self.dragPosition = None;  
  99.         self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint | Qt.Popup | Qt.Tool)  
  100.         self.setWindowOpacity(1)  
  101.           
  102.         self.showTime( )            # print the time when the clock show  
  103.         self.systemTrayIcon.show( ) # show the SystemTaryIcon when the clock show   
  104.   
  105.         self.timer = QTimer( )  
  106.         self.connect(self.timer, SIGNAL("timeout( )"), self.showTime)  
  107.         self.timer.start(1000)  
  108.           
  109.         self.resize(50060)  
  110.           
  111.       
  112.     #----------------------------------------------------------------------------  
  113.     def showTime(self):  
  114.         """ 
  115.         show the current time  
  116.         """  
  117.         self.date = QDate.currentDate( )  
  118.         self.time = QTime.currentTime( )  
  119.         text = self.date.toString("yyyy-MM-dd") + " " + self.time.toString("hh:mm:ss")  
  120.         self.display(text)  
  121.   
  122.           
  123.   
  124.     #----------------------------------------------------------------------------  
  125.     def mousePressEvent(self, event):  
  126.         """ 
  127.         clicked the left mouse to move the clock 
  128.         clicked the right mouse to hide the clock 
  129.         """  
  130.         if event.button( ) == Qt.LeftButton:  
  131.             self.dragPosition = event.globalPos( ) - self.frameGeometry( ).topLeft( )  
  132.             event.accept( )  
  133.         elif event.button( ) == Qt.RightButton:  
  134.             self.systemTrayIcon.closeEvent(event)  
  135.   
  136.             #self.systemTrayIcon.hide( )  
  137.             #self.close( )  
  138.   
  139.     def mouseMoveEvent(self, event):  
  140.         """ 
  141.         """  
  142.         if event.buttons( ) & Qt.LeftButton:  
  143.             self.move(event.globalPos( ) - self.dragPosition)  
  144.             event.accept( )  
  145.       
  146.     def keyPressEvent(self, event):  
  147.         """ 
  148.         you can enter "ESC" to normal show the window, when the clock is Maxmize 
  149.         """  
  150.         if event.key() == Qt.Key_Escape and self.isMaximized( ):  
  151.             self.showNormal( )  
  152.   
  153.     def mouseDoubleClickEvent(self, event):  
  154.         """ 
  155.         """  
  156.         if event.buttons() == Qt.LeftButton:  
  157.             if self.isMaximized( ):  
  158.                 self.showNormal( )  
  159.             else:  
  160.                 self.showMaximized( )  
  161.       
  162. if __name__ == "__main__":  
  163.     app = QApplication(sys.argv)  
  164.       
  165.     digitClock = DigitClock( )  
  166.     digitClock.show( )      
  167.       
  168.     sys.exit(app.exec_( ))  
  169.       
转载:http://blog.csdn.net/gatieme/article/details/17659259
目录
相关文章
|
16天前
|
存储 人工智能 搜索推荐
【python】python用户管理系统[简易版](源码+报告)【独一无二】
【python】python用户管理系统[简易版](源码+报告)【独一无二】
|
22天前
|
Python
Python实现简易天气查询系统
Python实现简易天气查询系统
26 4
|
1月前
|
数据库 开发者 Python
用Python代码打造你的私人网页交互系统
用Python代码打造你的私人网页交互系统
27 1
|
2月前
|
存储 算法 计算机视觉
用Python做了个图片识别系统(附源码)
用Python做了个图片识别系统(附源码)
|
2月前
|
前端开发 关系型数据库 MySQL
基于python+mysql的宠物领养网站系统
基于python+mysql的宠物领养网站系统
36 2
|
2月前
|
监控 安全 自动驾驶
基于python的室内老人实时摔倒智能监测系统-跌倒检测系统(康复训练检测+代码)
基于python的室内老人实时摔倒智能监测系统-跌倒检测系统(康复训练检测+代码)
71 1
|
3天前
|
数据采集 NoSQL 搜索推荐
五一假期畅游指南:Python技术构建的热门景点分析系统解读
五一假期畅游指南:Python技术构建的热门景点分析系统解读
|
11天前
|
人工智能 机器人 测试技术
【Python】Python仓储管理系统(源码)【独一无二】
【Python】Python仓储管理系统(源码)【独一无二】
|
16天前
|
人工智能 机器人 测试技术
【Python】Python房屋销售系统(源码)【独一无二】(课程设计)
【Python】Python房屋销售系统(源码)【独一无二】(课程设计)
|
19天前
|
机器学习/深度学习 数据采集 算法
基于Apriori关联规则的电影推荐系统(附python代码)
这是一个基于Apriori算法的电影推荐系统概览。系统通过挖掘用户评分数据来发现关联规则,例如用户观看某部电影后可能感兴趣的其他电影。算法核心是逐层生成频繁项集并设定最小支持度阈值,之后计算规则的置信度。案例中展示了数据预处理、频繁项集生成以及规则提取的过程,具体包括用户评分电影的统计分析,如1-5部电影的评分组合。最后,通过Python代码展示了Apriori算法的实现,生成推荐规则,并给出了一个简单的推荐示例。整个过程旨在提高推荐的精准度,基于用户已评分的电影推测他们可能尚未评分但可能喜欢的电影。
基于Apriori关联规则的电影推荐系统(附python代码)

热门文章

最新文章