python+PyQT实现系统桌面时钟
作者:智元元 发布时间:2023-07-21 07:46:33
标签:python,PyQT,时钟
用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_( ))
来源:https://blog.csdn.net/qq_41352018/article/details/80102659


猜你喜欢
- 今天,由于工作需要,我在自己的电脑上配置了Mysql5环境,同时安装了一个phpMyAdmin管理工具,安装完成后,发现在phpMyAdmi
- 本文实例讲述了python解析多层json操作。分享给大家供大家参考,具体如下:原始文件内容:{ "MaskPolyg
- 前言相信大家都知道当声明一个变量,并且没有给赋值的情况下,它的初始值是undefined。但是在javascript中,怎么检查一个值是否为
- 译序:这篇文章是可用性大师 Jakob Nielsen 在10年前总结的,到今天仍然受用。通过这个时间跨度,可以得出,可用性话题不是某个时代
- 一、最基础的应用import urllib2url = r'http://www.baidu.com'html = urll
- 定义和用法DATEDIFF(datepart,startdate,enddate)startdate 和 enddate 参数是合法的日期表
- 一、前言大家好,今天我来介绍我接一个Python单子。我完成这个单子前后不到2小时。首先我接到这个单子的想法是处理Excel表,在两个表之间
- 1. 系统盘清理说明win7 80G的系统盘,随着使用时间的增加,空间越来越小,只剩不到2G,随计划清理系统盘数据1. maven 下载的j
- 源代码:# coding=utf-8import loggingimport osimport timeLEVELS={'debug
- 在移动端开发应用UI组件也会遇到一系列需要注意的问题。问题1比如说,标签页是一个整体的组件,但是我们需要将标签页的标题和其他组件一起固定到顶
- SQL Server2005扩展函数已经不是一件什么新鲜的事了,但是我看网上的大部分都是说聚合函数,例子也比较浅,那么这里就讲讲我运用扩展函
- 识别验证码OCR(Optical Character Recognition)即光学字符识别技术,专门用于对图片文字进行识别,并获取文本。字
- Windows下ORACLE完全卸载:使用OUI可以卸载数据库,但卸载后注册表和文件系统内仍会有部分残留。这些残留不仅占用磁盘空间,而且影响
- 1. 像素基本操作1.1 读取、修改像素可以通过[行,列]坐标来访问像素点数据,对于多通道数据,返回一个数组,包含所有通道的值,对于单通道数
- 一、数据描述数据集中9994条数据,横跨1237天,销售额为2,297,200.8603美元,利润为286,397.0217美元,他们的库存
- 将dataframe中的NaN替换成希望的值import pandas as pddf1 = pd.DataFrame([{'col
- sql server 锁定模式有三种:共享( S锁),更新(U锁),排他(X锁);S锁是共享锁,如果事务T对数据A加上共享锁后,则其他事务只
- 最近要做个从 pdf 文件中抽取文本内容的工具,大概查了一下 python 里可以使用 pdfminer 来实现。下面就看看怎样使用吧。PD
- 导语昨天下班,回家吃完饭就直接躺了,无聊的时候大家都会干什么呢?当然是刷刷刷——抖音啦,嗯哼,然后返现了抖音上一款特效——「变身漫画」,简直
- 如何做一个检索结果带链接的检索?具体代码和说明如下:<% data=request.form("search_da