[PyQt] thread in pyqt ... simple example needed

massimo di stefano massimodisasha at yahoo.it
Mon Apr 6 02:43:12 BST 2009



Hazen, All,

reaalythanks for your hel, it works for me !
thanks to these example i can explore it
to learn more about thread, pyQt and programming in general!
i'll give a deep look in QtAssistant too
i'l  try to "translate" what can i read for c++ (is a bit hard for a  
"non programmer")
and compare the class and methods used to in your python-code.
  .

really thanks for the help !!!

have good Q-time :-)






Massimo Di Stefano
massimodisasha at yahoo.it

epifanio on   irc.freenode.net  /join gfoss







Il giorno 06/apr/09, alle ore 00:04, Hazen Babcock ha scritto:

> Arnold Krille wrote:
>> On Sunday 05 April 2009 21:29:44 Hazen Babcock wrote:
>>> I'd suggest expanding your MyThread class to something like this:
>>> class MyThread(QtCore.QThread):
>>>     def __init__(self, parent = None):
>>>         QtCore.QThread.__init__(self, parent)
>>>         self.running = 1
>>>      def run(self):
>>>          n = 0
>>>          step = 1
>>>          while self.running:
>>>              n += step
>>>              print n
>>>              self.msleep(100)
>>>     def stop(self):
>>> 	 self.running = 0
>>> You should also look at the QtCore.QMutex() class, which provides  
>>> a way
>>> to synchronize (via locking) between the thread process and other
>>> processes.
>> Another reason while this will not work as expected is the Big  
>> Intepreter Lock. Unless QThread releases the BIL in its *sleep- 
>> functions, the thread will block the execution of the main-thread...
>> Arnold
>
> Yeah that wasn't such a good suggestion. I actually tested this one  
> and it works as expected for me:
>
> import sys
> from PyQt4 import QtCore, QtGui
> import time
>
> class MyThread(QtCore.QThread):
>    def __init__(self, parent = None):
>        QtCore.QThread.__init__(self, parent)
>        self.alive = 1
>        self.running = 0
>        self.n = 0
>
>    def run(self):
>        while self.alive:
>            step = 1
>            n = 0
>            while self.running:
>                n += step
>                print n
>                self.msleep(100)
>            self.msleep(100)
>
>    def toggle(self):
>        if self.running:
>            self.running = 0
>        else:
>            self.running = 1
>
>    def stop(self):
>        self.alive = 0
>
> class Gui(QtGui.QWidget):
>    def __init__(self, parent=None):
>        QtGui.QGroupBox.__init__(self, parent)
>        self.gcenter = QtGui.QPushButton("X", self)
>        self.gcenter.setAutoRepeat(True)
>        guiLayout = QtGui.QGridLayout()
>        guiLayout.addWidget(self.gcenter,1,0)
>        self.setLayout(guiLayout)
>        self.thread = MyThread()
>        self.thread.start()
>        self.connect(self.gcenter, QtCore.SIGNAL("clicked()"),  
> self.thread.toggle)
>
>
> if __name__ == "__main__":
>    app = QtGui.QApplication(sys.argv)
>    gui = Gui()
>    gui.show()
>    sys.exit(app.exec_())
>
>
> I assume that QThread must be releasing the GIL while sleeping.
>
> -Hazen
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> http://www.riverbankcomputing.com/mailman/listinfo/pyqt

Chiacchiera con i tuoi amici in tempo reale! 
 http://it.yahoo.com/mail_it/foot/*http://it.messenger.yahoo.com 



More information about the PyQt mailing list