如何使用python启动两个线程?
我有3个python文件:如何使用python启动两个线程?,python,multithreading,python-2.7,3d,leap-motion,Python,Multithreading,Python 2.7,3d,Leap Motion,我有3个python文件: 该文件包含许多类,包括使用pyQt初始化框架和其他GUI的任何方法 包含从leap Motion读取数据的leap Motion侦听器类的文件 用于启动其他类的主文件 现在我想一起启动GUI框架和Leap Motion类。我试着在主课上开始两个线程,但是有很多问题 此代码仅对运行pyQt帧有效: import sys from PyQt4 import QtCore, QtGui from Painter import GUI_animate class Start
import sys
from PyQt4 import QtCore, QtGui
from Painter import GUI_animate
class StartQT4(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = GUI_animate()
self.ui.setupUi(self)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = StartQT4()
myapp.show()
sys.exit(app.exec_())
这就是我试图运行pyQt frame and Leap Motion类所做的:
import sys
from PyQt4 import QtCore, QtGui
from Painter import GUI_animate
import LeapMotion
from threading import Thread
class StartQT4(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
t1 = Thread(target=show_frame())
t2 = Thread(target=start_LeapMotion())
t1.start()
t2.start()
self.ui.setupUi(self)
def start_LeapMotion():
LeapMotion.main()
def show_frame():
StartQT4.ui = GUI_animate()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = StartQT4()
myapp.show()
sys.exit(app.exec_())
但只有Leap Motion类运行,并在阅读完Leap Motion后,框架显示
我怎样才能把它们放在一起 当您将成对括号指定为线程的
目标时,不要将它们放在show_frame
和start_LeapMotion
之后。Python将functionName
解释为引用
的变量,而functionName()
是对该函数的调用。当您指定线程的目标时,您不希望向该函数传递调用;您希望传递函数本身。如中所述,t1.start()
调用Thread
对象的run()
方法,除非您重写它,“调用作为目标参数传递给对象构造函数的可调用对象”——请注意,target
参数应该接收可调用对象(即函数本身,因此没有括号),而不是调用(这是您当前正在传递的)。谢谢:)当我删除括号时,它工作正常^_^