setStyleSheet()导致崩溃

时间:2019-11-28 13:32:12

标签: python pyqt

频繁调用“ setStyleSheet”会导致Qt5崩溃。我找不到原因。请帮我弄清楚。

这是错误代码:

from Ui_untitled import Ui_MainWindow 

class MyMainWindow(QMainWindow,Ui_MainWindow):
    def __init__(self,parent = None):
        super(MyMainWindow, self).__init__(parent)
        self._translate = QtCore.QCoreApplication.translate
        self.setupUi(self)
        self.preColor = [[1,1],[1,1],[1,1],[1,1],[1,1]]
        self.label = [[self.label11,self.label12], [self.label21, self.label22], [self.label31, self.label32],[self.label41, self.label42], [self.label51, self.label52]]
    def changeUI(self, color, data):
        for i in range(5):
            for j in range(2):
                if(color[i][j]):
                    if(self.preColor[i][j]):
                        pass
                    else:
                        self.preColor[i][j] = 1
                        self.label[i][j].setStyleSheet("background-color: rgb(240, 240, 240);\n""font: 14pt \"楷体\";\n""")
                        time.sleep(0.2)
                else:
                    if(self.preColor[i][j]):
                        self.label[i][j].setStyleSheet("background-color: rgb(252, 229, 223);\n""font: 14pt \"楷体\";\n""")
                        time.sleep(0.2)
                        self.preColor[i][j] = 0

                self.label[i][j].setText(self._translate("MainWindow", str(data[i][j])))

此代码有误:>> self.label[i][j].setStyleSheet("background-color: rgb(240, 240, 240);\n""font: 14pt \"楷体\";\n""")

其他代码1:

import myui
def data_to_lalala(data):
    data_set=np.array(data).reshape(5,2)
    data_set[0,0]=float(data_set[0,0].strip('B'))
    data_set[4,1]=float(data_set[4,1].strip('E'))
    return data_set

def isright(t):
    if(t[0] < 1):
        a = [True]
    else:
        a = [False]
    if(t[1] < 1):
        a.append(True)
    else:
        a.append(False)
    return a

Sign = 1
def serialRun(mw):
    ser = serial.Serial('COM10',115200, timeout=0.5)
    while Sign:
        data_serial = ser.readline()
        #data_serial = "B2.200:0.300:0.000:0.000:0.000:1.200:0.300:0.000:0.000:2.009E"
        data_write = str(data_serial)[2:-5]
        if(data_write[0] is not 'B' or data_write[-1] is not 'E'):
            continue
        data_write = data_write[1:-1]
        data_split= data_write.split(':')
        data_draw=data_to_lalala(data_split)
        data_draw=data_draw.astype(float).tolist()
        color = list(map(isright, data_draw))
        mw.changeUI(color, data_draw)

该功能是从串行获取数据并根据数据更新样式表。

0 个答案:

没有答案