Issue
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
Solution
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)
Answered By - Schmuddi
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.