Using Python 2.5 and PyQt 4.4.3, I could not find any questions like this in Python, so sorry if I repeat the other questions mentioned below, but I could not easily understand this C code.
I have two classes, a graphical interface and a stream , and I'm trying to get return values from a stream. I used the link here as a base for writing my code , which works just fine. To summarize and illustrate the question in the code here (I don't think this code will work on its own):
from PyQt4 import QtCore, QtGui
import sys, time
class MainWindow (QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.buttonDaemon = QtGui.QPushButton(self)
self.layout = QtGui.QVBoxLayout(self)
self.layout.addWidget(self.buttonDaemon)
self.setLayout(self.layout)
self.thread = Worker()
self.connect(self.thread, QtCore.SIGNAL('finished()'), self.unfreezeUi)
self.connect(self.thread, QtCore.SIGNAL('terminated()'), self.unfreezeUi)
self.connect(self.thread, QtCore.SIGNAL('stopped(int)'), self.stopped)
self.connect(self.buttonDaemon, QtCore.SIGNAL('clicked()'), self.pressDaemon)
def unfreezeUi (self):
self.buttonDaemon.setEnabled(True)
def pressDaemon (self):
self.buttonDaemon.setEnabled(False)
if self.thread.isDaemonRunning():
self.thread.setDaemonStopSignal(True)
self.buttonDaemon.setText('Daemon - run code every %s sec'% 1)
else:
self.thread.startDaemon()
self.buttonDaemon.setText('Stop Daemon')
self.buttonDaemon.setEnabled(True)
def stopped (self, val):
print 'stopped ' + str(val)
class Worker (QtCore.QThread):
daemonIsRunning = False
daemonStopSignal = False
daemonCurrentDelay = 0
def isDaemonRunning (self): return self.daemonIsRunning
def setDaemonStopSignal (self, bool): self.daemonStopSignal = bool
def __init__ (self, parent = None):
QtCore.QThread.__init__(self, parent)
self.exiting = False
self.thread_to_run = None
def __del__ (self):
self.exiting = True
self.thread_to_run = None
self.wait()
def run (self):
if self.thread_to_run != None:
self.thread_to_run(mode='continue')
def startDaemon (self, mode = 'run'):
if mode == 'run':
self.thread_to_run = self.startDaemon
return self.start()
self.daemonIsRunning = True
print 'Daemon started'
self.daemonStopSignal = False
sleepStep = 0.1
while self.daemonStopSignal == False and not self.exiting:
print 'Daemon running'
delay = 0
while self.daemonStopSignal == False and not self.exiting and delay < 1:
print 'Daemon sleeping'
time.sleep(sleepStep)
delay += sleepStep
self.daemonIsRunning = False
print 'Daemon stopped'
self.emit(QtCore.SIGNAL('stopped(int)'), self.daemonIsRunning)
self.emit(QtCore.SIGNAL('terminated'))
def main (args):
app = QtGui.QApplication(args)
win = MainWindow()
win.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main(sys.argv)
, , . - def pressDaemon. , 3 self.thread. , self.thread.startDaemon() , , . , .
, , . , QtCore.QtThread, , . , return emit.
, . , , , , . , , , Qt, Python. , , , .
edit. . , ! , , , .