[PyQt] [python/Qt4]a problem of QThread

tsic imen_tsic at hotmail.com
Tue May 6 08:00:58 BST 2008


Hello,
I tried to program a chat interface between a server and many clients.
when I used Qthread this message apear when I execute my program file from
DOS:
QThread: Destroyed while thread is still running
And stop the execution of the program because pythonw.exe has find a problem
and must close...
this is my code

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'interf_s.ui'
#
# Created: Tue Apr 29 12:55:58 2008
#      by: PyQt4 UI code generator 4.3.3
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import socket, sys, threading
import time
import locale

class Thread(QThread):
    #lock = QReadWriteLock()
    def __init__(self, host, port):
        super(Thread, self).__init__()
        #self.socketId = socketId
        self.host = host
        self.port = port
    
    def run(self):
        HOST = self.host
        PORT = int(self.port)
        
        mySocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        
        try:
            mySocket.bind((HOST, PORT))
        except socket.error:
            #self.envoies.setText("La liaison du socket a l'adresse choisie
a echoue.")
            print "La liaison du socket a l'adresse choisie a echoue."
            sys.exit()
        while 1:
            #self.envoies.append("Serveur pret en attente de requetes .")
            print "Serveur pret en attente de requetes ."
            mySocket.listen(5)   
            connexion, adresse = mySocket.accept()
            c= time.strftime('%A %c')
            client= "Connexion au port" + str(adresse[1]) + "  a " + c
            #self.envoies.append(client)
            print client
            donneesRecues = connexion.recv(8192)
            t=time.strftime("%H:%M:%S")
            #self.envoies.append(" Donnees Recues " +t+ " > "+
donneesRecues)
            rep = " Donnees Recues " +t+ " > "+ donneesRecues
            print rep
            #rep = self.chat() 
            connexion.send(rep)
            
            #"Donnees Envoyees>"+rep

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
       
MainWindow.resize(QtCore.QSize(QtCore.QRect(0,0,610,600).size()).expandedTo(MainWindow.minimumSizeHint()))

        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")

        self.gc = QtGui.QGroupBox(self.centralwidget)
        self.gc.setGeometry(QtCore.QRect(9,9,581,111))
        self.gc.setObjectName("gc")

        self.adripes = QtGui.QLineEdit(self.gc)
        self.adripes.setGeometry(QtCore.QRect(110,30,301,20))
        self.adripes.setObjectName("adripes")

        self.label = QtGui.QLabel(self.gc)
        self.label.setGeometry(QtCore.QRect(20,30,71,16))
        self.label.setObjectName("label")

        self.label_2 = QtGui.QLabel(self.gc)
        self.label_2.setGeometry(QtCore.QRect(20,60,91,16))
        self.label_2.setObjectName("label_2")

        self.nipes = QtGui.QLineEdit(self.gc)
        self.nipes.setGeometry(QtCore.QRect(110,60,301,20))
        self.nipes.setObjectName("nipes")

        self.connectes = QtGui.QPushButton(self.gc)
        self.connectes.setGeometry(QtCore.QRect(470,70,91,23))
        self.connectes.setObjectName("connectes")

        self.envoies = QtGui.QTextEdit(self.centralwidget)
        self.envoies.setGeometry(QtCore.QRect(10,150,256,271))
        self.envoies.setObjectName("envoies")

        self.chates = QtGui.QLineEdit(self.centralwidget)
        self.chates.setGeometry(QtCore.QRect(10,450,256,31))
        self.chates.setObjectName("chates")

        self.benves = QtGui.QPushButton(self.centralwidget)
        self.benves.setGeometry(QtCore.QRect(190,520,75,23))
        self.benves.setObjectName("benves")

        self.gt = QtGui.QGroupBox(self.centralwidget)
        self.gt.setGeometry(QtCore.QRect(280,140,311,401))
        self.gt.setObjectName("gt")

        self.tablees = QtGui.QTableWidget(self.gt)
        self.tablees.setGeometry(QtCore.QRect(20,40,281,181))
        self.tablees.setObjectName("tablees")

        self.ajoues = QtGui.QPushButton(self.gt)
        self.ajoues.setGeometry(QtCore.QRect(210,280,75,23))
        self.ajoues.setObjectName("ajoues")

        self.suppes = QtGui.QPushButton(self.gt)
        self.suppes.setGeometry(QtCore.QRect(210,330,75,23))
        self.suppes.setObjectName("suppes")
        MainWindow.setCentralWidget(self.centralwidget)

        self.statusbar = QtGui.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.menubar = QtGui.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0,0,610,21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)

        self.retranslateUi(MainWindow)
       
QtCore.QObject.connect(self.ajoues,QtCore.SIGNAL("clicked()"),self.ajout)
       
QtCore.QObject.connect(self.suppes,QtCore.SIGNAL("clicked()"),self.supprim)
       
QtCore.QObject.connect(self.connectes,QtCore.SIGNAL("clicked()"),self.conec)
       
