Reputation:
How to show the window + print that text? Where it does not show window anymore if i have my while loop on.
import sys
import datetime
import time
from PyQt4 import QtCore, QtGui
class Main(QtGui.QMainWindow):
def __init__(self, parent=None):
super(Main, self).__init__(parent)
self.b = QtGui.QPushButton("exit", self, clicked=self.close)
self.c = QtGui.QLabel("Test", self)
if __name__ == "__main__":
app=QtGui.QApplication(sys.argv)
myapp=Main()
myapp.show()
while True:
time.sleep(2)
print "Print this + Show the Window???!!!"
sys.exit(app.exec_())
Tried:
import sys
import datetime
import time
from PyQt4 import QtCore, QtGui
class Main(QtGui.QMainWindow):
def __init__(self, parent=None):
super(Main, self).__init__(parent)
self.b = QtGui.QPushButton("exit", self, clicked=self.close)
self.c = QtGui.QLabel("Test", self)
def myRun():
while True:
time.sleep(2)
print "Print this + Show the Window???!!!"
if __name__ == "__main__":
app=QtGui.QApplication(sys.argv)
myapp=Main()
myapp.show()
thread = QtCore.QThread()
thread.run = lambda self: myRun()
thread.start()
sys.exit(app.exec_())
Output:
TypeError: () takes exactly 1 argument (0 given)
Upvotes: 1
Views: 373
Reputation: 4367
Few issues: 1) You're not calling or initializing the thread correctly. 2) You need to tell your main thread to keep processing events while the other thread is running 3) Your label is hovering over the 'exit' button, and therefore you'll not be able to ever click it!
import sys
import datetime
import time
from PyQt4 import QtCore, QtGui
class Main(QtGui.QMainWindow):
def __init__(self, parent=None):
super(Main, self).__init__(parent)
self.b = QtGui.QPushButton("exit", self, clicked=self.close)
def myRun(self):
while True:
time.sleep(2)
print "Print this + Show the Window???!!!"
if __name__ == "__main__":
app=QtGui.QApplication(sys.argv)
myapp=Main()
myapp.show()
thread = QtCore.QThread()
thread.run = lambda myapp=myapp: myapp.myRun()
thread.start()
app.connect(app, QtCore.SIGNAL("lastWindowClosed()"), app, QtCore.SLOT("quit()"))
sys.exit(app.exec_())
while thread.isAlive():
#Make sure the rest of the GUI is responsive
app.processEvents()
Upvotes: 2
Reputation: 328724
lambda self: myRun()
tries to invoke the global function myRun()
. Try
lambda myapp=myapp: myapp.myRun()
instead. The odd assignment will create a default parameter because thread.run()
doesn't get one.
Upvotes: 0