You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 02:44:57