Sunday, December 7, 2014

PyQt (3): QLabel

QLabel is a widget using to display text or image.

QtCore.Qt.AlignmentFlag provides flags to fine-tune the apperance of aligned text:

QtCore.Qt.QtAlignTop
QtCore.Qt.AlignBottom
QtCore.Qt.AlignRight
QtCore.Qt.AlignLeft
QtCore.Qt.AlignVCenter
QtCore.Qt.AlignHCenter
QtCore.Qt.AlignCenter

Example:
from PyQt4 import QtGui, QtCore

# create main widget and its layout
mainWidget = QtGui.QWidget()
mainLayout = QtGui.QHBoxLayout()
mainWidget.setLayout(mainLayout)

# create a label
label = QtGui.QLabel('PrintOut Button: ')
# fine-tune the appearance of aligned text
label.setAlignment(QtCore.Qt.AlignCenter)

# create a button
btn = QtGui.QPushButton('Button')

# create button click function
def clickFunc():
    print 'This is a button!'

# create signal for the button
# when click the button, excute the click function
btn.clicked.connect(clickFunc)

# add label and button to the main layout
mainLayout.addWidget(label)
mainLayout.addWidget(btn)

# show the main widget
mainWidget.show()

No comments:

Post a Comment