PyQt5实战笔记

Label的文字字体水平垂直居中

self.wait_lab.setAlignment(QtCore.Qt.AlignCenter)
self.wait_lab.setAlignment(QtCore.Qt.AlignHCenter|QtCore.Qt.AlignVCenter)

使用QSS设置样式
Qt Style Sheets Examples
Pyqt QSS简单的Ui美化
Qt css样式大全

        self.wait_lab.setStyleSheet("#wait_lab{\
                                        background-color: rgba(0, 0, 0, 0);\
                                        font-weight:Regular; \
                                        font-family:SourceHanSansCN-Regular;\
                                        font-size:36px;\
                                        line-height:7px;\
                                        color:#FFFFFF;\
                                        text-align:center; \
                                        }")

在QWidget中使用QSS需要重写绘图事件

    def paintEvent(self, event):
        """
        重写paintEvent 否则不能使用样式表定义外观
        :param evt:
        :return:
        """
        opt = QtWidgets.QStyleOption()
        opt.initFrom(self)
        painter = QtGui.QPainter(self)
        # 反锯齿
        painter.setRenderHint(QtGui.QPainter.Antialiasing)
        self.style().drawPrimitive(QtWidgets.QStyle.PE_Widget, opt, painter, self)

将QSS与界面逻辑分离,需要编写单独的 .qss文件:

QLabel{
    background-color:rgba(0,0,0,0);
    border-image:url(./Resource/img/stamper.png);
}

#lab_title{
    border-image:url(./Resource/Pic/background.png);
}

#btn_1:pressed{
    border-image:url(./Resource/img/btn_down.png);
}

QPushButton{
    background-color:rgba(0,0,0,0);
    border-image:url(./Resource/img/btn_up.png);
    font-family:SourceHanSansCN-Regular;
    font-weight:Regular;
    text-align:left;
    color:#FFFFFF;
    font-size:24px;
}
# 用于读取 .qss文件
class CommonHelper:
    def __init__(self):
        pass

    @staticmethod
    def readQss(style):
        with open(style, 'r') as f:
            return f.read()

# 读取 .qss文件
self.styleFile = "./Resource/QSS/style.qss"
style = CommonHelper.readQss(self.styleFile)
Form.setStyleSheet(style)

设置rgba,背景颜色透明
opacity、rgb、rgba透明背景

self.setStyleSheet("#wait{\
                        background-color: rgba(0, 0, 0, 0);\
                        border-image:url(./Resource/Pic/入纸确认/wait_lab.png);}")

QCamera调用摄像头
Qt Multimedia
QCameraViewfinder Class
QCameraImageCapture Class
qt 使用QCamera 实现简单的摄像头使用

import sys
import time

from PyQt5.QtWidgets import QApplication, QWidget, QLabel
from PyQt5.QtMultimedia import QCamera, QCameraInfo, QCameraImageCapture, QAudioRecorder, QMediaRecorder
from PyQt5.QtMultimedia import QAudioEncoderSettings
from PyQt5.QtMultimediaWidgets import QCameraViewfinder



class Widget(QWidget):

    def __init__(self, parent=None):
        super(Widget, self).__init__(parent)

        self.resize(400, 400)

        self.camerainfo = QCameraInfo()
        self.Camera = QCamera()     # 读取摄像头
        self.Viewfinder = QCameraViewfinder()    # 提供一个相机取景器部件


def Camera():
    camera = QCamera()  # 设置摄像头
    Viewfinder = QCameraViewfinder()  # 提供一个相机取景器部件
    # cameraImage = QCameraImageCapture(camera)  # 获取摄像头当前帧

    Viewfinder.show()
    camera.setViewfinder(Viewfinder)
    camera.setCaptureMode(QCamera.CaptureStillImage)
    camera.start()



if __name__ == "__main__":
    app = QApplication(sys.argv)
    # win = Camera()
    camera = QCamera()  # 设置摄像头
    Viewfinder = QCameraViewfinder()  # 提供一个相机取景器部件
    Viewfinder.show()
    camera.setViewfinder(Viewfinder)

    cameraImage = QCameraImageCapture(camera)  # 获取摄像头当前帧

    camera.setCaptureMode(QCamera.CaptureStillImage)
    camera.start()

    # time.sleep(2)
    camera.searchAndLock()

    cameraImage.capture()

    camera.unlock()

    sys.exit(app.exec())


# camerainfo = QCameraInfo()
# print("可用的摄像头:", camerainfo.availableCameras())
# print(camera.availableDevices())

你可能感兴趣的:(python,Pyqt5)