本文主要是介绍python+PyQt5 左右声道测试,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
UI:
源代码:
# -*- coding: utf-8 -*-# Form implementation generated from reading ui file 'MicrophoneWinFrm.ui'
#
# Created by: PyQt5 UI code generator 5.15.2
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.Qt import *
from PyQt5 import QtWidgets
from PyQt5 import QtGui
from PyQt5 import QtCore
import logging
import os
import json
import configparser
from PyQt5.QtCore import pyqtSignal
import sys
import random#随机数
import subprocess
import inspect
from subprocess import Popen,PIPE
from colorama import Fore,Style
import re
#pip install colorama
import pygame
#pip install pygameclass Mic_ChannelTest(QtCore.QThread):test_result_signal = QtCore.pyqtSignal(tuple) # 新增信号,传递一个包含设备路径和测试结果的元组finished_signal = QtCore.pyqtSignal() # 新增信号,表示线程完成def __init__(self, parent=None):super(Mic_ChannelTest, self).__init__(parent)self.is_running=Truedef run(self):self.parent().test_AudioChannel()#启动多线程self.finished_signal.emit()#线程完成时发出信号self.stop()def stop(self):self.is_running = False # 或者使用更安全的停止逻辑class Ui_Form(QWidget):updateTimer = pyqtSignal(bool) # 时间线程启动器def __init__(self):super().__init__()self.config = configparser.ConfigParser() # 创建对象self.itemName = '' # 项目名称self.testArgs = [] # 测试参数信息self.testStandardArgs = '' # 测试准标参数self.Err = '' # 错误信息self.cfgArgs = [] # 測試參數self.leftChannel_TestPass=False#左声道测试self.rigthChannel_TestPass=False#右声道测试self.leftChannelSoundFile=''#左声道音频文件self.rightChannelSoundFile=''#右声道音频文件self.nowTest='leftChannel'#默认左声道测试self.randNum=1#随机数# 生成日志信息self.logger = logging.getLogger('my_logger') # 步骤1 创建日志记录器self.logger.setLevel(logging.DEBUG) # 步骤2 将指定日志级别self.file_handler = logging.FileHandler('./log/log.txt') # 步骤3 创建文件处理器self.formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s') # 步骤4 创建格式化器self.file_handler.setFormatter(self.formatter) # 步骤4 将格式化器添加到处理器self.logger.addHandler(self.file_handler) # 步骤5 将处理器添加到日志记录器# 读取配置self.config.read('./Conf/config.conf', encoding='utf-8') # 读取配置文件,如果配置文件不存在则创建#左右声道音频文件self.leftChannelSoundFile=self.config.get('MicrophoneConfig','LeftChannel')#左声道音频文件self.rightChannelSoundFile=self.config.get('MicrophoneConfig','RightChannel')#右声道音频文件# 读取测试配置self.itemName = self.config.get('TestItemNameArrays', 'MicTest') # 项目名称self.itemFailSleepExit = int(self.config.get('TestItemWinFrmSleepExit', 'MicTest')) # 项目测试Fail延时退出self.testArgs.clear()self.testArgs = self.ReadJsonInfo('./Conf/TestArgs.json') # 读取测试参数信息self.setupUi()self.lbl_CurrentSwTest.setText('读取配置信息...')#将变量添加到容器中self.left_buttons = [self.bp_Left_1,self.bp_Left_2,self.bp_Left_3,self.bp_Left_4,self.bp_Left_5,self.bp_Left_6,self.bp_Left_7,self.bp_Left_8]#左声道控件self.right_buttons=[self.bp_Rigth_1,self.bp_Rigth_2,self.bp_Rigth_3,self.bp_Rigth_4,self.bp_Rigth_5,self.bp_Rigth_6,self.bp_Rigth_7,self.bp_Rigth_8]#右声道控件for lbut in self.left_buttons:#遍历左声道控件lbut.clicked.connect(self.on_left_button_clicked)#添加左击事件lbut.setEnabled(False)for rbut in self.right_buttons:#遍历右声道控件rbut.clicked.connect(self.on_right_button_clicked)#添加右击事件rbut.setEnabled(False)if self.ReadJsonTestArgs(self.itemName) == True: # 讀取標準參數test=[]#测试参数testArgs=self.testStandardArgs.split('|')#print(self.testStandardArgs)if testArgs[0][testArgs[0].find('=')+1:]=='True':self.lbl_LeftChannel_2.setText('True')self.lbl_LeftChannel_2.setStyleSheet('background-color: rgb(85, 255, 127);')self.label_8.setStyleSheet('background-color: rgb(85, 255, 127);')if testArgs[1][testArgs[1].find('=')+1:]=='True':self.lbl_RightChannel.setText('True')self.lbl_RightChannel.setStyleSheet('background-color: rgb(85, 255, 127);')self.label_8.setStyleSheet('background-color: rgb(85, 255, 127);')#创建一个定时器来检查左右声道音频诊断测试self.timer = QTimer(self)self.timer.setInterval(1000) # 每秒检查一次self.timer.timeout.connect(self.check_Test)self.timer.start()# 连接信号到槽self.updateTimer.connect(self.handleTimer)#左键单击事件def on_left_button_clicked(self):button = self.sender()button.setStyleSheet('background-color: rgb(85, 255, 127);')# 假设我们根据按钮的编号来改变颜色button_number = self.left_buttons.index(button) + 1print(self.randNum,button_number)if self.randNum==button_number:self.leftChannel_TestPass=Trueself.nowTest='rigthChannel'for lbt in self.left_buttons:lbt.setEnabled(False)self.handleTimer(True); # 启动线程#右键单击事件def on_right_button_clicked(self):button=self.sender()button.setStyleSheet('background-color: rgb(85, 255, 127);')# 假设我们根据按钮的编号来改变颜色button_number = self.right_buttons.index(button) + 1if self.randNum==button_number:self.rigthChannel_TestPass=Truefor lbt in self.right_buttons:lbt.setEnabled(False)self.handleTimer(True); # 启动线程#启动多线程def start_analysis_threads(self):# 创建多个线程self.handleTimer(False) # 停止进程self.finished_threads = 0 # 重置计数器thread=Mic_ChannelTest(self)thread.start()#执行跑线程#判断启动随机播放音频def test_AudioChannel(self):self.randNum = random.randint(1, 8) # 生成1到10之间的随机整数if self.leftChannel_TestPass == True: # 左声道测试PASSself.label_2.setText('右声道:人机交互测试,点击左边听到的对应序号按钮..')self.lbl_CurrentSwTest.setText('音频右声道测试..')for rbut in self.right_buttons: # 遍历右声道控件rbut.setStyleSheet('')rbut.setEnabled(True)# rbut.setStyleSheet('background-color: rgb(85, 255, 127);')self.nowTest = 'leftChannel' # 当前左声道测试self.play_mp3(f'{self.rightChannelSoundFile}') # 左声道播放self.play_mp3(f'Microphone/R{str(self.randNum)}.mp3') # 左声道随机数播放else:self.label_2.setText('左声道:人机交互测试,点击右边听到的对应序号按钮..')self.lbl_CurrentSwTest.setText('音频左声道测试..')for lbut in self.left_buttons: # 遍历左声道控件lbut.setEnabled(True)lbut.setStyleSheet('')# lbut.setStyleSheet('background-color: rgb(85, 255, 127);')self.nowTest = 'rightChannel' # 当前右声道测试self.play_mp3(f'{self.leftChannelSoundFile}') # 右声道播放self.play_mp3(f'Microphone/L{str(self.randNum)}.mp3') # 右声道随机数播放#if self.lbl_LeftChannel_2.text()=='True' and self.lbl_RightChannel.text()=='True':#播放MP3def play_mp3(self,file_path):try:print('file_path',file_path)pygame.mixer.init()pygame.mixer.music.load(file_path)pygame.mixer.music.play()# 等待音乐播放完成while pygame.mixer.music.get_busy():pygame.time.Clock().tick(10)except Exception as e:print(f'play mp3 Err:{e}')sys.exit(1)# 校验是否完成测试def check_Test(self):if self.lbl_LeftChannel_2.text()=='True' and self.lbl_RightChannel.text()=='True':if self.leftChannel_TestPass==True and self.rigthChannel_TestPass==True:self.UpdateJsonTestArgs(self.itemName,'LeftChannel=True|RightChannel=True','PASS')#更新测试信息self.ShowLog('测试PASS', True)sys.exit(0)elif self.lbl_LeftChannel_2.text()=='True':if self.leftChannel_TestPass==True:self.UpdateJsonTestArgs(self.itemName, 'LeftChannel=True', 'PASS') # 更新测试信息self.ShowLog('测试PASS', True)sys.exit(0)elif self.lbl_RightChannel.text()=='True':if self.rigthChannel_TestPass==True:self.UpdateJsonTestArgs(self.itemName, 'RightChannel=True', 'PASS') # 更新测试信息self.ShowLog('测试PASS', True)sys.exit(0)#self.updateTimer.emit(False) # 重新启动定时器self.lbl_CurrentSwTest.setText('音频测试中..')self.start_analysis_threads()# 定义触发器def handleTimer(self, start):if start:self.timer.start()else:self.timer.stop()def setupUi(self):self.setObjectName("Form")self.resize(678, 737)icon = QtGui.QIcon()icon.addPixmap(QtGui.QPixmap("IMAGE/Mic.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)self.setWindowIcon(icon)self.gridLayout = QtWidgets.QGridLayout(self)self.gridLayout.setObjectName("gridLayout")self.verticalLayout = QtWidgets.QVBoxLayout()self.verticalLayout.setObjectName("verticalLayout")self.horizontalLayout = QtWidgets.QHBoxLayout()self.horizontalLayout.setObjectName("horizontalLayout")self.verticalLayout_2 = QtWidgets.QVBoxLayout()self.verticalLayout_2.setObjectName("verticalLayout_2")self.horizontalLayout_4 = QtWidgets.QHBoxLayout()self.horizontalLayout_4.setObjectName("horizontalLayout_4")self.lbl_Logo = QtWidgets.QLabel(self)self.lbl_Logo.setText("")self.lbl_Logo.setPixmap(QtGui.QPixmap("IMAGE/logo.jpg"))self.lbl_Logo.setAlignment(QtCore.Qt.AlignCenter)self.lbl_Logo.setObjectName("lbl_Logo")self.horizontalLayout_4.addWidget(self.lbl_Logo)self.label = QtWidgets.QLabel(self)font = QtGui.QFont()font.setPointSize(12)self.label.setFont(font)self.label.setStyleSheet("background-color: rgb(170, 170, 127);")self.label.setAlignment(QtCore.Qt.AlignCenter)self.label.setObjectName("label")self.horizontalLayout_4.addWidget(self.label)self.verticalLayout_2.addLayout(self.horizontalLayout_4)self.horizontalLayout_5 = QtWidgets.QHBoxLayout()self.horizontalLayout_5.setObjectName("horizontalLayout_5")self.lbl_CurrentSwTest = QtWidgets.QLabel(self)font = QtGui.QFont()font.setPointSize(12)self.lbl_CurrentSwTest.setFont(font)self.lbl_CurrentSwTest.setStyleSheet("background-color: rgb(85, 255, 127);")self.lbl_CurrentSwTest.setAlignment(QtCore.Qt.AlignCenter)self.lbl_CurrentSwTest.setObjectName("lbl_CurrentSwTest")self.horizontalLayout_5.addWidget(self.lbl_CurrentSwTest)self.verticalLayout_2.addLayout(self.horizontalLayout_5)self.horizontalLayout.addLayout(self.verticalLayout_2)self.verticalLayout_3 = QtWidgets.QVBoxLayout()self.verticalLayout_3.setObjectName("verticalLayout_3")self.horizontalLayout_6 = QtWidgets.QHBoxLayout()self.horizontalLayout_6.setObjectName("horizontalLayout_6")self.label_5 = QtWidgets.QLabel(self)font = QtGui.QFont()font.setPointSize(12)self.label_5.setFont(font)self.label_5.setStyleSheet("background-color: rgb(255, 170, 127);")self.label_5.setAlignment(QtCore.Qt.AlignCenter)self.label_5.setObjectName("label_5")self.horizontalLayout_6.addWidget(self.label_5)self.verticalLayout_4 = QtWidgets.QVBoxLayout()self.verticalLayout_4.setObjectName("verticalLayout_4")self.label_4 = QtWidgets.QLabel(self)font = QtGui.QFont()font.setPointSize(12)self.label_4.setFont(font)self.label_4.setStyleSheet("background-color: rgb(255, 170, 127);")self.label_4.setAlignment(QtCore.Qt.AlignCenter)self.label_4.setObjectName("label_4")self.verticalLayout_4.addWidget(self.label_4)self.horizontalLayout_8 = QtWidgets.QHBoxLayout()self.horizontalLayout_8.setObjectName("horizontalLayout_8")self.label_7 = QtWidgets.QLabel(self)font = QtGui.QFont()font.setPointSize(12)self.label_7.setFont(font)self.label_7.setStyleSheet("background-color: rgb(255, 170, 127);")self.label_7.setAlignment(QtCore.Qt.AlignCenter)self.label_7.setObjectName("label_7")self.horizontalLayout_8.addWidget(self.label_7)self.label_6 = QtWidgets.QLabel(self)font = QtGui.QFont()font.setPointSize(12)self.label_6.setFont(font)self.label_6.setStyleSheet("background-color: rgb(255, 170, 127);")self.label_6.setAlignment(QtCore.Qt.AlignCenter)self.label_6.setObjectName("label_6")self.horizontalLayout_8.addWidget(self.label_6)self.verticalLayout_4.addLayout(self.horizontalLayout_8)self.horizontalLayout_6.addLayout(self.verticalLayout_4)self.verticalLayout_3.addLayout(self.horizontalLayout_6)self.horizontalLayout_7 = QtWidgets.QHBoxLayout()self.horizontalLayout_7.setObjectName("horizontalLayout_7")self.horizontalLayout_9 = QtWidgets.QHBoxLayout()self.horizontalLayout_9.setObjectName("horizontalLayout_9")self.label_8 = QtWidgets.QLabel(self)font = QtGui.QFont()font.setPointSize(12)self.label_8.setFont(font)self.label_8.setAlignment(QtCore.Qt.AlignCenter)self.label_8.setObjectName("label_8")self.horizontalLayout_9.addWidget(self.label_8)self.horizontalLayout_7.addLayout(self.horizontalLayout_9)self.horizontalLayout_10 = QtWidgets.QHBoxLayout()self.horizontalLayout_10.setObjectName("horizontalLayout_10")self.lbl_LeftChannel_2 = QtWidgets.QLabel(self)font = QtGui.QFont()font.setPointSize(12)self.lbl_LeftChannel_2.setFont(font)self.lbl_LeftChannel_2.setAlignment(QtCore.Qt.AlignCenter)self.lbl_LeftChannel_2.setObjectName("lbl_LeftChannel_2")self.horizontalLayout_10.addWidget(self.lbl_LeftChannel_2)self.lbl_RightChannel = QtWidgets.QLabel(self)font = QtGui.QFont()font.setPointSize(12)self.lbl_RightChannel.setFont(font)self.lbl_RightChannel.setAlignment(QtCore.Qt.AlignCenter)self.lbl_RightChannel.setObjectName("lbl_RightChannel")self.horizontalLayout_10.addWidget(self.lbl_RightChannel)self.horizontalLayout_7.addLayout(self.horizontalLayout_10)self.verticalLayout_3.addLayout(self.horizontalLayout_7)self.horizontalLayout.addLayout(self.verticalLayout_3)self.verticalLayout.addLayout(self.horizontalLayout,1)self.horizontalLayout_2 = QtWidgets.QHBoxLayout()self.horizontalLayout_2.setObjectName("horizontalLayout_2")self.groupBox = QtWidgets.QGroupBox(self)font = QtGui.QFont()font.setPointSize(12)self.groupBox.setFont(font)self.groupBox.setAlignment(QtCore.Qt.AlignCenter)self.groupBox.setObjectName("groupBox")self.gridLayout_3 = QtWidgets.QGridLayout(self.groupBox)self.gridLayout_3.setObjectName("gridLayout_3")self.verticalLayout_5 = QtWidgets.QVBoxLayout()self.verticalLayout_5.setObjectName("verticalLayout_5")self.horizontalLayout_11 = QtWidgets.QHBoxLayout()self.horizontalLayout_11.setObjectName("horizontalLayout_11")self.label_13 = QtWidgets.QLabel(self.groupBox)self.label_13.setText("")self.label_13.setObjectName("label_13")self.horizontalLayout_11.addWidget(self.label_13)font = QtGui.QFont()font.setPointSize(18)self.bp_Left_1 = QtWidgets.QPushButton(self.groupBox)self.bp_Left_1.setObjectName("bp_Left_1")self.bp_Left_1.setFont(font)self.horizontalLayout_11.addWidget(self.bp_Left_1)self.label_12 = QtWidgets.QLabel(self.groupBox)self.label_12.setText("")self.label_12.setObjectName("label_12")self.horizontalLayout_11.addWidget(self.label_12)self.verticalLayout_5.addLayout(self.horizontalLayout_11)self.horizontalLayout_12 = QtWidgets.QHBoxLayout()self.horizontalLayout_12.setObjectName("horizontalLayout_12")self.label_14 = QtWidgets.QLabel(self.groupBox)self.label_14.setText("")self.label_14.setObjectName("label_14")self.horizontalLayout_12.addWidget(self.label_14)self.bp_Left_2 = QtWidgets.QPushButton(self.groupBox)self.bp_Left_2.setObjectName("bp_Left_2")self.bp_Left_2.setFont(font)self.horizontalLayout_12.addWidget(self.bp_Left_2)self.label_21 = QtWidgets.QLabel(self.groupBox)self.label_21.setText("")self.label_21.setObjectName("label_21")self.horizontalLayout_12.addWidget(self.label_21)self.verticalLayout_5.addLayout(self.horizontalLayout_12)self.horizontalLayout_13 = QtWidgets.QHBoxLayout()self.horizontalLayout_13.setObjectName("horizontalLayout_13")self.label_15 = QtWidgets.QLabel(self.groupBox)self.label_15.setText("")self.label_15.setObjectName("label_15")self.horizontalLayout_13.addWidget(self.label_15)self.bp_Left_3 = QtWidgets.QPushButton(self.groupBox)self.bp_Left_3.setObjectName("bp_Left_3")self.bp_Left_3.setFont(font)self.horizontalLayout_13.addWidget(self.bp_Left_3)self.label_22 = QtWidgets.QLabel(self.groupBox)self.label_22.setText("")self.label_22.setObjectName("label_22")self.horizontalLayout_13.addWidget(self.label_22)self.verticalLayout_5.addLayout(self.horizontalLayout_13)self.horizontalLayout_24 = QtWidgets.QHBoxLayout()self.horizontalLayout_24.setObjectName("horizontalLayout_24")self.label_30 = QtWidgets.QLabel(self.groupBox)self.label_30.setText("")self.label_30.setObjectName("label_30")self.horizontalLayout_24.addWidget(self.label_30)self.bp_Left_4 = QtWidgets.QPushButton(self.groupBox)self.bp_Left_4.setObjectName("bp_Left_4")self.bp_Left_4.setFont(font)self.horizontalLayout_24.addWidget(self.bp_Left_4)self.label_3 = QtWidgets.QLabel(self.groupBox)self.label_3.setText("")self.label_3.setObjectName("label_3")self.horizontalLayout_24.addWidget(self.label_3)self.verticalLayout_5.addLayout(self.horizontalLayout_24)self.horizontalLayout_23 = QtWidgets.QHBoxLayout()self.horizontalLayout_23.setObjectName("horizontalLayout_23")self.label_31 = QtWidgets.QLabel(self.groupBox)self.label_31.setText("")self.label_31.setObjectName("label_31")self.horizontalLayout_23.addWidget(self.label_31)self.bp_Left_5 = QtWidgets.QPushButton(self.groupBox)self.bp_Left_5.setObjectName("bp_Left_5")self.bp_Left_5.setFont(font)self.horizontalLayout_23.addWidget(self.bp_Left_5)self.label_11 = QtWidgets.QLabel(self.groupBox)self.label_11.setText("")self.label_11.setObjectName("label_11")self.horizontalLayout_23.addWidget(self.label_11)self.verticalLayout_5.addLayout(self.horizontalLayout_23)self.horizontalLayout_22 = QtWidgets.QHBoxLayout()self.horizontalLayout_22.setObjectName("horizontalLayout_22")self.label_32 = QtWidgets.QLabel(self.groupBox)self.label_32.setText("")self.label_32.setObjectName("label_32")self.horizontalLayout_22.addWidget(self.label_32)self.bp_Left_6 = QtWidgets.QPushButton(self.groupBox)self.bp_Left_6.setObjectName("bp_Left_6")self.bp_Left_6.setFont(font)self.horizontalLayout_22.addWidget(self.bp_Left_6)self.label_28 = QtWidgets.QLabel(self.groupBox)self.label_28.setText("")self.label_28.setObjectName("label_28")self.horizontalLayout_22.addWidget(self.label_28)self.verticalLayout_5.addLayout(self.horizontalLayout_22)self.horizontalLayout_21 = QtWidgets.QHBoxLayout()self.horizontalLayout_21.setObjectName("horizontalLayout_21")self.label_33 = QtWidgets.QLabel(self.groupBox)self.label_33.setText("")self.label_33.setObjectName("label_33")self.horizontalLayout_21.addWidget(self.label_33)self.bp_Left_7 = QtWidgets.QPushButton(self.groupBox)self.bp_Left_7.setObjectName("bp_Left_7")self.bp_Left_7.setFont(font)self.horizontalLayout_21.addWidget(self.bp_Left_7)self.label_29 = QtWidgets.QLabel(self.groupBox)self.label_29.setText("")self.label_29.setObjectName("label_29")self.horizontalLayout_21.addWidget(self.label_29)self.verticalLayout_5.addLayout(self.horizontalLayout_21)self.horizontalLayout_14 = QtWidgets.QHBoxLayout()self.horizontalLayout_14.setObjectName("horizontalLayout_14")self.label_16 = QtWidgets.QLabel(self.groupBox)self.label_16.setText("")self.label_16.setObjectName("label_16")self.horizontalLayout_14.addWidget(self.label_16)self.bp_Left_8 = QtWidgets.QPushButton(self.groupBox)self.bp_Left_8.setObjectName("bp_Left_8")self.bp_Left_8.setFont(font)self.horizontalLayout_14.addWidget(self.bp_Left_8)self.label_23 = QtWidgets.QLabel(self.groupBox)self.label_23.setText("")self.label_23.setObjectName("label_23")self.horizontalLayout_14.addWidget(self.label_23)self.verticalLayout_5.addLayout(self.horizontalLayout_14)self.gridLayout_3.addLayout(self.verticalLayout_5, 0, 0, 1, 1)self.horizontalLayout_2.addWidget(self.groupBox)self.groupBox_2 = QtWidgets.QGroupBox(self)font = QtGui.QFont()font.setPointSize(12)self.groupBox_2.setFont(font)self.groupBox_2.setAlignment(QtCore.Qt.AlignCenter)self.groupBox_2.setObjectName("groupBox_2")self.gridLayout_4 = QtWidgets.QGridLayout(self.groupBox_2)self.gridLayout_4.setObjectName("gridLayout_4")self.verticalLayout_6 = QtWidgets.QVBoxLayout()self.verticalLayout_6.setObjectName("verticalLayout_6")self.horizontalLayout_15 = QtWidgets.QHBoxLayout()self.horizontalLayout_15.setObjectName("horizontalLayout_15")self.label_24 = QtWidgets.QLabel(self.groupBox_2)self.label_24.setText("")self.label_24.setObjectName("label_24")font = QtGui.QFont()font.setPointSize(18)self.horizontalLayout_15.addWidget(self.label_24)self.bp_Rigth_1 = QtWidgets.QPushButton(self.groupBox_2)self.bp_Rigth_1.setObjectName("bp_Rigth_1")self.bp_Rigth_1.setFont(font)self.horizontalLayout_15.addWidget(self.bp_Rigth_1)self.label_17 = QtWidgets.QLabel(self.groupBox_2)self.label_17.setText("")self.label_17.setObjectName("label_17")self.horizontalLayout_15.addWidget(self.label_17)self.verticalLayout_6.addLayout(self.horizontalLayout_15)self.horizontalLayout_27 = QtWidgets.QHBoxLayout()self.horizontalLayout_27.setObjectName("horizontalLayout_27")self.label_38 = QtWidgets.QLabel(self.groupBox_2)self.label_38.setText("")self.label_38.setObjectName("label_38")self.horizontalLayout_27.addWidget(self.label_38)self.bp_Rigth_2 = QtWidgets.QPushButton(self.groupBox_2)self.bp_Rigth_2.setObjectName("bp_Rigth_2")self.bp_Rigth_2.setFont(font)self.horizontalLayout_27.addWidget(self.bp_Rigth_2)self.label_34 = QtWidgets.QLabel(self.groupBox_2)self.label_34.setText("")self.label_34.setObjectName("label_34")self.horizontalLayout_27.addWidget(self.label_34)self.verticalLayout_6.addLayout(self.horizontalLayout_27)self.horizontalLayout_28 = QtWidgets.QHBoxLayout()self.horizontalLayout_28.setObjectName("horizontalLayout_28")self.label_39 = QtWidgets.QLabel(self.groupBox_2)self.label_39.setText("")self.label_39.setObjectName("label_39")self.horizontalLayout_28.addWidget(self.label_39)self.bp_Rigth_3 = QtWidgets.QPushButton(self.groupBox_2)self.bp_Rigth_3.setObjectName("bp_Rigth_3")self.bp_Rigth_3.setFont(font)self.horizontalLayout_28.addWidget(self.bp_Rigth_3)self.label_35 = QtWidgets.QLabel(self.groupBox_2)self.label_35.setText("")self.label_35.setObjectName("label_35")self.horizontalLayout_28.addWidget(self.label_35)self.verticalLayout_6.addLayout(self.horizontalLayout_28)self.horizontalLayout_26 = QtWidgets.QHBoxLayout()self.horizontalLayout_26.setObjectName("horizontalLayout_26")self.label_40 = QtWidgets.QLabel(self.groupBox_2)self.label_40.setText("")self.label_40.setObjectName("label_40")self.horizontalLayout_26.addWidget(self.label_40)self.bp_Rigth_4 = QtWidgets.QPushButton(self.groupBox_2)self.bp_Rigth_4.setObjectName("bp_Rigth_4")self.bp_Rigth_4.setFont(font)self.horizontalLayout_26.addWidget(self.bp_Rigth_4)self.label_36 = QtWidgets.QLabel(self.groupBox_2)self.label_36.setText("")self.label_36.setObjectName("label_36")self.horizontalLayout_26.addWidget(self.label_36)self.verticalLayout_6.addLayout(self.horizontalLayout_26)self.horizontalLayout_25 = QtWidgets.QHBoxLayout()self.horizontalLayout_25.setObjectName("horizontalLayout_25")self.label_41 = QtWidgets.QLabel(self.groupBox_2)self.label_41.setText("")self.label_41.setObjectName("label_41")self.horizontalLayout_25.addWidget(self.label_41)self.bp_Rigth_5 = QtWidgets.QPushButton(self.groupBox_2)self.bp_Rigth_5.setObjectName("bp_Rigth_5")self.bp_Rigth_5.setFont(font)self.horizontalLayout_25.addWidget(self.bp_Rigth_5)self.label_37 = QtWidgets.QLabel(self.groupBox_2)self.label_37.setText("")self.label_37.setObjectName("label_37")self.horizontalLayout_25.addWidget(self.label_37)self.verticalLayout_6.addLayout(self.horizontalLayout_25)self.horizontalLayout_17 = QtWidgets.QHBoxLayout()self.horizontalLayout_17.setObjectName("horizontalLayout_17")self.label_25 = QtWidgets.QLabel(self.groupBox_2)self.label_25.setText("")self.label_25.setObjectName("label_25")self.horizontalLayout_17.addWidget(self.label_25)self.bp_Rigth_6 = QtWidgets.QPushButton(self.groupBox_2)self.bp_Rigth_6.setObjectName("bp_Rigth_6")self.bp_Rigth_6.setFont(font)self.horizontalLayout_17.addWidget(self.bp_Rigth_6)self.label_18 = QtWidgets.QLabel(self.groupBox_2)self.label_18.setText("")self.label_18.setObjectName("label_18")self.horizontalLayout_17.addWidget(self.label_18)self.verticalLayout_6.addLayout(self.horizontalLayout_17)self.horizontalLayout_18 = QtWidgets.QHBoxLayout()self.horizontalLayout_18.setObjectName("horizontalLayout_18")self.label_26 = QtWidgets.QLabel(self.groupBox_2)self.label_26.setText("")self.label_26.setObjectName("label_26")self.horizontalLayout_18.addWidget(self.label_26)self.bp_Rigth_7 = QtWidgets.QPushButton(self.groupBox_2)self.bp_Rigth_7.setObjectName("bp_Rigth_7")self.bp_Rigth_7.setFont(font)self.horizontalLayout_18.addWidget(self.bp_Rigth_7)self.label_19 = QtWidgets.QLabel(self.groupBox_2)self.label_19.setText("")self.label_19.setObjectName("label_19")self.horizontalLayout_18.addWidget(self.label_19)self.verticalLayout_6.addLayout(self.horizontalLayout_18)self.horizontalLayout_19 = QtWidgets.QHBoxLayout()self.horizontalLayout_19.setObjectName("horizontalLayout_19")self.label_27 = QtWidgets.QLabel(self.groupBox_2)self.label_27.setText("")self.label_27.setObjectName("label_27")self.horizontalLayout_19.addWidget(self.label_27)self.bp_Rigth_8 = QtWidgets.QPushButton(self.groupBox_2)self.bp_Rigth_8.setObjectName("bp_Rigth_8")self.bp_Rigth_8.setFont(font)self.horizontalLayout_19.addWidget(self.bp_Rigth_8)self.label_20 = QtWidgets.QLabel(self.groupBox_2)self.label_20.setText("")self.label_20.setObjectName("label_20")self.horizontalLayout_19.addWidget(self.label_20)self.verticalLayout_6.addLayout(self.horizontalLayout_19)self.gridLayout_4.addLayout(self.verticalLayout_6, 0, 0, 1, 1)self.horizontalLayout_2.addWidget(self.groupBox_2)self.verticalLayout.addLayout(self.horizontalLayout_2,7)self.horizontalLayout_3 = QtWidgets.QHBoxLayout()self.horizontalLayout_3.setObjectName("horizontalLayout_3")self.groupBox_3 = QtWidgets.QGroupBox(self)font = QtGui.QFont()font.setPointSize(12)self.groupBox_3.setFont(font)self.groupBox_3.setObjectName("groupBox_3")self.gridLayout_2 = QtWidgets.QGridLayout(self.groupBox_3)self.gridLayout_2.setObjectName("gridLayout_2")self.label_2 = QtWidgets.QLabel(self.groupBox_3)self.label_2.setStyleSheet("background-color: rgb(0, 0, 0);\n"
"color: rgb(255, 255, 127);")self.label_2.setAlignment(QtCore.Qt.AlignCenter)self.label_2.setObjectName("label_2")self.gridLayout_2.addWidget(self.label_2, 0, 0, 1, 1)self.horizontalLayout_3.addWidget(self.groupBox_3)self.verticalLayout.addLayout(self.horizontalLayout_3,2)self.gridLayout.addLayout(self.verticalLayout, 0, 0, 1, 1)self.retranslateUi()QtCore.QMetaObject.connectSlotsByName(self)self.setWindowFlags(QtCore.Qt.WindowMinimizeButtonHint | QtCore.Qt.WindowCloseButtonHint) # 只显示最小化按钮和关闭按钮# 更新测试参数json,itemName:项目名称,readValue:读取值,testResult:测试结果def UpdateJsonTestArgs(self, itemName, readValue, testResult):try:updateTestArgs = [] # 更新的测试参数self.testArgs = self.ReadJsonInfo('./Conf/TestArgs.json')for js in self.testArgs:if itemName in js['ItemName']:js['Read'] = readValue # 读取的值js['TestResult'] = testResult # 测试结果updateTestArgs.append(js)else:updateTestArgs.append(js)with open("./Conf/TestArgs.json", "w") as write_file:json.dump(updateTestArgs, write_file)return Trueexcept Exception as e:self.ShowLog("Read TestArgs.json ItemName:" + itemName + " Info Err:" + str(e), False)sys.exit(1)# 启动线重脚本def TestThread(self):pass# self.t_autoplay=Thread(target=self.Test)# self.t_autoplay.start()# 读取项目参数信息,itemName:项目名称def ReadJsonTestArgs(self, itemName):try:#print('self.testArgs:', self.testArgs)self.testArgs = self.ReadJsonInfo('./Conf/TestArgs.json')for js in self.testArgs:if itemName in js['ItemName']:self.testStandardArgs = js['Standard']return Trueself.ShowLog('Read TestArgs.json ItemName:' + itemName + ' Info Is Empty!!', False)sys.exit(1)except Exception as e:self.ShowLog("Read TestArgs.json ItemName:" + itemName + " Info Err:" + str(e), False)sys.exit(1)def retranslateUi(self):_translate = QtCore.QCoreApplication.translateself.setWindowTitle(_translate("Form", "Mic-【测试】"))self.label.setText(_translate("Form", "控制过程"))self.lbl_CurrentSwTest.setText(_translate("Form", "Wait..."))self.label_5.setText(_translate("Form", "测试项目"))self.label_4.setText(_translate("Form", "音频声道"))self.label_7.setText(_translate("Form", "左声道"))self.label_6.setText(_translate("Form", "右声道"))self.label_8.setText(_translate("Form", "喇叭测试"))self.lbl_LeftChannel_2.setText(_translate("Form", "N/A"))self.lbl_RightChannel.setText(_translate("Form", "N/A"))self.groupBox.setTitle(_translate("Form", "【左声道】"))self.bp_Left_1.setText(_translate("Form", "1"))self.bp_Left_2.setText(_translate("Form", "2"))self.bp_Left_3.setText(_translate("Form", "3"))self.bp_Left_4.setText(_translate("Form", "4"))self.bp_Left_5.setText(_translate("Form", "5"))self.bp_Left_6.setText(_translate("Form", "6"))self.bp_Left_7.setText(_translate("Form", "7"))self.bp_Left_8.setText(_translate("Form", "8"))self.groupBox_2.setTitle(_translate("Form", "【右声道】"))self.bp_Rigth_1.setText(_translate("Form", "1"))self.bp_Rigth_2.setText(_translate("Form", "2"))self.bp_Rigth_3.setText(_translate("Form", "3"))self.bp_Rigth_4.setText(_translate("Form", "4"))self.bp_Rigth_5.setText(_translate("Form", "5"))self.bp_Rigth_6.setText(_translate("Form", "6"))self.bp_Rigth_7.setText(_translate("Form", "7"))self.bp_Rigth_8.setText(_translate("Form", "8"))self.groupBox_3.setTitle(_translate("Form", "【日志】"))self.label_2.setText(_translate("Form", "待测试.."))# 读取json信息def ReadJsonInfo(self, fileName):try:if os.path.exists(fileName):f = open(fileName, 'r', encoding='utf-8')return json.loads(f.read())except Exception as e:self.ShowLog("Read " + fileName + " Err:" + str(e), False)#sys.exit(1)# 手动关闭窗口def closeEvent(self, event):# 告知线程停止运行sys.exit(1)# 定义一个函数使得函数窗口居中显示def Center(self):# 获取屏幕尺寸screen_geometry = app.desktop().availableGeometry()# 计算窗口居中位置x = (screen_geometry.width() - self.width()) // 2y = (screen_geometry.height() - self.height()) // 2# 设置窗口位置self.move(x, y)# 打印的信息def ShowLog(self, log, isPass):try:if isPass == True:self.label_2.setStyleSheet("background-color: rgb(85, 255, 127);color:green;")self.logger.info(str(log))self.label_2.setText("TEST PASS")else:self.label_2.setStyleSheet("background-color: rgb(85, 255, 127);color: red;")self.logger.error(str(log))self.label_2.setText(log)except Exception as e:print("\033[1;31m" + str(e) + " \033[0m")#sys.exit(1)if __name__=='__main__':app=QApplication(sys.argv)win=Ui_Form()win.Center() # 居中win.show()#win.start_realtime_audio_processing() # 启动实时音频处理sys.exit(app.exec_())
这篇关于python+PyQt5 左右声道测试的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!