Search code examples
pythonpyqt4

Formatting text inside PYQT4?


userPassword = user_input
password = QtGui.QLabel('{}', self).format(userPassword)

I want the label to have the text inside which has been entered by the user? ERROR BELOW

AttributeError: 'QLabel' object has no attribute 'format'

Solution

  • The syntax for the format function is str.format()

    (Python Docs for str.format())

    So your code should be this since you want to format the String(i.e. the '{}') and not the QLabel Object:

    userPassword = user_input
    password = QtGui.QLabel('{}'.format(userPassword), self)