python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > PyQt5 PySide2 触摸测试

PyQt5 PySide2 触摸测试功能的实现代码

作者:cqust_qilin02811

这篇文章主要介绍了PyQt5 PySide2 触摸测试功能的实现,本文通过示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

一、前言

该测试功能是Linux产测软件的一个子功能,主要涉及:
140行代码
PySide2的Event、信号和槽、QLabel,QWidget。
QLabel实现每个小框,QWidget作为主界面

另外发现PySide2和Pyqt5没啥大区别,只要把import的包改好,代码是可以两方通用的。

手指滑动,手指坐标所在方块的颜色发生改变,如果手指划出方块区域,则所有已染色方块清空颜色,松开事件同理

实现效果:

请添加图片描述

请添加图片描述

二、实现思路:

1.使用GridLayout,绘制四周方格,方格初始化为黄色,点击到则为红色

2.重写Label,对每个label定义x,y表示所在gridlayout中的位置
如x = 1,y = 3表示label位于第一行第三列,left,right,top,bottom表示方格四条边的实际坐标

3.重写moveEvent事件和ReleaseEvent事件,
moveEvnet事件:
每次鼠标移动即触发moveEvent,则发出已经定义的信号move_signal
move_singal.connect(self.manager_touch)
manager_touch是判断当前鼠标是否位于方格内,如果在方格内则染色,如果鼠标划入空白部分,则清空所有方块颜色
另对染色label进行计数,如果达到绘制的label的总数量则发出信号返回测试通过

ReleaseEvent事件:
鼠标左键松开(对应手指离开屏幕)即触发ReleaseEvent,则清空已染色方块,另把已染色方块数量清零

4.clear_sources: 在退出页面前应将保存label的列表即self.touch_labels清空,否则重启该界面会报错

三、实现代码:

sytle.py

COLOR_RED = "color: rgb(255, 255, 255); background-color: #FF0000;"
COLOR_YELLOW = "color: rgb(255, 255, 255); background-color: #FFFF00;"
COLOR_WHITE = "color: rgb(255, 255, 255); background-color: #FFFFFF;"
COLOR_BLACK = "color: rgb(255, 255, 255); background-color: #000000;"
COLOR_GREEN = "color: rgb(255, 255, 255); background-color: #00FF00;"
COLOR_BLUE = "color: rgb(255, 255, 255); background-color:  #0000FF;"
COLOR_PURPLE = "color: rgb(255, 255, 255); background-color: #871F78;"

main.py

import sys
from functools import partial
from PySide2 import QtWidgets
from PySide2.QtCore import Qt, Signal
from PySide2.QtGui import QCursor
from PySide2.QtWidgets import QGridLayout, QPushButton, QWidget, QApplication
import style
class TouchLabel(QtWidgets.QLabel):
    def __init__(self, i, j, top, bottom, left, right, target, parent=None):
        super(TouchLabel, self).__init__(parent)
        self.setStyleSheet(style.COLOR_YELLOW)
        self.flag = False  # 如果被滑过则置为True
        self.target = target
        self.x = i  # x,y 代表在gridLayout中的位置
        self.y = j

        self.flag = False
        self.left = left
        self.right = right
        self.top = top
        self.bottom = bottom


