每当我关闭窗口,我的代码运行,它似乎仍然在后台运行。到目前为止我还不明白为什么会这样。作为第一个直觉,我考虑在可视代码中逐步跟踪代码的执行。怎么做呢? 我运行的代码如下:
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
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。