PyQt5 Qlabel QPixmap Rotation using pushButton

1

So i am trying to rotate QLabel with a button but i am getting error and not sure what is going on:

Process finished with exit code -1073740791 (0xC0000409)

I was using this post as a reference Rotating a pixmap in pyqt4 gives undesired translation but since this was for PyQt4 i needed to make several changes.

Can someone lease explain what am I doing wrong?

from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(355, 153)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(20, 20, 75, 23))
        self.pushButton.setObjectName("pushButton")
        pixmap = self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(120, 20, 81, 81))
        self.label.setText("")
        self.label.setPixmap(QtGui.QPixmap("../QComboBox/img/Arrows Green.png"))
        self.label.setScaledContents(True)
        self.label.setObjectName("label")
        self.label.setAlignment(QtCore.Qt.AlignCenter)
        self.comboBox = QtWidgets.QComboBox(self.centralwidget)
        self.comboBox.setGeometry(QtCore.QRect(240, 50, 69, 22))
        self.comboBox.setObjectName("comboBox")
        self.comboBox.addItem("")
        self.comboBox.addItem("")
        self.comboBox.addItem("")
        self.comboBox.addItem("")
        self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit.setGeometry(QtCore.QRect(240, 80, 81, 20))
        self.lineEdit.setObjectName("lineEdit")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 355, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "Rotate 5deg"))
        self.comboBox.setItemText(0, _translate("MainWindow", "Rotate 5deg"))
        self.comboBox.setItemText(1, _translate("MainWindow", "Rotate 10deg"))
        self.comboBox.setItemText(2, _translate("MainWindow", "Rotate 15deg"))
        self.comboBox.setItemText(3, _translate("MainWindow", "Rotate 20deg"))

        self.pushButton.clicked.connect(self.rotate_pixmap)

    def rotate_pixmap(self):
        pixmap = self.label.setPixmap(QtGui.QPixmap("../QComboBox/img/Arrows Green.png"))
        rotation += 5
        transform = QtGui.QTransform().rotate(rotation)
        pixmap = pixmap.transformed(transform, QtCore.Qt.SmoothTransformation)
        self.label.setPixmap(pixmap)


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

Sample View

My next step was to implement rotation based on QCombobox and User input but that is for later.

python
pyqt5
qlabel
asked on Stack Overflow Jun 27, 2020 by Ainadiel • edited Jun 27, 2020 by eyllanesc

1 Answer

1

It is recommended that you run your code from the console to get a more descriptive error message, if you do this you get:

Traceback (most recent call last):
  File "main.py", line 55, in rotate_pixmap
    rotation += 5
UnboundLocalError: local variable 'rotation' referenced before assignment
Aborted (core dumped)

And the cause is obvious, you are accumulating in a variable that has never been assigned before, the solution in that case is to create an attribute that is initialized only once.

Even correcting that error another error becomes visible:

Traceback (most recent call last):
  File "main.py", line 59, in rotate_pixmap
    pixmap = pixmap.transformed(transform, QtCore.Qt.SmoothTransformation)
AttributeError: 'NoneType' object has no attribute 'transformed'
Aborted (core dumped)

And you can also know the cause of the error, the setPixmap method does not return anything. The solution is not to use setPixmap unnecessarily and copy the QPixmap.

Finally, it is recommended not to modify the code generated by Qt Designer, so you must restore the .py by executing: pyuic5 your_file.ui -o gui.py -x

and then use the following .py:

main.py

from PyQt5 import QtCore, QtGui, QtWidgets


from gui import Ui_MainWindow


class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setupUi(self)
        self.rotation = 0
        self.pushButton.clicked.connect(self.rotate_pixmap)

    def rotate_pixmap(self):
        pixmap = QtGui.QPixmap("../QComboBox/img/Arrows Green.png")
        self.rotation += 5
        transform = QtGui.QTransform().rotate(self.rotation)
        pixmap = pixmap.transformed(transform, QtCore.Qt.SmoothTransformation)
        self.label.setPixmap(pixmap)


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())
answered on Stack Overflow Jun 27, 2020 by eyllanesc

User contributions licensed under CC BY-SA 3.0