A
A
ArkHiMed-tech2021-11-07 13:28:49
Python
ArkHiMed-tech, 2021-11-07 13:28:49

Python gives 'missing 1 required positional argument: 'self'', what should I do?

I know that there are a lot of such questions, but no matter how much I searched, I can not find the answer.

I am trying to make a dynamic countdown timer while executing the code along the way

I have the code:

class Class(object):
    <задание параметров окна и вызов функции startCard через self.button.clicked.connect()>
    def beginCard(self):
            <выполняемый код>
            self.threadBeginCard.join() 
    
    def startCard(self):
        <выполняемый код>
        self.threadBeginCard.start() 
          

    threadBeginCard = Timer(0.1, beginCard, args=None, kwargs=None)

class InputFrame(QtWidgets.QMainWindow, Class):
    def __init__(self):
        super().__init__()
        self.setupUi(self)

def main():
    app = QtWidgets.QApplication(sys.argv)
    window = InputFrame()
    window.show() 
    app.exec() 

if __name__ == '__main__':
    main()


And I end up getting a missing 1 required positional argument: 'self' error when I try to call self.threadBeginCard.start()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2021-11-07
@ArkHiMed-tech

threadBeginCard = Timer(0.1, beginCard, args=None, kwargs=None)
I have no idea what Timer() is, but I can guess what it does.
It calls beginCard() with no positional (args) or named (kwargs) parameters when some event occurs.
The problem is that you are calling a class method, i.e. actually Class.beginCard(). He needs to pass an instance of the class as the first parameter, i.e. self.
If you were to call an instance method, i.e.
c = Class()
c.beginCard()
Then self would be substituted automatically.
Conclusion: create a Timer() inside an instance method, and pass self.beginClass to it as a function. Note the absence of brackets.
True, I don't know what happens next - I don't really like the join() call, as it will block the UI thread until the worker thread finishes.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question