htmlcsspyqtqlabel

How to set qlabel text's opacity in PyQt?


I have a label and I want to set it html\css formatted text from text variable.

label = QtGui.QLabel()   
text = '''<font face="tahoma" color="#45688E">THIS TEXT</font>'''
label.setText(text)

I need the word THIS to be colored with my color(#45688E) and word TEXT to be transparent.

The idea is that I want label to show only one word on screen, meanwhile I could programatically get two words.

How to do it?

Thank you.

Update1: photo Code:

from PyQt4 import QtGui, QtCore
import sys

app = QtGui.QApplication(sys.argv)

window = QtGui.QWidget()
window.resize(300, 400)
window.setWindowTitle('TITLE1')

window_layout = QtGui.QVBoxLayout()
window.setLayout(window_layout)

label = QtGui.QLabel()   
text = '''<font face="tahoma"><span style="color:#45688E">THIS</span><span style="opacity:0"> TEXTANOTHER_WORD</span></font>'''
label.setText(text)

window_layout.addWidget(label)

window.show()
sys.exit(app.exec_())

Solution

  • Wrap your first word in a span tag and style with a color. Add all other words in another span tag and set the opacity of the span tag to 0

    '''<font face="tahoma"><span style="color:#45688E">THIS</span><span style="opacity:0">TEXTANOTHER_WORD</span></font>'''
    

    Browser Snippet below

    <font face="tahoma"><span style="color:#45688E">THIS</span><span style="opacity:0">TEXTANOTHER_WORD</span></font>

    EDIT:

    Solution 2

     '''<font face="tahoma"><span style="color:#45688E">THIS</span><span style="display:none">TEXTANOTHER_WORD</span></font>'''
    

    A somewhat odd solution with PyQT

    Somewhat setting the color to transparent hides that text

    style="color:transparent"
    

    Example

    text = '''<font face="tahoma" color="red" style="color:green;"><span>THIS</span><span  style="color:transparent">TEXT</span></font>'''
    

    Picture below

    see picture here