On Friday 12 April 2002 13:08, Boudewijn Rempt wrote:
> Janez Jere mailed me the other day about using threading in PyQt, and I
> found that I couldn't get a working solution myself.
>
Of course, actually attaching the scripts is an idea...
--
Boudewijn Rempt | http://www.valdyas.org
#
# thread.py - Qt threads
#
import sys
from qt import *
mutex = QMutex()
class GuiThread(QThread):
def __init__(self, label, name, *args):
QThread.__init__(self, *args)
self.label=label
self.name = name
def run(self):
counter = 0
while counter < 200:
#qApp.lock()
#self.label.setText(str(self.label.text()) + "\n" +
# str(self.counter))
print counter
counter += 1
#qApp.unlock()
self.sleep(1)
class MainWindow(QMainWindow):
def __init__(self, *args):
QMainWindow.__init__(self, *args)
self.container = QWidget(self)
self.setCentralWidget(self.container)
self.layout = QHBoxLayout(self.container, 5, 5, "main")
self.label1=QLabel("Thread A", self.container)
self.layout.addWidget(self.label1)
self.label2=QLabel("Thread B", self.container)
self.layout.addWidget(self.label2)
self.thread1=GuiThread(self.label1, "Thread A")
self.thread2=GuiThread(self.label2, "Thread B")
self.thread1.start()
self.thread2.start()
def main(args):
app=QApplication(args)
win=MainWindow()
win.show()
app.connect(app, SIGNAL("lastWindowClosed()")
, app
, SLOT("quit()")
)
app.exec_loop()
if __name__=="__main__":
main(sys.argv)
#
# thread.py - Qt threads
#
import sys
from qt import *
mutex = QMutex()
class GuiThread(QThread):
def __init__(self, label, name, *args):
QThread.__init__(self, *args)
self.label=label
self.name = name
def run(self):
counter = 0
while counter < 200:
qApp.lock()
self.label.setText(str(self.label.text()) + "\n" +
str(self.counter))
counter += 1
qApp.unlock()
self.sleep(1)
class MainWindow(QMainWindow):
def __init__(self, *args):
QMainWindow.__init__(self, *args)
self.container = QWidget(self)
self.setCentralWidget(self.container)
self.layout = QHBoxLayout(self.container, 5, 5, "main")
self.label1=QLabel("Thread A", self.container)
self.layout.addWidget(self.label1)
self.label2=QLabel("Thread B", self.container)
self.layout.addWidget(self.label2)
self.thread1=GuiThread(self.label1, "Thread A")
self.thread2=GuiThread(self.label2, "Thread B")
self.thread1.start()
self.thread2.start()
def main(args):
app=QApplication(args)
win=MainWindow()
win.show()
app.connect(app, SIGNAL("lastWindowClosed()")
, app
, SLOT("quit()")
)
app.exec_loop()
if __name__=="__main__":
main(sys.argv)