python - Take screenshot in QThread loop causes app to quit -
with following code i'm able create splash screen pyqt , spawn thread emits signal update qlabel. qthread has timer-delayed loop emits qstring-containing signal, and, when main app receives it, qlabel on splash screen dynamically updated: i've verified general approach works.
the problem when try take screenshot on every iteration of loop. i'm seeing app quits without error messages or warnings. print
statements indicate screenshot step somehow causing app quit, i.e., removing 1 line makes app function properly.
is there way fix this, or @ least find out may causing problem?
from pyqt4 import qtcore pyqt4.qtcore import (qt, signal, pyqtsignal, qobject) pyqt4.qtgui import (qapplication, qdialog, qlineedit, qtextbrowser, qvboxlayout, qpushbutton, qsplashscreen, qlabel) import random import time import sys import pyscreenshot class worker(qtcore.qthread): def __init__(self, parent=none): qtcore.qthread.__init__(self) print("hello world") def __del__(self): self.wait() print 'del' def run(self): print 'in' while true: time.sleep(1) = range(0, 500) pyscreenshot.grab_to_file('/home/rm/f.png') #this problem self.emit( qtcore.signal('update(qstring)'), "<font color=red size=72><b>{0}</b></font>".format(str(random.choice(a))) ) class form(qdialog): def __init__(self, parent=none): super(form, self).__init__(parent) message = 'hello' self.label = qlabel("<font color=red size=72><b>{0}</b></font>".format(message)) self.setwindowflags(qt.splashscreen) self.setattribute(qt.wa_translucentbackground) self.label.show() self.resize(1250, 50) okbutton = qpushbutton("&ok") layout = qvboxlayout() layout.addwidget(self.label) layout.addwidget(okbutton) self.setlayout(layout) self.connect(okbutton, signal("clicked()"),self.run) def changelab(self, msg): print '************************', msg self.label.settext(msg) def run(self): self.workthread = worker() self.connect( self.workthread, qtcore.signal("update(qstring)"), self.changelab ) self.workthread.start() app = qapplication(sys.argv) form = form() form.show() app.exec_()
i suspect pyqt end of pyscreenshot not thread safe. looks uses qpixmap
s quick google indicates shouldn't used outside of gui thread. i'd suggest forcing different end (if thread safe) or use qtimer
take screenshots main thread.
Comments
Post a Comment