类型错误:*后面的start()参数必须是可迭代的,而不是整数。

5

我想在线程中运行一些定时器,但是它显示错误TypeError: start() argument after * must be an iterable, not int,我该如何解决?

    while 1:
            try:
                _thread.start_new_thread(self.timer0.start,100)
                _thread.start_new_thread(self.timer1.start,150)
                _thread.start_new_thread(self.timer2.start,200)
                _thread.start_new_thread(self.timer3.start,250)
                _thread.start_new_thread(self.timer4.start,300)
                break
            except:
                print ("Error: unable to start thread")
            break
1个回答

13

查看文档:https://docs.python.org/zh-cn/3/library/_thread.html

_thread.start_new_thread(function, args[, kwargs])

启动一个新线程并返回其标识符。该线程使用参数列表args(必须是元组)执行函数function。

因此,正确的调用应该像这样:

_thread.start_new_thread(self.timer0.start, (100,))

它说 'QObject::startTimer: 定时器不能在另一个线程中启动'。 - kevin880701
2
看起来你对线程非常着迷,想在任何时候都使用它。QTimer是一个QObject对象,不支持多线程,因此你不能从另一个线程修改它。这就是为什么Qt告诉你不要从与创建线程不同的线程修改QTimer的原因。 - eyllanesc

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接