使用按钮的 PyQt5 Qlabel QPixmap 旋转
PyQt5 Qlabel QPixmap Rotation using pushButton
所以我试图用按钮旋转 QLabel,但我收到错误并且不确定发生了什么:
Process finished with exit code -1073740791 (0xC0000409)
我使用这个 post 作为参考 Rotating a pixmap in pyqt4 gives undesired translation 但是因为这是针对 PyQt4 我需要做一些改变。
谁能解释一下我做错了什么?
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_())
示例视图
我的下一步是根据 QCombobox 和用户输入实现旋转,但那是以后的事。
建议您从控制台 运行 您的代码以获得更具描述性的错误消息,如果您这样做,您将得到:
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)
原因很明显,您正在累积一个以前从未分配过的变量,这种情况下的解决方案是创建一个只初始化一次的属性。
即使更正该错误,也会出现另一个错误:
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)
而且你也可以知道错误的原因,setPixmap方法并没有return任何东西。解决办法就是不用setPixmap,复制QPixmap。
最后,建议不要修改Qt Designer生成的代码,必须通过执行pyuic5 your_file.ui -o gui.py -x
恢复.py
然后使用下面的.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_())
所以我试图用按钮旋转 QLabel,但我收到错误并且不确定发生了什么:
Process finished with exit code -1073740791 (0xC0000409)
我使用这个 post 作为参考 Rotating a pixmap in pyqt4 gives undesired translation 但是因为这是针对 PyQt4 我需要做一些改变。
谁能解释一下我做错了什么?
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_())
示例视图
我的下一步是根据 QCombobox 和用户输入实现旋转,但那是以后的事。
建议您从控制台 运行 您的代码以获得更具描述性的错误消息,如果您这样做,您将得到:
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)
原因很明显,您正在累积一个以前从未分配过的变量,这种情况下的解决方案是创建一个只初始化一次的属性。
即使更正该错误,也会出现另一个错误:
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)
而且你也可以知道错误的原因,setPixmap方法并没有return任何东西。解决办法就是不用setPixmap,复制QPixmap。
最后,建议不要修改Qt Designer生成的代码,必须通过执行pyuic5 your_file.ui -o gui.py -x
然后使用下面的.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_())