zoukankan      html  css  js  c++  java
  • PyQt4 初试牛刀二

    一、最小话托盘后,调用showNormal()后窗口不刷新,解决办法如下:

        重写showNormal 方法,调用父类方法后,repaint窗体

    def showNormal(self):
        super(LcdTime, self).showNormal()
        self.repaint()

    二、透明显示窗口后无法拖动窗体:

        必须拖动非透明区域,比如数字,目前没有找到好的解决方案。

        

    # -*- coding: utf-8 -*-
    import sys
    from PyQt4 import QtCore, QtGui
    
    
    class LcdTime(QtGui.QDialog):
        def __init__(self, parent=None):
            super(LcdTime, self).__init__(parent)
    
            self.hour = QtGui.QLCDNumber(8, self)
            self.hour.setGeometry(10, 10, 200, 80)
            self.hour.setSegmentStyle(QtGui.QLCDNumber.Flat)
            self.display()
    
            self.timer = QtCore.QTimer()
            self.connect(self.timer, QtCore.SIGNAL('timeout()'), self.display)
            self.timer.start(1000)
    
            self.build_tray()
            self.resize(220, 100)
            self.central()
    
            self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
            # 透明处理,移动需要拖动数字
            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):
            trayIcon = QtGui.QSystemTrayIcon(self)
            trayIcon.setIcon(QtGui.QIcon('logo.png'))
            trayIcon.show()
            trayIcon.setToolTip('时钟')
            trayIcon.activated.connect(self.trayClick)
    
            menu = QtGui.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)
    
            trayIcon.setContextMenu(menu)
    
        def exit(self):
            # 不设置Visible为False,退出后TrayIcon不会刷新
            self.setVisible(False)
            sys.exit(0)
    
        def trayClick(self, reason):
            if reason == QtGui.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 = QtGui.QDesktopWidget().screenGeometry()
            size = self.geometry()
            self.move(screen.width() - size.width(), 0)
    
    
    app = QtGui.QApplication(sys.argv)
    lcd = LcdTime()
    lcd.show()
    sys.exit(app.exec_())

    作者:,,,沙子,,,
    遵循创作共用版权协议,要求署名、非商业、保持一致 。在满足创作共用版权协议的基础上可以转载,但请以超链接形式注明出处。
  • 相关阅读:
    spring-boot 在启动运行脚本中执行修改表结构,执行前校验表结构是否符合,如果不符合就修改
    Ehcache flush() 源码阅读笔记
    关于超时的实现---利用Timer
    java 基础笔记--Map
    spring-boot+mybatis+ehcache实现快速查询
    笔记---html输入限制
    spring boot thymeleaf模板url上的参数传递
    xcode 8   去除无用打印信息
    项目整理 (一)
    FMDB最简单的教程-3 清空数据表并将自增字段清零
  • 原文地址:https://www.cnblogs.com/lkpp/p/7400048.html
Copyright © 2011-2022 走看看