Can you please tell me why i can't run this sample test program??

from PyQt4 import QtGui
class testui(QtGui.QTabWidget):    
    def __init__(self,parent=None):
        QtGui.QTabWidget.__init__(self)
    def test(self):
        QtGui.QMessageBox.warning(self,"Hello","This is a test message")

def frun(objec):
    time.sleep(8)
    print('hello')
    objec.test()
   
app=QtGui.QApplication(sys.argv)
obj=testui()
obj.show()
threading.Thread(target=frun,args=(obj,)).start()
app.exec_()

But in real i want to receive messages from the threads running in other python modules and display or process in my pyqt application.. If any of you ever did that share with me.. Thanks..

i want to receive messages from the threads running in other python modules and display or process in my pyqt application.

You would have to communicate between the threads. I use multiprocessing instead of threading and so would communicate through a manager list or dictionary. This is an example of multiprocessing. Note that the function sleeps for 1/2 second, and the __main__() sleeps for 3/4 second so they won't be in sync. "multiprocessing" in included in Python versions greater than 2.5, otherwise you will have to install pyprocessing (same thing) http://pyprocessing.berlios.de/

import time
from multiprocessing import Process, Manager

def test_f(test_d):
   while not test_d["QUIT"]:
      ## change the counter's value
      test_d["ctr"] += 1
      time.sleep(0.5)


if __name__ == '__main__':
   ## define the dictionary to be used as a counter
   manager = Manager()
   test_d = manager.dict()
   test_d["ctr"] = 0
   test_d["QUIT"] = False

   ## start the process
   p = Process(target=test_f, args=(test_d,))
   p.start()

   for x in range(7):
       print test_d["ctr"]
       time.sleep(0.75)

   ## exit the process/function
   test_d["QUIT"] = True
   print "\ntest_d dictionary changed to terminate loop"

   p.terminate()

You would have to communicate between the threads.

Thank you. To my knowledge we can use shared objects in threading comfortably. But my aim is to not communicate between threads.. Let me state my objective clearly. My aim is to call a method in pyqt from a python thread I've stated a minimal example here...

***********************
labeldisplay.py
**********************
from PyQt4 import QtGui
import sys
class label(QtGui.QDialog):
    def __init__(self,parent=None):
        QtGui.QDialog.__init__(self,parent)
        #self.labeltext("Hello")
        
    def labeltext(self,text):
        self.labl=QtGui.QLabel(self)
        self.labl.setText(text)
        
app=QtGui.QApplication(sys.argv)
obj=label()
obj.show()
app.exec_()

******************************
calllabeltest.py
*****************************
import labeldisplay
import threading

def frun(limit):
    for i in range(limit):
        print(i)
        callLabel=labeldisplay.label()
        callLabel.labeltext(str(i))
        
threading.Thread(target=frun,args=(10,)).start()

Expecting your answers.. Regards..

My aim is to call a method in pyqt from a python thread

And how do you propose to do this without some sort of communication? If you want to update a label, as an example, then one way to do it is to update some common variable. Otherwise, you have to have the class and the function in two, separate threads. Otherwise, the threaded function would not run until the qt class is exited, or vice-versa depending on how they are called.

And how do you propose to do this without some sort of communication? If you want to update a label, as an example, then one way to do it is to update some common variable. Otherwise, you have to have the class and the function in two, separate threads. Otherwise, the threaded function would not run until the qt class is exited, or vice-versa depending on how they are called.

Thanks for replying..
Yes that's what i required(a common variable). Got an example here.. http://www.informit.com/articles/article.aspx?p=30708&seqNum=3 . Here the queue is shared. Right now i'm using a queue to get connected with Qt..
Anyhow do you know any other way of doing this???.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.