0

I've been trying to scale a Pixmap image correctly, however when calling the function I get a TypeError - am I doing something wrong?

It's stating that "size" isn't a valid argument, but also stating it's an argument within the same error call.

Code:

label = QtWidgets.QLabel(self.main_window)
image = QtGui.QPixmap(assets.images.icon)

image = image.scaled(
     size = QtCore.QSize(64, 64),
     aspectRatioMode = QtCore.Qt.AspectRatioMode.KeepAspectRatio,
     TransformationMode = QtCore.Qt.TransformationMode.SmoothTransformation
)

label.resize(QtCore.QSize(64, 64))
label.setPixmap(image)

Error:

arguments did not match any overloaded call:
scaled(self, size: QSize, aspectRatioMode: Qt.AspectRatioMode = Qt.IgnoreAspectRatio, transformMode: Qt.TransformationMode = Qt.FastTransformation): 'size' is not a valid keyword argument
4
  • Weird error, but maybe use transformMode instead of TransformationMode, as given in the error message.
    – oskar
    Commented Aug 2 at 12:17
  • @oskar same issue unfortunately; I'll just have to run the code without stating the arguments.
    – N4GR
    Commented Aug 2 at 12:22
  • or don't write size= and just leave QtCore.QSize(64, 64), as a non-keyword argument; edit: ok, you found the solution👍
    – oskar
    Commented Aug 2 at 12:24
  • In PyQt only optional arguments are accepted in keyword form (key=value), not positional ones. This applies to all native Qt class constructors and functions, but not for overrides of subclasses, as they're native Python functions and work as expected. Always check the documentation for argument signature, if arguments are positional, don't use them as named arguments. When in doubt, use help(<class or function>) in the interactive python shell. Commented Aug 2 at 12:33

1 Answer 1

0

Strangely enough, it decided to work once I took away the argument names.

label = QtWidgets.QLabel(self.main_window)
image = QtGui.QPixmap(assets.images.icon)

image = image.scaled(
    QtCore.QSize(64, 64),
    QtCore.Qt.AspectRatioMode.KeepAspectRatio,
    QtCore.Qt.TransformationMode.SmoothTransformation
)

label.resize(QtCore.QSize(64, 64))
label.setPixmap(image)

Not the answer you're looking for? Browse other questions tagged or ask your own question.