Exploring the PySide Label

vegaseat 5 Tallied Votes 744 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_()