简体   繁体   中英

Change the color result of text after being compared in PyQt4

I have a pretty easy question that i'm trying to wrap my head around why it's not working.

Basically I am trying to print out the result of my compare function and the results that DO NOT match in a different color.\\

heres a sample of my code:

with open(compareResults, 'wb') as fdout:
    for index, tabName in enumerate(setNames):
        tabWidget = QtGui.QWidget()
        tabLabel = QtGui.QTextEdit()
        print "Tab Name is :{}".format(tabName)
        fdout.write('{}'.format(tabName) + '\r\n')
        nameData = lst[index]
        print 'name data = {}'.format(nameData)
        for k in nameData:

            # if nameData[k] == 'ESS':
            #     print 'ESS found'
            # elif nameData[k] != 'ESS':
            if nameData[k] != correct_parameters[k]:
                tabLabel.append('{} = {}'.format(k, nameData[k]))
                tabLabel.setStyleSheet('color: black')
                fdout.write('\t' + '|' + '{} = {}'.format(k, nameData[k]) + '\t' + '|' + '\r\n')
                print ('{} = {}'.format(k, nameData[k]))
            elif nameData[k] == correct_parameters[k]:
                tabLabel.append('{} = {}'.format(k, nameData[k]))
                tabLabel.setStyleSheet('color: red')
                fdout.write('\t' + '|' + '{} = {}'.format(k, nameData[k]) + '\t' + '|' + '\r\n')
                print ('{} = {}'.format(k, nameData[k]))
        tabLayout = QtGui.QVBoxLayout()
        tabLayout.addWidget(tabLabel)
        tabWidget.setLayout(tabLayout)
        self.tabWidget.addTab(tabWidget, tabName)

I want the text result that does match == to be in red and the result that doesn't match in black != however i believe the loop is over writing the results with the second elif so my result only appears in red and doesn't show the != result.

My what code does is compare two text documents that have a series of data and parameters in side of it. Once the program has compared both files it will output any result that are Different so for example if in main file (correct file) dog = 6 and in the other file (incorrect file) dog = 9 it will print out dog = 9 as in "Hey look dog in your other file is incorrect please fix it"

Currently my code does what it's suppose to and print out the incorrect values to my result however I just want to format it to have everything the incorrect values and the correct values. How can i color coat my results correctly?

Heres a visual representation of what am trying to achieve: As you can see Mouse is in black since it's incorrect and the result is in red since it's correct. 输出1

With tabLabel.setStyleSheet('color: red') you're changing the css of the whole QTextEdit : you define that all text, even previously written, will be red.

To change the color of the text, you can simply call setTextColor(QtGui.QColor) before writing.

For example:

self.text=QtGui.QTextEdit()
self.text.setTextColor(QtGui.QColor("blue"))
self.text.append("I'm blue !")
self.text.setTextColor(QtGui.QColor("red"))
self.text.append("I'm red !")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM