PyQt5 - higher-order form beautification

  • [Initialize the class does not change the internal logic of the compiled pyuic5]
  • Rounded Form
  • Custom Minimize, Maximize, Exit button (cancel frame)
  • Mobile mouse control window (cancel frame)



Rounded Form

#首先设置无边框,其次设置背景透明
#背景透明后,可以在整体后方添加一个有色Label标签
#对该有色标签进行QSS圆角化!

MainWindow.setWindowFlag(QtCore.Qt.FramelessWindowHint)
#MainWindow.setWindowOpacity(0.9)  # 设置窗口透明度
MainWindow.setAttribute(QtCore.Qt.WA_TranslucentBackground)  # 设置窗口背景透明


 Mouse to drag the window

Rewrite the mouse or keyboard events:
    [inherited form class containing the corresponding mouse / keyboard events, so this class will inherit the base class for mouse / keyboard events coverage]
file Why use original pyuic5 compiled together if __name__ == "__main__" block execution is not able to perform normal?
    the __name__ == IF "__main__":
        App = QtWidgets.QApplication (the sys.argv)
        the MainWindow The QMainWindow = ()
        UI = Ui_MainWindow ()
        ui.setupUi (the MainWindow)
        with Open ( "./ QSS / demo_0628.qss") AS F:
            MainWindow.setStyleSheet (f.read ())
        MainWindow.show ()
        sys.exit (app.exec_ ())
    we initialize the class were rewritten its mouse event functions,
    but before the execution block in the calling class on the use of the original completed form class instance (third line of code),
    rewrites the mouse event function will not correspond to the form in this instance;
    
    solution:
        1> to modify the structure of the class initialization (i.e. pyuic5 to modify the logical structure of our class initialization)
            .Show directly initialize a class display, instead of the instance of the form is displayed in the execution block
        2> a new class using the class generation block execution complete instances, and then via another control class initialization

class XX(QMainWindow):
    def __init__(self):
        super().__init__()
    # def keyPressEvent(self, event):
    #     print("12345")
    # def mousePressEvent(self,event):
    #     if event.button() == Qt.LeftButton:
    #         print("鼠标左键点击!")
    #         # print(event.pos().x(),event.pos().y())
    #     if event.button() == Qt.RightButton:
    #         print("鼠标右键点击!")
    def mousePressEvent(self, event):
        if event.button() == Qt.LeftButton:
            self.m_flag = True
            self.m_Position = event.globalPos() - self.pos()  # 获取鼠标相对窗口的位置
            event.accept()
            self.setCursor(QCursor(Qt.OpenHandCursor))  # 更改鼠标图标
    def mouseMoveEvent(self, QMouseEvent):
        if Qt.LeftButton and self.m_flag:
            self.move(QMouseEvent.globalPos() - self.m_Position)  # 更改窗口位置
            QMouseEvent.accept()
    def mouseReleaseEvent(self, QMouseEvent):
        self.m_flag = False
        self.setCursor(QCursor(Qt.ArrowCursor))


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = XX()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    with open("./QSS/demo_0628.qss") as f:
        MainWindow.setStyleSheet(f.read())
    MainWindow.show()
    sys.exit(app.exec_())

principle:

#测试派生类同名函数的替代

class A:
    def __init__(self,user,password,sex='男'):
        self.user = user
        self.__password = password
        self.sex = sex
    def show(self):
        print(self.user,self.__password,self.sex,sep="\n")
    def __str__(self):
        return "[AAA]"
    __repr__ = __str__

class B(A):
    def __init__(self):
        A.__init__(self,password=123456,user=1)
    def show(self):
        print("OOXX'"+str(1234)+"'"+"OOXX")
        print('OOXX"'+str(1234)+'"'+'OOXX')
    def __str__(self):
        return "[BBB]"
    __repr__ = __str__

if __name__ == "__main__":
    b = B()
    print(b)


Complete test source

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

# Form implementation generated from reading ui file 'xxx.ui'
#
# Created by: PyQt5 UI code generator 5.12.2
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtGui import QCursor
import sys


