Introduction to PyQt5 (twenty-three) window, drawing and special effects

table of Contents

1. Set the style of the controls in the window

2. Set the window style

Three. Use code to set the window to maximize and minimize

4. Project actual combat: realize drawing application

Five. Create transparent and semi-transparent windows


1. Set the style of the controls in the window

Code:

import sys
from PyQt5.QtWidgets import *
from PyQt5 import QtCore


#窗口可以显示的风格样式
print('窗口可以显示的风格样式:',QStyleFactory.keys())

class WindowStyle(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle('设置窗口风格')
        horizontalLayout = QHBoxLayout()
        self.styleLabel = QLabel('设置窗口风格:')
        #下拉框
        self.styleComboBox = QComboBox()
        self.styleComboBox.addItems(QStyleFactory.keys())

        # 获取当前窗口的风格
        print('当前窗口的风格:',QApplication.style().objectName())
        #获取当前窗口的风格的索引
        index = self.styleComboBox.findText(QApplication.style().objectName(),QtCore.Qt.MatchFixedString)
        #将下拉框初始设置为当前窗口的风格的名字
        self.styleComboBox.setCurrentIndex(index)

        self.styleComboBox.activated[str].connect(self.handleStyleChanged)

        horizontalLayout.addWidget(self.styleLabel)
        horizontalLayout.addWidget(self.styleComboBox)
        self.setLayout(horizontalLayout)

    def handleStyleChanged(self,style):
        #设置风格
        QApplication.setStyle(style)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    form = WindowStyle()
    form.show()
    sys.exit(app.exec_())

operation result:

You will see that the style of the space will change.

 

2. Set the window style

setWindowFlags (Qt::WindowFlags type)
Qt::FrameWindowHint: a bai window without a border du
Qt::WindowStaysOnTopHint://always on the top of the window
Qt::CustomizeWindowHint://customize the window title bar, the following mark is marked It must be used together with this flag to be valid, otherwise the window will have a default title bar
Qt::WindowTitleHint: display the window title bar
Qt::WindowSystemMenuHint://display the system menu
Qt::WindowMinimizeButtonHint://display the minimize button
Qt::WindowMaximizeButtonHint://display maximize button
Qt::WindowMinMaxButtonsHint://display minimize button and maximize button
Qt::WindowCloseButtonHint://display close button
setWindowFlags(Qt::FramelessWindowHint); hide it directly

Code:

'''
设置窗口样式(主要是窗口边框、标题栏以及窗口本身的样式)
'''

from PyQt5.QtCore import *
import sys
from PyQt5.QtWidgets import *

class WindowPattern(QMainWindow):
    def __init__(self):
        super().__init__()
        self.resize(500,260)
        self.setWindowTitle('设置窗口的样式')
        #设置窗口的样式 (hint暗示,示意)
        self.setWindowFlags(Qt.WindowMaximizeButtonHint | Qt.WindowStaysOnTopHint | Qt.FramelessWindowHint)#显示最大化按钮,窗口永远在最前端且无边框
        #设置窗口的名字
        self.setObjectName("MainWindow")
        #背景图,后面会讲,设置语法类似CSS
        self.setStyleSheet("#MainWindow{border-image:url(../picture/fairy.jpg);}")

if __name__ == "__main__":
    app = QApplication(sys.argv)
    form = WindowPattern()
    form.show()
    sys.exit(app.exec_())

operation result:

 

Three. Use code to set the window to maximize and minimize

Code:

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *


### 自定义窗口类
class WindowMaxMin(QWidget):
    #  构造函数
    def __init__(self, parent=None):
        '''构造函数'''
        # 调用父类构造函数
        super(WindowMaxMin, self).__init__(parent)
        self.resize(300,400)
        self.setWindowTitle("用代码控制窗口的最大化和最小化")
        self.setWindowFlags(Qt.WindowMaximizeButtonHint)

        layout = QVBoxLayout()
        maxButton1 = QPushButton()
        maxButton1.setText('窗口最大化1')
        maxButton1.clicked.connect(self.maximized1)

        maxButton2 = QPushButton()
        maxButton2.setText('窗口最大化2')
        maxButton2.clicked.connect(self.showMaximized)

        minButton = QPushButton()
        minButton.setText('窗口最小化')
        minButton.clicked.connect(self.showMinimized)

        layout.addWidget(maxButton1)
        layout.addWidget(maxButton2)
        layout.addWidget(minButton)
        self.setLayout(layout)


    def maximized1(self):
        #获得桌面
        desktop = QApplication.desktop()
        # 获取桌面可用尺寸
        rect = desktop.availableGeometry()
        self.setGeometry(rect)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = WindowMaxMin()
    window.show()
    # 应用程序事件循环
    sys.exit(app.exec_())

operation result:

Try it yourself!

 

4. Project actual combat: realize drawing application

Need to solve 3 core content
1. How to draw

Drawing in the paintEvent method, trigger the call of painEvent by calling the update method

2. Where to draw

Draw in the QPixmap object on a white background

3. How to draw by moving the mouse

The mouse has 3 events:
(1) Mouse press: mousePressEvent
(2) Mouse movement: mouseMoveEvent
(3) Mouse up: mouseReleaseEvent

 

Code:

import sys
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtGui import QPainter, QPixmap
from PyQt5.QtCore import Qt, QPoint


class Drawing(QWidget):
    def __init__(self, parent=None):
        super(Drawing, self).__init__(parent)
        self.setWindowTitle("绘图应用")
        self.pix = QPixmap()
        self.lastPoint = QPoint()
        self.endPoint = QPoint()
        self.initUi()

    def initUi(self):

        self.resize(600, 600)
        '''
        QPixmap 类用于绘图设备的图像显示,可作为一个 QPaintDevice 对象,也可以加载到一个控件中。
        QPixmap 可以读取的图像文件类型有:BMP、GIF、JPG、JPEG、PNG、PBM、PGM、PPM、XBM、XPM 等。
        '''
        # 画布大小为600*600,背景为白色
        self.pix = QPixmap(600, 600)
        self.pix.fill(Qt.white)

    #下面的方法都是自动调用的
    def paintEvent(self, event):
        #画笔
        pp = QPainter(self.pix)
        # 根据鼠标指针前后两个位置绘制直线
        pp.drawLine(self.lastPoint, self.endPoint)
        # 让前一个坐标值等于后一个坐标值,
        # 这样就能实现画出连续的线
        self.lastPoint = self.endPoint
        painter = QPainter(self)
        #pixmap (n.)像素映射,象图
        #即先画在QPixmap上,再从图像文件中提取 Pixmap 并将其显示在指定位置
        painter.drawPixmap(0,0,self.pix)

    #鼠标按下
    def mousePressEvent(self, event):
        #如果鼠标左键按下
        if event.button() == Qt.LeftButton:
            # 获得当前坐标
            self.lastPoint = event.pos()

    #鼠标移动
    def mouseMoveEvent(self, event):
        #如果鼠标左键一直按着
        if event.buttons() and Qt.LeftButton:
            self.endPoint = event.pos()
            self.update()#触发paintEvent

    # 鼠标抬起
    def mouseReleaseEvent(self, event):
        # 鼠标左键释放
        if event.button() == Qt.LeftButton:
            self.endPoint = event.pos()
            # 进行重新绘制
            self.update()#每次调用paintEvent都会重新触发一次


if __name__ == "__main__":
    app = QApplication(sys.argv)
    form = Drawing()
    form.show()
    sys.exit(app.exec_())


operation result:

It's a shame to draw it easily.

 

Five. Create transparent and semi-transparent windows

Code:

from PyQt5.Qt import *
import sys

if __name__ == "__main__":
    app = QApplication(sys.argv)
    win = QMainWindow()
    win.setWindowTitle('窗口的透明度设置')
    # 0到1,1表示不透明,0表示完全透明
    win.setWindowOpacity(0.6)

    button = QPushButton('我的按钮',win)

    win.resize(400,200)
    win.show()
    sys.exit(app.exec())

operation result:

Because it is transparent, you can see the code behind the window.

Guess you like

Origin blog.csdn.net/weixin_44593822/article/details/113819077