class TouchWidget(QWidget):
    touch_labels = []  # 存储label的列表
    sum_moved_labels = 0
    move_signal = Signal()
    out_signal = Signal()
    release_signal = Signal()

    def __init__(self):
        super().__init__()
        self.sum_labels = 0  # 统计染色的方块个数
        self.target = 0
        print("start TouchWidget")
        self.setWindowFlags(Qt.FramelessWindowHint)
        self.showFullScreen()
        self.init_parameters()  # 初始化gridlayout的参数
        print("绘制的按钮个数为:" + str(self.target))

    def init_parameters(self):
        self.layout = QGridLayout(self)
        self.layout.setMargin(0)  # 设置widget离窗口的距离
        self.layout.setSpacing(0)  # 设置控件间距
        self.row = 10  # 纵向的按钮数量
        self.column = 10  # 横向的按钮数量
        desktop = QtWidgets.QApplication.desktop()
        global label_width, label_height
        label_width = desktop.width() / self.column  # 列宽
        label_height = desktop.height() / self.row  # 行高
        self.target = self.row * 2 + self.column * 2 - 4

        self.touch_labels.clear()
        self.init_touch_label(self.row, self.column, label_height, label_width)
        self.add_touch_label(label_width, label_height)

    def init_touch_label(self, row, column, label_height, label_width):
        desktop = QtWidgets.QApplication.desktop()
        height = desktop.height()
        width = desktop.width()
        for i in range(row):
            for j in range(column):
                if i == 0 and j == 0:
                    label = TouchLabel(i, j, 0, label_height, 0, label_width, self.target)
                    self.touch_labels.append(label)
                elif i == 0 and j != 0:
                    label = TouchLabel(i, j, 0, label_height, j * label_width, (j + 1) * label_width, self.target)
                    self.touch_labels.append(label)
                elif i != 0 and j == 0:
                    label = TouchLabel(i, j, i * label_height, (i + 1) * label_height, 0, label_width, self.target)
                    self.touch_labels.append(label)
                elif i == row - 1 and j != 0:
                    label = TouchLabel(i, j, height - label_height, height, j * label_width, (j + 1) * label_width,
                                       self.target)
                    self.touch_labels.append(label)
                elif j == column - 1 and i != 0:
                    label = TouchLabel(i, j, i * label_height, (i + 1) * label_height, width - label_width, width,
                                       self.target)
                    self.touch_labels.append(label)

    def add_touch_label(self, label_width, label_height):
        for label in self.touch_labels:
            self.layout.addWidget(label, label.x, label.y, 1, 1)
        nopass_quit_btn = QPushButton("如果测试不通过,点击此按钮退出")
        nopass_quit_btn.setMinimumSize(label_width, label_height)
        self.layout.addWidget(nopass_quit_btn, self.row / 2 - 1, self.column / 2 - 1, 3, 3)
        nopass_quit_btn.clicked.connect(partial(self.on_result, "TEST NOT PASS"))

        self.move_signal.connect(self.manager_touch)
        self.out_signal.connect(partial(self.on_result, "TEST PASS"))
        self.release_signal.connect(self.clear_label_status)

    def manager_touch(self):
        desktop = QtWidgets.QApplication.desktop()
        frontier_top = label_height
        frontier_bottom = desktop.height() - label_height
        frontier_left = label_width
        frontier_right = desktop.width() - label_width
        x = QCursor.pos().x()
        y = QCursor.pos().y()

        if frontier_left <= x <= frontier_right and frontier_top <= y <= frontier_bottom:
            self.clear_label_status()
            print("手指划出边界,清空所有方块颜色")

        for label in self.touch_labels:
            if label.left <= x <= label.right and label.top <= y <= label.bottom:
                if label.flag:
                    continue
                else:
                    label.setStyleSheet(style.COLOR_RED)
                    TouchWidget.sum_moved_labels += 1
                    label.flag = True
            if TouchWidget.sum_moved_labels == self.target:
                self.out_signal.emit()

    def clear_label_status(self):
        for label in self.touch_labels:
            label.setStyleSheet(style.COLOR_YELLOW)
            label.flag = False
        TouchWidget.sum_moved_labels = 0

    def clear_sources(self):
        TouchWidget.sum_moved_labels = 0
        self.touch_labels.clear()
        self.sum_moved_labels = 0
        self.target = 0

    def mouseMoveEvent(self, event):
        self.move_signal.emit()

    def mouseReleaseEvent(self, event):
        self.release_signal.emit()
        print("释放焦点,清空所有方块颜色")

    def on_result(self, ret):
        self.clear_sources()
        print(ret)
        self.close()
if __name__ == '__main__':
    app = QApplication()
    widget = TouchWidget()
    widget.show()
    sys.exit(app.exec_())

到此这篇关于PyQt5 PySide2 触摸测试功能的实现的文章就介绍到这了,更多相关PyQt5 PySide2 触摸测试内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文