关闭窗口后,为什么我的程序仍在后台运行?

时间:2019-12-17 21:46:45

标签: python

每当我关闭运行代码的窗口时,它似乎仍在后台运行。到目前为止,我还不知道为什么会这样。首先,我想到了逐步在可视代码中执行代码。我该怎么办?

我正在运行的代码如下:

import socket
import sys
from threading import Thread
from PyQt5.QtWidgets import QApplication,QDialog
from PChatDialog import ChatCliente_Servidor

conexion = None

class ChatServidor(QDialog):
    def __init__(self):
        super().__init__()

        self.ui = ChatCliente_Servidor()
        self.ui.setupUi(self)
        self.ui.lbl_titulo.setText('Servidor')
        self.txt_conversacion = self.ui.txt_conversacion
        self.ui.pushButton_enviar.clicked.connect(self.enviar)
        self.show()

    def enviar(self):
        global conexion
        mensaje = self.ui.txt_msj.text()
        conexion.send(mensaje.encode('utf-8'))

        self.ui.txt_conversacion.append('Servidor:{}'.format(mensaje))
        self.ui.txt_msj.setText('')

class ServidorThread(Thread):
    def __init__(self,ventana):
        super().__init__()
        self.ventana = ventana

    def run(self):
        IP = '0.0.0.0'
        Puerto = 9999

        socket_servidor=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
        socket_servidor.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
        socket_servidor.bind((IP,Puerto))

        threads = []
        socket_servidor.listen(4)

        while True:
            global conexion

            (conexion,(ip,puerto)) = socket_servidor.accept()
            t = ClienteThread(ip,puerto,self.ventana)
            t.start()
            threads.append(t)

        for t in threads:
            t.join()

class ClienteThread(Thread):
    def __init__(self,ip,puerto,ventana):
        super().__init__()
        self.ip = ip
        self.puerto = puerto
        self.ventana = ventana

    def run(self):
        while True:
            global conexion
            datos = conexion.recv(1024)
            self.ventana.txt_conversacion.append('Cliente: {}'.format(datos.decode('utf-8')))



if __name__ == '__main__':
    app = QApplication(sys.argv)
    ventana = ChatServidor()
    t = ServidorThread(ventana)
    t.start()

    ventana.exec()

    sys.exit(app.exec_())       

任何帮助将不胜感激!

0 个答案:

没有答案