1

Can't figure out what's wrong. Just need to change label text from 'Default label' to 'New label 01'. from PySide.QtGui import *

class myWidget(QWidget):
    def __init__(self):
        super(myWidget, self).__init__()
        layout = QVBoxLayout(self)

        label1 = QLabel('Default label')
        layout.addWidget(label1)

        button = QPushButton('Change')
        layout.addWidget(button)
        button.clicked.connect(self.newlabel)


    def newlabel(self):
        print 'ACTION1'
        self.label1.setText('New label 01')
        print 'ACTION2'

app = QApplication([])
window = myWidget()
window.show()
app.exec_()

This is what I got after running in pycharm

C:\Python27\python.exe D:/OneDrive/Projects/Personal/Tutorials/Python/CGScripting/PySide/simpleWidget.py
ACTION1
Traceback (most recent call last):
  File "D:/OneDrive/Projects/Personal/Tutorials/Python/CGScripting/PySide/simpleWidget.py", line 32, in newlabel
    self.label1.setText('New label 01')
AttributeError: 'myWidget' object has no attribute 'label1'

Process finished with exit code 0

1 Answers1

1

You have to make label1 an attribute of your myWidget instance by prepending self in the __init__ method:

    self.label1 = QLabel('Default label')
    layout.addWidget(self.label1)
Schmuddi
  • 1,995
  • 21
  • 35