class Ui_MainWindow(QMainWindow):

    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(690, 480)
        MainWindow.setMinimumSize(QtCore.QSize(690, 480))
        MainWindow.setMaximumSize(QtCore.QSize(690, 480))
        MainWindow.setStyleSheet("background-color: rgb(255, 255, 255);")
        MainWindow.setWindowFlag(QtCore.Qt.FramelessWindowHint)
        MainWindow.setWindowOpacity(0.9)  # 设置窗口透明度
        MainWindow.setAttribute(QtCore.Qt.WA_TranslucentBackground)  # 设置窗口背景透明

        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(200, 170, 261, 91))
        font = QtGui.QFont()
        font.setPointSize(14)
        font.setBold(True)
        font.setWeight(75)
        self.pushButton.setFont(font)
        self.pushButton.setStyleSheet("")
        self.pushButton.setObjectName("pushButton")
        self.min_button = QtWidgets.QPushButton(self.centralwidget)
        self.min_button.setGeometry(QtCore.QRect(20, 20, 21, 21))
        self.min_button.setAutoFillBackground(False)
        self.min_button.setStyleSheet("background-color: rgb(28, 255, 3);")
        self.min_button.setText("")
        self.min_button.setAutoDefault(False)
        self.min_button.setDefault(False)
        self.min_button.setFlat(False)
        self.min_button.setObjectName("min_button")
        self.max_button = QtWidgets.QPushButton(self.centralwidget)
        self.max_button.setGeometry(QtCore.QRect(50, 20, 21, 21))
        self.max_button.setStyleSheet("background-color: rgb(255, 243, 75);")
        self.max_button.setText("")
        self.max_button.setObjectName("max_button")
        self.close_button = QtWidgets.QPushButton(self.centralwidget)
        self.close_button.setGeometry(QtCore.QRect(80, 20, 21, 21))
        self.close_button.setStyleSheet("background-color: rgb(255, 12, 12);")
        self.close_button.setText("")
        self.close_button.setObjectName("close_button")

        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(0, 0, 671, 441))
        self.label.setAutoFillBackground(True)
        self.label.setStyleSheet("background-color: rgb(209, 255, 171);")
        self.label.setText("")
        self.label.setObjectName("label")

        self.label.raise_()
        self.pushButton.raise_()
        self.min_button.raise_()
        self.max_button.raise_()
        self.close_button.raise_()

        MainWindow.setCentralWidget(self.centralwidget)
        self.close_button.clicked.connect(MainWindow.close)
        self.min_button.clicked.connect(MainWindow.showMinimized)
        #self.max_button.clicked.connect(MainWindow.showMaximized)  因为窗口固定,放大功能暂时关闭

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "->点击<-"))


class XX(QMainWindow):
    def __init__(self):
        super().__init__()
    # def keyPressEvent(self, event):
    #     print("12345")
    # def mousePressEvent(self,event):
    #     if event.button() == Qt.LeftButton:
    #         print("鼠标左键点击!")
    #         # print(event.pos().x(),event.pos().y())
    #     if event.button() == Qt.RightButton:
    #         print("鼠标右键点击!")
    def mousePressEvent(self, event):
        if event.button() == Qt.LeftButton:
            self.m_flag = True
            self.m_Position = event.globalPos() - self.pos()  # 获取鼠标相对窗口的位置
            event.accept()
            self.setCursor(QCursor(Qt.OpenHandCursor))  # 更改鼠标图标
    def mouseMoveEvent(self, QMouseEvent):
        if Qt.LeftButton and self.m_flag:
            self.move(QMouseEvent.globalPos() - self.m_Position)  # 更改窗口位置
            QMouseEvent.accept()
    def mouseReleaseEvent(self, QMouseEvent):
        self.m_flag = False
        self.setCursor(QCursor(Qt.ArrowCursor))


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = XX()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    with open("./QSS/demo_0628.qss") as f:
        MainWindow.setStyleSheet(f.read())
    MainWindow.show()
    sys.exit(app.exec_())

 

Guess you like

Origin blog.csdn.net/qq_42292831/article/details/94059218