PyQt6 Pixmap缩放报错:参数不匹配,关键字参数size无效
解决QPixmap.scaled()调用的TypeError问题
你的问题出在两个地方:
- 第一个参数不能用
size=关键字传递:PyQt的scaled()方法在接收QSize对象时,该参数为位置参数,不支持用size=作为关键字传递,直接传入QSize对象即可。 - 第三个参数名称大小写错误:你写的
TransformationMode需改为transformMode(首字母小写),和方法定义中的参数名保持一致。
修正后的代码:
label = QtWidgets.QLabel(self.main_window) image = QtGui.QPixmap(assets.images.icon) image = image.scaled( QtCore.QSize(64, 64), aspectRatioMode = QtCore.Qt.AspectRatioMode.KeepAspectRatio, transformMode = QtCore.Qt.TransformationMode.SmoothTransformation ) label.resize(QtCore.QSize(64, 64)) label.setPixmap(image)
另外,如果你想用关键字参数指定尺寸,也可以调用接收宽高的重载版本:
image = image.scaled( width=64, height=64, aspectRatioMode = QtCore.Qt.AspectRatioMode.KeepAspectRatio, transformMode = QtCore.Qt.TransformationMode.SmoothTransformation )
内容的提问来源于stack exchange,提问作者N4GR
相关产品推荐
相关产品推荐

