zoukankan      html  css  js  c++  java
  • 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_())

    作者:,,,沙子,,,
    遵循创作共用版权协议,要求署名、非商业、保持一致 。在满足创作共用版权协议的基础上可以转载,但请以超链接形式注明出处。
  • 相关阅读:
    python学习笔记(2)--sublimeText3运行python
    python学习笔记(1)--遍历txt文件,正则匹配替换文字
    JS学习笔记(4)--js变量的生命周期
    JS学习笔记(3)--json格式数据的添加,删除及排序方法
    JS学习笔记(2)--正则表达式获取指定字符串
    JS学习笔记(1)--sort排序
    VBA学习笔记(1)----VBA对象属性方法
    能够作图的软件都有哪些
    怎么用ChemDraw连接两个结构片段
    在几何画板上画椭圆可以根据椭圆第二定义
  • 原文地址:https://www.cnblogs.com/lkpp/p/7400035.html
Copyright © 2011-2022 走看看