Exploring the PySide Label

vegaseat 5 Tallied Votes 763 Views Share

A small test program exploring the PySide/PyQT Label widget. With HTML code you can do some nice formatting of the label's text.

''' ps_test_QLabel.py
very simple template to test PySide widgets like
QLabel, QVBoxLayout, QGroupBox

QLabel accepts a HTML formatted string
for result see: http://prntscr.com/s1bqj

PySide is the official LGPL-licensed version of PyQT
download from:
http://qt-project.org/wiki/PySide_Binaries_Windows
http://www.lfd.uci.edu/~gohlke/pythonlibs/

for more info see:
http://srinikom.github.com/pyside-docs/PySide/QtGui/QLabel.html

tested with Python27/33 and PySide112  by  vegaseat  08feb2013
'''

from PySide.QtCore import *
from PySide.QtGui import *

# replace [] with sys.argv for commandline
app = QApplication([])

# ----- start your widget test code ----

group_box = QGroupBox("QLabel test")

s = "Hello World!"
label_1 = QLabel()
label_1.setText(s + " (normal)")
# use HTML code to show text in color and larger size
s_html = "<font color=red size=5> {} </font>".format(s)
label_2 = QLabel(s_html)
# use HTML code to bring in a font
s_html2 = '''\
<font face="Times New Roman" color=blue size=7> {} </font>
'''.format(s)
label_3 = QLabel(s_html2)
# more HTML code ...
s4 = """\
<font size=5>
<p><b>This text is bold</b></p>
<p><strong>This text is strong</strong></p>
<p><big>This text is big</big></p>
<p><em>This text is emphasized</em></p>
<p><i>This text is italic</i></p>
<p><small>This text is small</small></p>
<p>This is<sub> subscript</sub> and <sup>superscript</sup></p>
</font>
"""
label_4 = QLabel(s4)

vbox = QVBoxLayout()
vbox.addWidget(label_1)
vbox.addWidget(label_2)
vbox.addWidget(label_3)
vbox.addWidget(label_4)
group_box.setLayout(vbox)

group_box.show()

# ---- end of widget test code -----

app.exec_()

Dani AI

Generated

Nice, compact test from — it shows the QLabel HTML trick clearly. QLabel (and other Qt text widgets) render rich text using Qt’s rich-text engine, which implements a subset of HTML4 and will usually auto-detect whether a string is plain text or rich text. That auto-detection (and the limited-but-useful HTML/CSS support) is why short snippets like yours format nicely without extra calls. (doc.qt.io)

If the label ever shows raw tags instead of formatted text, explicitly set the format or make sure your string is not HTML-escaped. For clickable anchors enable the proper interaction flags and choose whether links should open externally or be handled in-app. For example:

label.setTextInteractionFlags(Qt.TextBrowserInteraction)
label.setOpenExternalLinks(True)

You can also connect to linkActivated to handle clicks in Python. Note that enabling rich text changes input/mouse behavior (Qt may enable mouse tracking), so toggle only when needed. (doc.qt.io)

Qt documents exactly which tags and CSS properties are supported (the engine accepts things like font, img, table, and a useful set of CSS properties such as font-size, color, and margin — but some CSS applies only to block tags). For images, <img src=... width=.. height=..> works, or use setPixmap() for local pixmap handling. For anything larger or interactive, switch to QTextBrowser/QTextEdit (read-only) or a full web view. (doc.qt.io)

If this example is being used today, note that PySide has been folded into the Qt for Python effort (PySide2/PySide6). For modern Python and Qt versions, consult the Qt for Python docs and prefer the current bindings (PySide6) or the appropriate version for your Qt release. For larger HTML needs embed a QWebEngineView (full web engine) instead of QLabel. (doc.qt.io)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.