Error lambda missing 1 required positional argument when using with QPushButton

Claire Duong

This is entire my code:

import sys

from PySide2.QtCore import Qt
from PySide2.QtWidgets import (
    QApplication,
    QHBoxLayout,
    QLabel,
    QMainWindow,
    QPushButton,
    QVBoxLayout,
    QWidget,
)

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        v = QVBoxLayout()
        h = QHBoxLayout()

        for a in range(10):
            button = QPushButton(str(a))
            button.clicked.connect(lambda checked, a=a: self.button_clicked(a)) # error here

            h.addWidget(button)

        v.addLayout(h)

        self.label = QLabel("")
        v.addWidget(self.label)
        
        w = QWidget()
        w.setLayout(v)

        self.setCentralWidget(w)

    def button_clicked(self, n):
        self.label.setText(str(n))

app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_()

When I run this code, I get a window like this:image here


Below the buttons, there is a QLabel, and I want when I click on any button, the button's label will refer to this QLabel, but I get a bunch of confusing errors in the terminal. What's wrong with my code, help me, thanks.

eyllanesc

The clicked signal is overload so it accepts 2 signatures where it can send a bool or not. The default signature depends on the library, in this case it seems that PySide2 by default does not send the "checked" parameter, unlike PyQt5 that does.

The solution is to indicate the signature:

button.clicked[bool].connect(lambda checked, a=a: self.button_clicked(a))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

TypeError: <lambda>() missing 1 required positional argument: 'count' when using lambda to sort a python 3 dictionary

TypeError: <lambda>() missing 1 required positional argument: 'df' when using data.head() for a dask dataframe

TypeError: <lambda>() missing 1 required positional argument:

TypeError: <lambda>() missing 1 required positional argument

<lambda>() missing 1 required positional argument

ERROR: typeerror when() missing 1 required positional argument 'value' in PySpark

missing 1 required positional argument when using self

"TYPE ERROR MISSING 1 REQUIRED POSITIONAL ARGUMENT"

<lambda>() missing 1 required positional argument: 'z' with lambda and reduce

missing 1 required positional argument

TypeError: <lambda>() missing 1 required positional argument: 'w'

I have this error delete() missing 1 required positional argument: 'id' when I click on the delete button

Why am I getting a 'missing 1 required positional argument:' error?

Getting this error in django "login() missing 1 required positional argument: 'user' "

Python 3.6 TYPE ERROR: missing 1 required positional argument

Python error: missing 1 required positional argument: 'self'

pyspark dataframe error: _() missing 1 required positional argument: 'col'

Selenium Error: Missing 1 required positional argument: 'url'

How do fix the "missing 1 required positional argument: 'self'" error?

Python Error " <method> missing 1 required positional argument: 'self' "

Error "TypeError: FirstForm() missing 1 required positional argument: 'request'"

Function in Class error: TypeError: function() missing 1 required positional argument:

snowflake with snowpark error: sql() missing 1 required positional argument: 'query'

How to remove this error?fit() missing 1 required positional argument: 'y'

Error:__init__() missing 1 required positional argument: 'rec'

Code gives missing 1 required positional argument error

TypeError: mute_error() missing 1 required positional argument: 'ctx'

Why do I get error "missing 1 required positional argument"?

missing 1 required positional argument:'self' when assigning a method to a variable