2 u011045506 u011045506 于 2013.09.28 23:16 提问

求助:写了个简单的python通信窗口,为什么刚运行一会就会崩掉呢?

代码如下,不知道为什么会崩,在线等大神解答:

# -*- coding:utf-8 -*-
# file : server.py

from PyQt4 import QtGui, QtCore
import sys
import socket
import threading

class Window(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.setWindowTitle('Server')
        self.edit = QtGui.QTextEdit()
        self.grid = QtGui.QGridLayout()
        self.button = QtGui.QPushButton('Start Listening')
        self.connect(self.button, QtCore.SIGNAL('clicked()'), self.Send)
        self.grid.addWidget(self.button, 0, 0)
        self.grid.addWidget(self.edit, 1, 0)
        self.setLayout(self.grid)

    def Send(self):
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1) 
        sock.bind(('', 8033))
        sock.listen(5)
        while True:
            try:
                server, addr = sock.accept()
                data = server.recv(1024)
                self.edit.append(data)
                server.send('I got ' + data)
                server.close()
            except:
                self.edit.append('Cut Down')
                break
        sock.close()

app = QtGui.QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())

1个回答

qq_30135665
qq_30135665   2016.08.22 23:21

if name=="__main__":
app = QtGui.QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
试一下这样写

Csdn user default icon
上传中...
上传图片
插入图片