Skip to main content
 首页 » 编程设计

python之TypeError: start() 恰好接受 1 个参数(给定 0)

2025年05月04日12wuhuacong

我有这样的东西:

class thread1(threading.Thread): 
    def __init__(self): 
        file = open("/home/antoni4040/file.txt", "r") 
        data = file.read() 
        num = 1 
        while True: 
           if str(num) in data: 
               clas = ExportToGIMP 
               clas.update() 
           num += 1      
thread = thread1 
        thread.start() 
        thread.join() 

我得到这个错误:

TypeError: start() takes exactly 1 argument (0 given) 

为什么?

请您参考如下方法:

thread = thread1 需要是 thread = thread1()。否则,您将尝试调用类的方法,而不是类的实际实例。


此外,不要覆盖 Thread 对象上的 __init__ 来完成您的工作 - 覆盖 run .

(虽然您可以覆盖 __init__ 来进行设置,但这实际上并不是在线程中运行,并且还需要调用 super()。)


您的代码应如下所示:

class thread1(threading.Thread): 
    def run(self): 
        file = open("/home/antoni4040/file.txt", "r") 
        data = file.read() 
        num = 1 
        while True: 
           if str(num) in data: 
               clas = ExportToGIMP 
               clas.update() 
           num += 1      
 
thread = thread1() 
        thread.start() 
        thread.join()