开发者社区> 问答> 正文

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

每当我关闭窗口,我的代码运行,它似乎仍然在后台运行。到目前为止我还不明白为什么会这样。作为第一个直觉,我考虑在可视代码中逐步跟踪代码的执行。怎么做呢? 我运行的代码如下:

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_())       

任何帮助将不胜感激! 问题来源StackOverflow 地址:/questions/59382578/why-does-my-program-keep-running-in-the-background-after-closing-the-window

展开
收起
kun坤 2019-12-27 17:07:18 1202 0
1 条回答
写回答
取消 提交回答
  • 精于基础,广于工具,熟于业务。

    python运行和编译器是否关闭是没有关系的

    2019-12-27 18:28:41
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载