凌的博客

您现在的位置是:首页  > 学无止境 > python

python

pyqt5设置窗体透明控件不透明

2019-10-04python0
import sys
from PyQt5.QtWidgets import QWidget, QApplication, QDesktopWidget, QLabel
from PyQt5.QtGui import QCursor,QFont
from PyQt5.QtCore import Qt, QTimer


class Example(QWidget):
    __dragWin = False

    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setWindowFlags(Qt.WindowStaysOnTopHint)
        # pyqt5设置窗体透明控件不透明
        self.setAttribute(Qt.WA_TranslucentBackground)
        self.setWindowFlags(Qt.FramelessWindowHint | Qt.Tool)

        self.pic_lbl = QLabel("5", self)
        self.pic_lbl.setStyleSheet("QLabel{font-size:200px; color:#f0f;}")
        self.pic_lbl.setFont(QFont("Microsoft YaHei"))
        self.pic_lbl.setAlignment(Qt.AlignCenter)
        # pic_lbl.setPixmap(QPixmap("bg2.png"))
        # self.setWindowOpacity(.5)

        self.timer = QTimer()
        self.timer.setInterval(1000)
        self.timer.timeout.connect(self.djs)
        self.timer.start()

        self.setGeometry(300, 300, 200, 240)
        self.setWindowTitle("测试")
        self.center()

    def djs(self):
        num = int(self.pic_lbl.text())
        num -= 1
        if num < 0:
            QApplication.instance().exit()

        self.pic_lbl.setText(str(num))
        self.pic_lbl.adjustSize()

    def center(self):
        qr = self.frameGeometry()
        cp = QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())

    def mousePressEvent(self, e):
        self.__dragWin = True
        self.__dragWin_x = e.x()
        self.__dragWin_y = e.y()
        self.setCursor(QCursor(Qt.OpenHandCursor))  # 更改鼠标图标

    def mouseMoveEvent(self, e):
        if self.__dragWin == True:
            pos = e.globalPos()
            self.move(pos.x() - self.__dragWin_x, pos.y() - self.__dragWin_y)

    def mouseReleaseEvent(self, e):
        self.__dragWin = False
        self.setCursor(QCursor(Qt.ArrowCursor))

    def mouseDoubleClickEvent(self, e):
        print("double click")
        QApplication.instance().exit()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    ex.show()
    sys.exit(app.exec_())

3.png

如果本文对您有所帮助,请支持下本站哦!!!^_^

文章评论

0条评论