qq_43412960 2019-11-30 15:46 采纳率: 76.5%
浏览 334
已采纳

这是一个关于 PYTHON 的PYQT5的相关问题

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(340, 430, 75, 23))
        self.pushButton.setObjectName("pushButton")
        self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_2.setGeometry(QtCore.QRect(510, 430, 75, 23))
        self.pushButton_2.setObjectName("pushButton_2")
        self.textEdit = QtWidgets.QTextEdit(self.centralwidget)
        self.textEdit.setGeometry(QtCore.QRect(280, 200, 411, 161))
        self.textEdit.setObjectName("textEdit")
        self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit.setGeometry(QtCore.QRect(210, 90, 113, 20))
        self.lineEdit.setObjectName("lineEdit")
        self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit_2.setGeometry(QtCore.QRect(410, 90, 113, 20))
        self.lineEdit_2.setObjectName("lineEdit_2")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(80, 90, 54, 12))
        self.label.setObjectName("label")
        self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_3.setGeometry(QtCore.QRect(600, 100, 75, 23))
        self.pushButton_3.setObjectName("pushButton_3")
        self.lineEdit_3 = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit_3.setGeometry(QtCore.QRect(190, 430, 113, 20))
        self.lineEdit_3.setObjectName("lineEdit_3")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 23))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "发送"))
        self.pushButton_2.setText(_translate("MainWindow", "关闭"))
        self.label.setText(_translate("MainWindow", "服务器"))
        self.pushButton_3.setText(_translate("MainWindow", "连接"))
import sys
from PyQt5 import QtWidgets
import untitled
import   socket
class Mywindow(QtWidgets.QMainWindow, untitled.Ui_MainWindow):
    quest=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    def __init__(self):
        super(Mywindow, self).__init__()
        self.setupUi(self)  #继承于Ui_MainWindow
        self.pushButton_3.clicked.connect(self.lianjie)
        self.pushButton.clicked.connect(self.xiaoxi)
    def  lianjie(self):
        self.quest.connect((self.lineEdit.Text(),int(self.lineEdit_2.Text())))
    def  xiaoxi(self):
        self.quest.send(self.lineEdit_3.Text().encode())
        a=self.quest.recv(100).decode()
        self.textEdit.setText(a)
        self.quest.close()
if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    window = Mywindow()
    window.show()
    sys.exit(app.exec_())

上面是客户机代码 ,代码和PYQT5联系在一起了

下面是服务器代码,没有和窗体联系在一起

import   socket
service=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
service.bind(('127.0.0.1',6))
service.listen(3)
tao,add=service.accept()
data=tao.recv(100).decode()
tao.send(data.encode())
tao.close()
service.close()

为什么我打开客户机,连接按钮 ,系统退出,产生错误呢?

  • 写回答

1条回答 默认 最新

  • dabocaiqq 2019-11-30 18:39
    关注

    把网络通讯的代码注释掉,看看是否有错误,如果有错误,就是qt这里没有写对。
    否则就是网络问题,用你之前的代码,如果不可以,就是环境的问题,否则代码的问题

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 不懂得怎么运行下载来的代码
  • ¥15 CST导出3D模型图为什么和软件显示不一样?
  • ¥15 加热反应炉PLC控制系统设计(相关搜索:梯形图)
  • ¥15 python 用Dorc包报错,我的写法和网上教的是一样的但是它显示无效参数,是什么问题
  • ¥15 经过滑动平均后的一维信号还原用什么结构好呢?
  • ¥15 指定IP电脑的访问设置
  • ¥30 matlab ode45 未发现警告,但是运行出错
  • ¥15 为什么devc++编译项目会失败啊
  • ¥15 vscode platformio
  • ¥15 代写uni代码,app唤醒