python透明的桌面时钟_PyQt5实现透明电子时钟

# -*- coding: utf-8 -*-

import sys

from PyQt5 import QtCore

from PyQt5 import QtGui

from PyQt5 import QtWidgets

class LcdTime(QtWidgets.QFrame):

def __init__(self, parent=None):

super(LcdTime, self).__init__(parent)

self.hour = QtWidgets.QLCDNumber(8, self)

self.hour.setGeometry(10, 10, 200, 70)

self.hour.setSegmentStyle(QtWidgets.QLCDNumber.Flat)

self.display()

self.timer = QtCore.QTimer()

self.timer.timeout.connect(self.display)

#self.connect(self.timer, QtCore.SIGNAL('timeout()'), self.display)

self.timer.start(1000)

self.build_tray()

self.resize(220, 100)

self.central()

# 边框透明

self.hour.setFrameShape(QtWidgets.QFrame.NoFrame)

self.setWindowFlags(QtCore.Qt.FramelessWindowHint | QtCore.Qt.SubWindow | QtCore.Qt.WindowStaysOnTopHint)

# 透明处理,移动需要拖动数字

self.setAttribute(QtCore.Qt.WA_TranslucentBackground, True)

self.setMouseTracking(True)

def mousePressEvent(self, event):

if event.button() == QtCore.Qt.LeftButton:

self.dragPosition = event.globalPos() - self.frameGeometry().topLeft()

event.accept()

def mouseMoveEvent(self, event):

if event.buttons() == QtCore.Qt.LeftButton:

self.move(event.globalPos() - self.dragPosition)

event.accept()

def build_tray(self):

self.trayIcon = QtWidgets.QSystemTrayIcon(self)

self.trayIcon.setIcon(QtGui.QIcon('resource/logo.png'))

self.trayIcon.show()

self.trayIcon.setToolTip('时钟 -LiKui')

self.trayIcon.activated.connect(self.trayClick)

menu = QtWidgets.QMenu()

normalAction = menu.addAction('正常显示')

miniAction = menu.addAction('最小化托盘')

exitAction = menu.addAction('退出')

normalAction.triggered.connect(self.showNormal)

exitAction.triggered.connect(self.exit)

miniAction.triggered.connect(self.showMinimized)

self.trayIcon.setContextMenu(menu)

def exit(self):

# 不设置Visible为False,退出后TrayIcon不会刷新

self.trayIcon.setVisible(False)

sys.exit(0)

def trayClick(self, reason):

if reason == QtWidgets.QSystemTrayIcon.DoubleClick:

self.showNormal()

self.repaint()

def display(self):

current = QtCore.QTime.currentTime()

self.hour.display(current.toString('HH:mm:ss'))

def showNormal(self):

super(LcdTime, self).showNormal()

self.repaint()

def central(self):

screen = QtWidgets.QDesktopWidget().screenGeometry()

size = self.geometry()

self.move(screen.width() - size.width(), 0)

app = QtWidgets.QApplication(sys.argv)

lcd = LcdTime()

lcd.show()

sys.exit(app.exec_())

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值