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
0
投稿
猜你喜欢
- Wingdings字体,Symbol字体<html> <head> <title>
- 1. viper的介绍viper是go一个强大的流行的配置解决方案的库。viper是spf13的另外一个重量级库。有大量项目都使用该库,比如
- Python import的搜索路径import的搜索路径为:搜索「内置模块」(built-in module)搜索 sys.path 中的
- 当然有,看看下面,你就会明白:<%Sub TimeDelaySeconds(Delay
- 如何剔除HTML标识?这里没有使用正则表达式来去除,当然大家用正则"<.+?>"也可以实现这个功能只留下干净
- 冒泡排序冒泡排序(英语:Bubble Sort)是一种简单的排序算法。它重复地遍历要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们
- 如下所示:#coding=utf-8import sys, re, osdef getDictList(dict): regx
- C语言是编译型语言,经过编译后,生成机器码,然后再运行,执行速度快,不能跨平台,一般用于操作系统,驱动等底层开发。Python是编译型还是解
- 1.安装pyserialhttps://pypi.python.org/pypi/pyserialDoc:http://pythonhost
- Apple4.us的张亮问我:“很多人说用户体验是苹果的核心竞争力。在用户体验方面,我究竟该从苹果的设计中学习什么” ?关于这个问题的答案我
- 格式化字符串是什么?Python字符串的格式化处理主要是用来将变量(对象)的值填充到字符串中,在字符串中解析Python表达式,对字符串进行
- 1. 安装vim:# apt-get install -y vim-gnome2. 安装ctags,ctags用于支持tagli
- 本文实例借鉴mvc模式,核心数据为model,维护1个矩阵,0表无雷,1表雷,-1表已经检测过。本例使用python的tkinter做gui
- 源码安装Python第三方库几乎都可以在github或者 pypi上找到源码。源码包格式大概有zip 、 tar.zip、 tar.bz2。
- 尝试安装server[justin@xen20-vm04 "]$ rpm -ivh MySQL-server-5.1.51-1.g
- 可以在Mac OS X 10.2.x(“Jaguar”)和以上版本上Mac OS X使用二进制安装软
- 可在图片连接后面加上宽和高,如:width="150" height="50"<h
- MMClassification是一个基于PyTorch的开源图像分类工具箱,是OpenMMLab项目的一部分,源码传送门,最新发布版本为v
- 本文实例实现的功能是监控一个文件或目录的变化,如果有变化,把文件上传备份至备份主机,并且要监控上传过程是否有问题等,具体内容如下#!/usr
- 使用指令:pyinstaller -F -w main.py生成的.exe文件执行报错看报错信息,初步怀疑是生成的.exe不能执行.ini配