Deezwend
Deezwend

Reputation: 59

TypeError in Threading, function takes 1 argument but 2 were given, in Python

I am coding a program that searches specified image and if it found that image, then it does what the user said to the program to do. I am having problem with threading. I want to use multi-threading more than once so I code a threading decorator. I am using it on two different functions. There is not any problem for the 1st function. But I am getting TypeError for the 2nd function. Here's my code:


from PyQt5 import QtWidgets
from PyQt5 import QtCore
from PyQt5 import QtGui
from threading import Thread
from pynput.keyboard import Listener
from keyboard import is_pressed as pressed
from keyboard import press
from tkinter.messagebox import showinfo
from tkinter.messagebox import showerror
from tkinter.messagebox import showwarning
from tkinter.messagebox import askyesno
from python_imagesearch.imagesearch import imagesearch as search
from functools import cache
from GUI import *
import time
import os
import sys
import pyautogui as pyg
import playsound

class ImageSearch(QtWidgets.QWidget):
    def thread(function):
       def wrapper(*values, **kwvalues):
           t1 = Thread(target=function, args=values, kwargs=kwvalues)
           t1.start()
       return wrapper

    @thread
    def Listen(self):
        with Listener(on_press=self.onpress) as listener:
            press(chr(92))
            listener.join()


    # It is the function that I am getting the error
    @thread
    def Start(self):
        if self.ui.btnStartSearching.text() == "Search":
            self.ui.btnStartSearching.setText("Stop")
            self.ui.groupBox.setEnabled(False)

And the error I am getting:

    self._target(*self._args, **self._kwargs)
TypeError: ImageSearch.Start() takes 1 positional argument but 2 were given

What am I doing wrong?

Upvotes: 0

Views: 415

Answers (1)

Deezwend
Deezwend

Reputation: 59

I just found the solution. I was calling it like this:

self.ui.btnStartSearching.clicked.connect(self.Start)

I tried to use the 'lambda' method and it has been solved. I changed the code above with that:

self.ui.btnStartSearching.clicked.connect(lambda: self.Start())

That's how I solved the problem.

@Matiiss found one more solution. We need to add *args to the function that we want to call.

Like this:

self.ui.btnStartSearching.clicked.connect(self.Start)
def Start(self, *args):
   #code

when *args is added to the function. It is solved.

Upvotes: 1

Related Questions