Reputation: 145
i have 3 python Files:
file including Many class any methods for initializing Frame and other GUI using pyQt.
file including leap Motion Listener class that read data from leap motion.
Main file that used to start other classes.
now i want to start the GUI frame and the Leap Motion class together. i tried to start two thread in the main class but there is many problems.
this code is valid to run only the pyQt frame :
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_())
and this is what i tried to do to run the pyQt frame and Leap Motion class :
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_())
but only Leap Motion class run, and after finish reading from leap motion, the frame show !
how can i run them together?
Upvotes: 2
Views: 322
Reputation: 300
Don't put the paired parentheses after show_frame
and start_LeapMotion
when you specify them as target
of the thread. Python interprets functionName
as a variable referring to <function functionName at (memory location)>
, whereas functionName()
is a call to that function. When you specify the thread's target
, you do not want to pass a call to that function; you want to pass the function itself. As explained in the API for the threading module, t1.start()
invokes the Thread
object's run()
method, which, unless you've overridden it, "invokes the callable object passed to the object's constructor as the target argument"--note that the target
argument should receive a callable object (i.e. the function itself, so no parentheses), rather than a call (which is what you are currently passing).
Upvotes: 4