下面是关于“python+PyQT实现系统桌面时钟”的完整攻略。
准备工作
在PyQT5中,可以使用QTimer
类和QLabel
类来创建一个实时更新的系统桌面时钟。需要借助Python中的datetime模块获取当前时间,使用PyQT5中的QLabel类来实时显示时间,使用QTimer类来定时器更新时间。在实现时钟前,需要有一些必要的准备工作,如安装所需的依赖包。
安装PyQt5
使用以下命令来安装PyQt5依赖包:
pip install PyQt5
导入模块
在Python代码中导入以下模块:
# 导入PyQt5中需要用到的模块
import sys
from PyQt5.QtWidgets import QApplication, QLabel
from PyQt5.QtCore import QTimer, Qt, QTime
创建系统桌面时钟
创建一个PyQt5窗口,将其设置为无框、透明,并且可以在窗口所在的位置之上。
# 创建 PyQT5 应用程序对象
app = QApplication(sys.argv)
app.setQuitOnLastWindowClosed(False)
# 创建时钟窗口并设置窗口属性
clock = QLabel()
clock.setAlignment(Qt.AlignCenter)
clock.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint)
clock.setAttribute(Qt.WA_TranslucentBackground)
# 设置时钟字体
font = clock.font()
font.setPointSize(30)
clock.setFont(font)
# 显示窗口
clock.show()
使用QTimer
类来更新时间,并且将当前时间按特定格式(时分秒)设置到标签中,实现桌面时钟显示:
def display_time():
# 获取系统时间
current_time = QTime.currentTime()
# 格式化时间
current_time_str = current_time.toString('hh:mm:ss')
# 显示时间
clock.setText(current_time_str)
# 创建 QTimer 实例并启动
timer = QTimer()
timer.timeout.connect(display_time)
timer.start(1000)
# 执行应用程序
sys.exit(app.exec_())
在上述代码中,display_time()
函数用于获取当前系统时间并将其格式化为hh:mm:ss的形式,最后用clock.setText()
函数将时间显示在QLabel
中。QTimer
定时器每隔1秒就会触发display_time()
函数,更新QLabel
中的时间。
示例一
下面是一个实现了桌面时钟的示例代码:
# 导入PyQt5中需要用到的模块
import sys
from PyQt5.QtWidgets import QApplication, QLabel
from PyQt5.QtCore import QTimer, Qt, QTime
# 创建 PyQT5 应用程序对象
app = QApplication(sys.argv)
app.setQuitOnLastWindowClosed(False)
# 创建时钟窗口并设置窗口属性
clock = QLabel()
clock.setAlignment(Qt.AlignCenter)
clock.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint)
clock.setAttribute(Qt.WA_TranslucentBackground)
# 设置时钟字体
font = clock.font()
font.setPointSize(30)
clock.setFont(font)
# 显示窗口
clock.show()
def display_time():
# 获取系统时间
current_time = QTime.currentTime()
# 格式化时间
current_time_str = current_time.toString('hh:mm:ss')
# 显示时间
clock.setText(current_time_str)
# 创建 QTimer 实例并启动
timer = QTimer()
timer.timeout.connect(display_time)
timer.start(1000)
# 执行应用程序
sys.exit(app.exec_())
运行以上代码,即可实现一个简单的系统桌面时钟应用。
示例二
下面是另一个示例代码,该代码添加了一个图标和一个联系邮箱的超链接:
# 导入PyQt5中需要用到的模块
import sys
from PyQt5.QtWidgets import QApplication, QLabel
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import QTimer, Qt, QTime, QUrl
from PyQt5.QtGui import QDesktopServices
# 创建 PyQT5 应用程序对象
app = QApplication(sys.argv)
app.setQuitOnLastWindowClosed(False)
# 创建时钟窗口并设置窗口属性
clock = QLabel()
clock.setAlignment(Qt.AlignCenter)
clock.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint)
clock.setAttribute(Qt.WA_TranslucentBackground)
# 设置时钟字体
font = clock.font()
font.setPointSize(30)
clock.setFont(font)
# 设置窗口图标
app_icon = QIcon('icon.png')
app.setWindowIcon(app_icon)
# 显示窗口
clock.show()
def display_time():
# 获取系统时间
current_time = QTime.currentTime()
# 格式化时间
current_time_str = current_time.toString('hh:mm:ss')
# 显示时间
clock.setText(current_time_str)
# 创建 QTimer 实例并启动
timer = QTimer()
timer.timeout.connect(display_time)
timer.start(1000)
# 创建超链接
link = QLabel('<a href="mailto:test@example.com">联系我们</a>')
link.setAlignment(Qt.AlignCenter)
link.setOpenExternalLinks(True)
# 显示超链接
link.show()
# 点击超链接打开邮箱客户端
def open_mailto_url(url):
QDesktopServices.openUrl(QUrl(url))
link.linkActivated.connect(open_mailto_url)
# 执行应用程序
sys.exit(app.exec_())
运行以上代码,即可实现一个含有超链接的系统桌面时钟应用。其中,QtCore.QUrl
和QtCore.QDesktopServices
类用于打开邮箱客户端。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:python+PyQT实现系统桌面时钟 - Python技术站