QtCore.QObject.connect(self.benves,QtCore.SIGNAL("clicked()"),self.chat)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow",
"MainWindow", None, QtGui.QApplication.UnicodeUTF8))
        self.gc.setTitle(QtGui.QApplication.translate("MainWindow",
"Connexion :", None, QtGui.QApplication.UnicodeUTF8))
        self.label.setText(QtGui.QApplication.translate("MainWindow",
"Adresse IP :", None, QtGui.QApplication.UnicodeUTF8))
        self.label_2.setText(QtGui.QApplication.translate("MainWindow",
"Numero de port  :", None, QtGui.QApplication.UnicodeUTF8))
        self.connectes.setText(QtGui.QApplication.translate("MainWindow",
"Connexion", None, QtGui.QApplication.UnicodeUTF8))
        self.envoies.setHtml(QtGui.QApplication.translate("MainWindow",
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style
type=\"text/css\">\n"
        "p, li { white-space: pre-wrap; }\n"
        "</style></head><body style=\" font-family:\'MS Shell Dlg 2\';
font-size:8.25pt; font-weight:400; font-style:normal;\">\n"
        "<p style=\"-qt-paragraph-type:empty; margin-top:0px;
margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0;
text-indent:0px; font-size:8pt;\"></p></body></html>", None,
QtGui.QApplication.UnicodeUTF8))
        self.benves.setText(QtGui.QApplication.translate("MainWindow",
"Envoyer", None, QtGui.QApplication.UnicodeUTF8))
        self.gt.setTitle(QtGui.QApplication.translate("MainWindow", "Gestion
commandes :", None, QtGui.QApplication.UnicodeUTF8))
        self.tablees.clear()
        self.tablees.setColumnCount(2)
        self.tablees.setRowCount(3)

        headerItem = QtGui.QTableWidgetItem()
        headerItem.setText(QtGui.QApplication.translate("MainWindow",
"Commande 1", None, QtGui.QApplication.UnicodeUTF8))
        self.tablees.setVerticalHeaderItem(0,headerItem)

        headerItem1 = QtGui.QTableWidgetItem()
        headerItem1.setText(QtGui.QApplication.translate("MainWindow",
"Commande 2", None, QtGui.QApplication.UnicodeUTF8))
        self.tablees.setVerticalHeaderItem(1,headerItem1)

        headerItem2 = QtGui.QTableWidgetItem()
        headerItem2.setText(QtGui.QApplication.translate("MainWindow",
"Commande 3", None, QtGui.QApplication.UnicodeUTF8))
        self.tablees.setVerticalHeaderItem(2,headerItem2)

        headerItem3 = QtGui.QTableWidgetItem()
        headerItem3.setText(QtGui.QApplication.translate("MainWindow",
"Requête", None, QtGui.QApplication.UnicodeUTF8))
        self.tablees.setHorizontalHeaderItem(0,headerItem3)

        headerItem4 = QtGui.QTableWidgetItem()
        headerItem4.setText(QtGui.QApplication.translate("MainWindow",
"Reponse", None, QtGui.QApplication.UnicodeUTF8))
        self.tablees.setHorizontalHeaderItem(1,headerItem4)

        item = QtGui.QTableWidgetItem()
        item.setText(QtGui.QApplication.translate("MainWindow", "$ GPSCON",
None, QtGui.QApplication.UnicodeUTF8))
        self.tablees.setItem(0,0,item)

        item1 = QtGui.QTableWidgetItem()
        item1.setText(QtGui.QApplication.translate("MainWindow", "Bonjour",
None, QtGui.QApplication.UnicodeUTF8))
        self.tablees.setItem(0,1,item1)

        item2 = QtGui.QTableWidgetItem()
        item2.setText(QtGui.QApplication.translate("MainWindow", "$
GPSNMUN", None, QtGui.QApplication.UnicodeUTF8))
        self.tablees.setItem(1,0,item2)

        item3 = QtGui.QTableWidgetItem()
        item3.setText(QtGui.QApplication.translate("MainWindow", "OK", None,
QtGui.QApplication.UnicodeUTF8))
        self.tablees.setItem(1,1,item3)

        item4 = QtGui.QTableWidgetItem()
        item4.setText(QtGui.QApplication.translate("MainWindow", "$ GPSACP",
None, QtGui.QApplication.UnicodeUTF8))
        self.tablees.setItem(2,0,item4)

        item5 = QtGui.QTableWidgetItem()
        item5.setText(QtGui.QApplication.translate("MainWindow", "Hello",
None, QtGui.QApplication.UnicodeUTF8))
        self.tablees.setItem(2,1,item5)
        self.ajoues.setText(QtGui.QApplication.translate("MainWindow",
"Ajouter", None, QtGui.QApplication.UnicodeUTF8))
        self.suppes.setText(QtGui.QApplication.translate("MainWindow",
"Supprimer", None, QtGui.QApplication.UnicodeUTF8))

    def ajout(self):
        self.tablees.insertRow(3)
    
    def supprim(self):
        self.tablees.removeRow(3)
    
    def chat(self):
        
        rep = self.chates.text() 
        self.envoies.append("Donnees Envoyees>"+rep)      
        self.chates.clear()              
        return rep
        #self.envoies.append("Deconnexion de ", self.client_address)


    
    def conec(self):
        HOST = self.adripes.text()
        PORT = int(self.nipes.text())
        thread = Thread(HOST, PORT)
        QtCore.QObject.connect(thread, SIGNAL("finished()"),thread,
SLOT("deleteLater()"))
        thread.start()
        


if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    MainWindow = QtGui.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_()) 


thank you



-- 
View this message in context: http://www.nabble.com/-python-Qt4-a-problem-of-QThread-tp16993255p16993255.html
Sent from the PyQt mailing list archive at Nabble.com.




More information about the PyQt mailing list