在第五篇中,简单实现了命令行下的 TCP/UDP server和client的单机通讯。
在第六篇中,实现了PyQt6开发界面,TCP协议实现的单机server和client的通讯功能。
这一篇,在第六篇的基础上,增加了UDP server和client的单机通讯功能。
一、界面
二、对比命令行代码的封装示意
1、UDP Server 界面实现服务配置和数据提供
2、UDP Server封装成2个部分:UDPServer和UDPServerSocketReceiveThread
3、server 完整代码
import socketfrom PyQt6.QtCore import QThread, pyqtSignalclass UDPServer:def __init__(self, ui, server_ip, server_hostname, server_port):self.clientAddr = Noneself.ui = ui # 主界面self.ip = server_ip # 服务器ip地址self.port = server_port # 服务器端口号self.serverName = server_hostname # 显示名称self.is_running = False # 是否已经启动self.socket = None # socketself.socketThread = None # 新的 socket receive 线程self.start()def start(self):if not self.is_running:self.is_running = Trueself.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)self.socket.bind((self.ip, self.port)) # 绑定IP与端口self.startSocketReceiveThread()def startSocketReceiveThread(self):self.socketThread = UDPServerSocketReceiveThread(self.socket)self.socketThread.receivedClientData.connect(self.show_client_message)self.socketThread.clientAddr.connect(self.server_status_trigger)self.socketThread.start()def send_data_to_client(self, message):self.ui.textEdit_3.append(self.serverName + ':' + message)self.socket.sendto(message.encode(), self.clientAddr)def server_status_trigger(self, clientAddr):self.clientAddr = clientAddrdef show_client_message(self, message):self.ui.textEdit_3.append('客户端:' + message)class UDPServerSocketReceiveThread(QThread):receivedClientData: pyqtSignal = pyqtSignal(str) # 向主线程发送客户端的数据clientAddr: pyqtSignal = pyqtSignal(tuple) # 向主线程发送服务器状态def __init__(self, serverSocket):super(UDPServerSocketReceiveThread, self).__init__()self.serverSocket = serverSocketself.clientSocket = Noneself.addr = Noneself.is_running = Truedef run(self):self.startReceiveData()def startReceiveData(self):while self.is_running:try:message, clientAddress = self.serverSocket.recvfrom(2048)self.receivedClientData.emit(message.decode('utf-8'))self.clientAddr.emit(clientAddress)except ConnectionResetError as reason:self.is_running = Falsebreakclass TCPServer:def __init__(self, ui, server_ip, server_hostname, server_port):self.ui = ui # 主界面self.ip = server_ip # 服务器ip地址self.port = server_port # 服务器端口号self.serverName = server_hostname # 显示名称self.is_running = False # 是否已经启动self.socket = None # socketself.socketThread = None # 新的 socket receive 线程self.start()def start(self):if not self.is_running:self.is_running = Trueself.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)self.socket.bind((self.ip, self.port)) # 绑定IP与端口self.socket.listen(1) # 设定最大连接数self.startSocketReceiveThread()def stop(self):try:if self.is_running:self.is_running = Falseif self.socketThread.is_running:self.socketThread.stop()except Exception as e:print(e)def startSocketReceiveThread(self):self.socketThread = TCPServerSocketReceiveThread(self.socket)self.socketThread.clientConnection.connect(self.socket_client_connect_trigger)self.socketThread.receivedClientData.connect(self.show_client_message)self.socketThread.serverStatus.connect(self.server_status_trigger)self.socketThread.start()def server_status_trigger(self, status):self.ui.statusbar.showMessage(status)def socket_client_connect_trigger(self, state):if state == 'connect':self.ui.statusbar.showMessage("客户端已经连接。")else:self.ui.statusbar.showMessage("客户端已经断开。")def show_client_message(self, message):self.ui.textEdit.append('客户端:' + message)def send_message_to_client(self, message):if self.is_running:self.ui.textEdit.append(self.serverName + ':' + message)self.socketThread.send_data_to_client(message)class TCPServerSocketReceiveThread(QThread):clientConnection: pyqtSignal = pyqtSignal(str) # 向主线程发送连接状态标志receivedClientData: pyqtSignal = pyqtSignal(str) # 向主线程发送接受到客户端的数据serverStatus: pyqtSignal = pyqtSignal(str) # 向主线程发送服务器状态def __init__(self, serverSocket):super(TCPServerSocketReceiveThread, self).__init__()self.serverSocket = serverSocketself.clientSocket = Noneself.addr = Noneself.is_running = Truedef run(self):self.serverStatus.emit("服务已经启动,等待客户端的连接......")self.clientSocket, self.addr = self.serverSocket.accept() # 接受客户端的连接self.emitConnectEvent('connect') # 发送客户端连接成功通知到主界面self.startReceiveData()def startReceiveData(self):while self.is_running:try:data = self.clientSocket.recv(1024).decode('utf-8') # 接受到字符串并按照utf-8编译if not data:self.emitConnectEvent('disconnect') # 发送客户端断开通知到主界面breakself.sendClientDataToUi(data)except ConnectionResetError as reason:self.sendClientDataToUi("已经离开对话。")self.is_running = Falseself.emitConnectEvent('disconnect') # 发送客户端断开通知到主界面breakself.clientSocket.close()self.serverSocket.close()self.serverStatus.emit("服务已经关闭。")def send_data_to_client(self, message):try:self.clientSocket.send(message.encode("utf-8"))except Exception as reason:print("发送失败,原因:", reason)def stop(self):if self.is_running:self.is_running = Falsedef emitConnectEvent(self, state):self.clientConnection.emit(state)def sendClientDataToUi(self, message):self.receivedClientData.emit(message)
4、client 完整代码
import socket
from PyQt6.QtCore import QThread, pyqtSignalclass UDPClient:def __init__(self, ui, ip, clientName, port):self.ui = uiself.ip = ipself.hostName = clientNameself.port = portself.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)self.socketThread = Noneself.startSocketReceiveThread()def send_udp_data(self, sentence):self.ui.textEdit_4.append(self.hostName + ":" + sentence)self.socket.sendto(sentence.encode(), (self.ip, self.port))def startSocketReceiveThread(self):self.socketThread = UDPClientSocketReceiveThread(self.socket)self.socketThread.receivedServerData.connect(self.show_server_message)self.socketThread.start()def show_server_message(self, message):self.ui.textEdit_4.append('服务端:' + message)class UDPClientSocketReceiveThread(QThread):receivedServerData: pyqtSignal = pyqtSignal(str) # 向主线程发送接受到客户端的数据def __init__(self, clientSocket):super(UDPClientSocketReceiveThread, self).__init__()self.clientSocket = clientSocketself.addr = Noneself.is_running = Truedef run(self):self.startReceiveData()def startReceiveData(self):while self.is_running:try:message, clientAddress = self.clientSocket.recvfrom(2048)self.receivedServerData.emit(message.decode('utf-8'))except ConnectionResetError as reason:self.is_running = Falsebreakclass TCPClient:def __init__(self, ui, ip, clientName, port):self.ui = uiself.ip = ipself.hostName = clientNameself.port = portself.socket = Noneself.socketThread = Noneself.connect_server()def connect_server(self):self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)self.socketThread = TCPClientSocketReceiveThread(self.socket)self.socketThread.receivedServerData.connect(self.update_ui_chat_content)if self.connect_success(self.ip, self.port):self.socketThread.start()def update_ui_chat_content(self, serverMessage):self.ui.textEdit_2.append("服务端:" + serverMessage)def stop(self):self.socketThread.stop()def send_data(self, sentence):self.ui.textEdit_2.append(self.hostName + ":" + sentence)self.socket.send(sentence.encode())def connect_success(self, ip, port):try:self.socket.connect((ip, port))return Trueexcept Exception as reason:print(reason)return Falseclass TCPClientSocketReceiveThread(QThread):receivedServerData: pyqtSignal = pyqtSignal(str) # 向主线程发送接受到客户端的数据def __init__(self, clientSocket):super(TCPClientSocketReceiveThread, self).__init__()self.clientSocket = clientSocketself.is_running = Truedef stop(self):self.is_running = Falseself.clientSocket.close()def run(self):while self.is_running:try:msg = self.clientSocket.recv(1024).decode("utf-8") # 接受服务端消息if not msg:breakself.receivedServerData.emit(msg)except Exception as reason:print(reason)breakself.stop()self.receivedServerData.emit("已经与服务端断开。")
5、主控逻辑代码
"""
主窗口模块
"""
from PyQt6 import QtWidgets
from .server import TCPServer, UDPServer
from .client import TCPClient, UDPClient
from ui.ui_Main import Ui_MainWindowclass MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):"""主窗口初始化"""def __init__(self):super(MainWindow, self).__init__()self.setupUi(self)self.show()self.pushButton.clicked.connect(self.tcp_server_start)self.pushButton_2.clicked.connect(self.tcp_server_stop)self.pushButton_4.clicked.connect(self.tcp_server_send_data)self.pushButton_5.clicked.connect(self.client_connect_server)self.pushButton_6.clicked.connect(self.client_disconnect_server)self.pushButton_7.clicked.connect(self.client_send_data)self.pushButton_8.clicked.connect(self.udp_server_start)self.pushButton_12.clicked.connect(self.send_udp_data_to_client)self.pushButton_13.clicked.connect(self.send_udp_data_to_server)self.tcpServer = Noneself.tcpClient = Noneself.udpServer = Noneself.udpClient = Nonedef send_udp_data_to_client(self):self.udpServer.send_data_to_client(self.lineEdit_11.text())def send_udp_data_to_server(self):if self.udpClient is None:server_ip = self.lineEdit_9.text()server_port = int(self.lineEdit_10.text())client_name = '客户端'self.udpClient = UDPClient(self, server_ip, client_name, server_port)self.udpClient.send_udp_data(self.lineEdit_12.text())def client_connect_server(self):server_ip = self.lineEdit_5.text()server_port = int(self.lineEdit_6.text())client_name = '客户端'self.tcpClient = TCPClient(self, server_ip, client_name, server_port)def client_disconnect_server(self):self.tcpClient.stop()def client_send_data(self):message = self.lineEdit_7.text()self.tcpClient.send_data(message)def tcp_server_start(self):server_ip = self.lineEdit.text()server_port = int(self.lineEdit_2.text())server_name = '服务器'self.tcpServer = TCPServer(self, server_ip, server_name, server_port)def tcp_server_stop(self):self.tcpServer.stop()def tcp_server_send_data(self):message = self.lineEdit_4.text()self.tcpServer.send_message_to_client(message)def udp_server_start(self):server_ip = self.lineEdit_3.text()server_port = int(self.lineEdit_8.text())server_name = '服务器'self.udpServer = UDPServer(self, server_ip, server_name, server_port)
6、Ui完整代码
# Form implementation generated from reading ui file 'D:\projects\python-projects\dhcp-server\ui\Main.ui'
#
# Created by: PyQt6 UI code generator 6.4.2
#
# WARNING: Any manual changes made to this file will be lost when pyuic6 is
# run again. Do not edit this file unless you know what you are doing.from PyQt6 import QtCore, QtGui, QtWidgetsclass Ui_MainWindow(object):def setupUi(self, MainWindow):MainWindow.setObjectName("MainWindow")MainWindow.resize(934, 600)self.centralwidget = QtWidgets.QWidget(parent=MainWindow)self.centralwidget.setObjectName("centralwidget")self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)self.verticalLayout.setObjectName("verticalLayout")self.tabWidget = QtWidgets.QTabWidget(parent=self.centralwidget)self.tabWidget.setObjectName("tabWidget")self.tab = QtWidgets.QWidget()self.tab.setObjectName("tab")self.horizontalLayout_10 = QtWidgets.QHBoxLayout(self.tab)self.horizontalLayout_10.setObjectName("horizontalLayout_10")self.verticalLayout_5 = QtWidgets.QVBoxLayout()self.verticalLayout_5.setObjectName("verticalLayout_5")self.groupBox = QtWidgets.QGroupBox(parent=self.tab)font = QtGui.QFont()font.setPointSize(11)self.groupBox.setFont(font)self.groupBox.setObjectName("groupBox")self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.groupBox)self.verticalLayout_2.setObjectName("verticalLayout_2")self.horizontalLayout_2 = QtWidgets.QHBoxLayout()self.horizontalLayout_2.setObjectName("horizontalLayout_2")self.label = QtWidgets.QLabel(parent=self.groupBox)self.label.setObjectName("label")self.horizontalLayout_2.addWidget(self.label)self.lineEdit = QtWidgets.QLineEdit(parent=self.groupBox)self.lineEdit.setObjectName("lineEdit")self.horizontalLayout_2.addWidget(self.lineEdit)self.pushButton_3 = QtWidgets.QPushButton(parent=self.groupBox)self.pushButton_3.setObjectName("pushButton_3")self.horizontalLayout_2.addWidget(self.pushButton_3)self.verticalLayout_2.addLayout(self.horizontalLayout_2)self.horizontalLayout_3 = QtWidgets.QHBoxLayout()self.horizontalLayout_3.setObjectName("horizontalLayout_3")self.label_2 = QtWidgets.QLabel(parent=self.groupBox)self.label_2.setObjectName("label_2")self.horizontalLayout_3.addWidget(self.label_2)self.lineEdit_2 = QtWidgets.QLineEdit(parent=self.groupBox)self.lineEdit_2.setObjectName("lineEdit_2")self.horizontalLayout_3.addWidget(self.lineEdit_2)spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Policy.Expanding, QtWidgets.QSizePolicy.Policy.Minimum)self.horizontalLayout_3.addItem(spacerItem)self.horizontalLayout_3.setStretch(2, 1)self.verticalLayout_2.addLayout(self.horizontalLayout_3)self.horizontalLayout_4 = QtWidgets.QHBoxLayout()self.horizontalLayout_4.setObjectName("horizontalLayout_4")self.verticalLayout_2.addLayout(self.horizontalLayout_4)self.verticalLayout_5.addWidget(self.groupBox)self.verticalLayout_4 = QtWidgets.QVBoxLayout()self.verticalLayout_4.setObjectName("verticalLayout_4")self.horizontalLayout = QtWidgets.QHBoxLayout()self.horizontalLayout.setObjectName("horizontalLayout")spacerItem1 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Policy.Expanding, QtWidgets.QSizePolicy.Policy.Minimum)self.horizontalLayout.addItem(spacerItem1)self.pushButton = QtWidgets.QPushButton(parent=self.tab)font = QtGui.QFont()font.setPointSize(11)self.pushButton.setFont(font)self.pushButton.setObjectName("pushButton")self.horizontalLayout.addWidget(self.pushButton)spacerItem2 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Policy.Expanding, QtWidgets.QSizePolicy.Policy.Minimum)self.horizontalLayout.addItem(spacerItem2)self.pushButton_2 = QtWidgets.QPushButton(parent=self.tab)font = QtGui.QFont()font.setPointSize(11)self.pushButton_2.setFont(font)self.pushButton_2.setObjectName("pushButton_2")self.horizontalLayout.addWidget(self.pushButton_2)spacerItem3 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Policy.Expanding, QtWidgets.QSizePolicy.Policy.Minimum)self.horizontalLayout.addItem(spacerItem3)self.verticalLayout_4.addLayout(self.horizontalLayout)self.verticalLayout_3 = QtWidgets.QVBoxLayout()self.verticalLayout_3.setObjectName("verticalLayout_3")self.textEdit = QtWidgets.QTextEdit(parent=self.tab)self.textEdit.setObjectName("textEdit")self.verticalLayout_3.addWidget(self.textEdit)self.horizontalLayout_5 = QtWidgets.QHBoxLayout()self.horizontalLayout_5.setObjectName("horizontalLayout_5")self.lineEdit_4 = QtWidgets.QLineEdit(parent=self.tab)self.lineEdit_4.setObjectName("lineEdit_4")self.horizontalLayout_5.addWidget(self.lineEdit_4)self.pushButton_4 = QtWidgets.QPushButton(parent=self.tab)font = QtGui.QFont()font.setPointSize(11)self.pushButton_4.setFont(font)self.pushButton_4.setObjectName("pushButton_4")self.horizontalLayout_5.addWidget(self.pushButton_4)self.verticalLayout_3.addLayout(self.horizontalLayout_5)self.verticalLayout_4.addLayout(self.verticalLayout_3)self.verticalLayout_5.addLayout(self.verticalLayout_4)self.horizontalLayout_10.addLayout(self.verticalLayout_5)self.verticalLayout_8 = QtWidgets.QVBoxLayout()self.verticalLayout_8.setObjectName("verticalLayout_8")self.groupBox_2 = QtWidgets.QGroupBox(parent=self.tab)font = QtGui.QFont()font.setPointSize(11)self.groupBox_2.setFont(font)self.groupBox_2.setObjectName("groupBox_2")self.verticalLayout_9 = QtWidgets.QVBoxLayout(self.groupBox_2)self.verticalLayout_9.setObjectName("verticalLayout_9")self.horizontalLayout_6 = QtWidgets.QHBoxLayout()self.horizontalLayout_6.setObjectName("horizontalLayout_6")self.label_4 = QtWidgets.QLabel(parent=self.groupBox_2)self.label_4.setObjectName("label_4")self.horizontalLayout_6.addWidget(self.label_4)self.lineEdit_5 = QtWidgets.QLineEdit(parent=self.groupBox_2)self.lineEdit_5.setObjectName("lineEdit_5")self.horizontalLayout_6.addWidget(self.lineEdit_5)self.verticalLayout_9.addLayout(self.horizontalLayout_6)self.horizontalLayout_7 = QtWidgets.QHBoxLayout()self.horizontalLayout_7.setObjectName("horizontalLayout_7")self.label_5 = QtWidgets.QLabel(parent=self.groupBox_2)self.label_5.setObjectName("label_5")self.horizontalLayout_7.addWidget(self.label_5)self.lineEdit_6 = QtWidgets.QLineEdit(parent=self.groupBox_2)self.lineEdit_6.setObjectName("lineEdit_6")self.horizontalLayout_7.addWidget(self.lineEdit_6)self.verticalLayout_9.addLayout(self.horizontalLayout_7)self.verticalLayout_8.addWidget(self.groupBox_2)self.verticalLayout_7 = QtWidgets.QVBoxLayout()self.verticalLayout_7.setObjectName("verticalLayout_7")self.horizontalLayout_8 = QtWidgets.QHBoxLayout()self.horizontalLayout_8.setObjectName("horizontalLayout_8")spacerItem4 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Policy.Expanding, QtWidgets.QSizePolicy.Policy.Minimum)self.horizontalLayout_8.addItem(spacerItem4)self.pushButton_5 = QtWidgets.QPushButton(parent=self.tab)font = QtGui.QFont()font.setPointSize(11)self.pushButton_5.setFont(font)self.pushButton_5.setObjectName("pushButton_5")self.horizontalLayout_8.addWidget(self.pushButton_5)spacerItem5 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Policy.Expanding, QtWidgets.QSizePolicy.Policy.Minimum)self.horizontalLayout_8.addItem(spacerItem5)self.pushButton_6 = QtWidgets.QPushButton(parent=self.tab)font = QtGui.QFont()font.setPointSize(11)self.pushButton_6.setFont(font)self.pushButton_6.setObjectName("pushButton_6")self.horizontalLayout_8.addWidget(self.pushButton_6)spacerItem6 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Policy.Expanding, QtWidgets.QSizePolicy.Policy.Minimum)self.horizontalLayout_8.addItem(spacerItem6)self.verticalLayout_7.addLayout(self.horizontalLayout_8)self.verticalLayout_6 = QtWidgets.QVBoxLayout()self.verticalLayout_6.setObjectName("verticalLayout_6")self.textEdit_2 = QtWidgets.QTextEdit(parent=self.tab)self.textEdit_2.setObjectName("textEdit_2")self.verticalLayout_6.addWidget(self.textEdit_2)self.horizontalLayout_9 = QtWidgets.QHBoxLayout()self.horizontalLayout_9.setObjectName("horizontalLayout_9")self.lineEdit_7 = QtWidgets.QLineEdit(parent=self.tab)self.lineEdit_7.setObjectName("lineEdit_7")self.horizontalLayout_9.addWidget(self.lineEdit_7)self.pushButton_7 = QtWidgets.QPushButton(parent=self.tab)font = QtGui.QFont()font.setPointSize(11)self.pushButton_7.setFont(font)self.pushButton_7.setObjectName("pushButton_7")self.horizontalLayout_9.addWidget(self.pushButton_7)self.verticalLayout_6.addLayout(self.horizontalLayout_9)self.verticalLayout_7.addLayout(self.verticalLayout_6)self.verticalLayout_8.addLayout(self.verticalLayout_7)self.horizontalLayout_10.addLayout(self.verticalLayout_8)self.tabWidget.addTab(self.tab, "")self.tab_2 = QtWidgets.QWidget()self.tab_2.setObjectName("tab_2")self.horizontalLayout_19 = QtWidgets.QHBoxLayout(self.tab_2)self.horizontalLayout_19.setObjectName("horizontalLayout_19")self.verticalLayout_16 = QtWidgets.QVBoxLayout()self.verticalLayout_16.setObjectName("verticalLayout_16")self.verticalLayout_13 = QtWidgets.QVBoxLayout()self.verticalLayout_13.setObjectName("verticalLayout_13")self.groupBox_3 = QtWidgets.QGroupBox(parent=self.tab_2)font = QtGui.QFont()font.setPointSize(11)self.groupBox_3.setFont(font)self.groupBox_3.setObjectName("groupBox_3")self.verticalLayout_11 = QtWidgets.QVBoxLayout(self.groupBox_3)self.verticalLayout_11.setObjectName("verticalLayout_11")self.horizontalLayout_11 = QtWidgets.QHBoxLayout()self.horizontalLayout_11.setContentsMargins(-1, -1, 5, -1)self.horizontalLayout_11.setObjectName("horizontalLayout_11")self.label_3 = QtWidgets.QLabel(parent=self.groupBox_3)self.label_3.setObjectName("label_3")self.horizontalLayout_11.addWidget(self.label_3)self.lineEdit_3 = QtWidgets.QLineEdit(parent=self.groupBox_3)self.lineEdit_3.setObjectName("lineEdit_3")self.horizontalLayout_11.addWidget(self.lineEdit_3)self.verticalLayout_11.addLayout(self.horizontalLayout_11)self.horizontalLayout_12 = QtWidgets.QHBoxLayout()self.horizontalLayout_12.setContentsMargins(-1, -1, 5, -1)self.horizontalLayout_12.setObjectName("horizontalLayout_12")self.label_6 = QtWidgets.QLabel(parent=self.groupBox_3)self.label_6.setObjectName("label_6")self.horizontalLayout_12.addWidget(self.label_6)self.lineEdit_8 = QtWidgets.QLineEdit(parent=self.groupBox_3)self.lineEdit_8.setObjectName("lineEdit_8")self.horizontalLayout_12.addWidget(self.lineEdit_8)self.verticalLayout_11.addLayout(self.horizontalLayout_12)self.verticalLayout_13.addWidget(self.groupBox_3)self.horizontalLayout_15 = QtWidgets.QHBoxLayout()self.horizontalLayout_15.setObjectName("horizontalLayout_15")spacerItem7 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Policy.Expanding, QtWidgets.QSizePolicy.Policy.Minimum)self.horizontalLayout_15.addItem(spacerItem7)self.pushButton_8 = QtWidgets.QPushButton(parent=self.tab_2)font = QtGui.QFont()font.setPointSize(11)self.pushButton_8.setFont(font)self.pushButton_8.setObjectName("pushButton_8")self.horizontalLayout_15.addWidget(self.pushButton_8)spacerItem8 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Policy.Expanding, QtWidgets.QSizePolicy.Policy.Minimum)self.horizontalLayout_15.addItem(spacerItem8)spacerItem9 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Policy.Expanding, QtWidgets.QSizePolicy.Policy.Minimum)self.horizontalLayout_15.addItem(spacerItem9)self.verticalLayout_13.addLayout(self.horizontalLayout_15)self.verticalLayout_16.addLayout(self.verticalLayout_13)self.verticalLayout_15 = QtWidgets.QVBoxLayout()self.verticalLayout_15.setObjectName("verticalLayout_15")self.textEdit_3 = QtWidgets.QTextEdit(parent=self.tab_2)self.textEdit_3.setObjectName("textEdit_3")self.verticalLayout_15.addWidget(self.textEdit_3)self.horizontalLayout_17 = QtWidgets.QHBoxLayout()self.horizontalLayout_17.setObjectName("horizontalLayout_17")self.lineEdit_11 = QtWidgets.QLineEdit(parent=self.tab_2)self.lineEdit_11.setObjectName("lineEdit_11")self.horizontalLayout_17.addWidget(self.lineEdit_11)self.pushButton_12 = QtWidgets.QPushButton(parent=self.tab_2)font = QtGui.QFont()font.setPointSize(11)self.pushButton_12.setFont(font)self.pushButton_12.setObjectName("pushButton_12")self.horizontalLayout_17.addWidget(self.pushButton_12)self.verticalLayout_15.addLayout(self.horizontalLayout_17)self.verticalLayout_16.addLayout(self.verticalLayout_15)self.horizontalLayout_19.addLayout(self.verticalLayout_16)self.verticalLayout_17 = QtWidgets.QVBoxLayout()self.verticalLayout_17.setObjectName("verticalLayout_17")self.verticalLayout_12 = QtWidgets.QVBoxLayout()self.verticalLayout_12.setObjectName("verticalLayout_12")self.groupBox_4 = QtWidgets.QGroupBox(parent=self.tab_2)font = QtGui.QFont()font.setPointSize(11)self.groupBox_4.setFont(font)self.groupBox_4.setObjectName("groupBox_4")self.verticalLayout_10 = QtWidgets.QVBoxLayout(self.groupBox_4)self.verticalLayout_10.setObjectName("verticalLayout_10")self.horizontalLayout_13 = QtWidgets.QHBoxLayout()self.horizontalLayout_13.setObjectName("horizontalLayout_13")self.label_7 = QtWidgets.QLabel(parent=self.groupBox_4)self.label_7.setObjectName("label_7")self.horizontalLayout_13.addWidget(self.label_7)self.lineEdit_9 = QtWidgets.QLineEdit(parent=self.groupBox_4)self.lineEdit_9.setObjectName("lineEdit_9")self.horizontalLayout_13.addWidget(self.lineEdit_9)self.verticalLayout_10.addLayout(self.horizontalLayout_13)self.horizontalLayout_14 = QtWidgets.QHBoxLayout()self.horizontalLayout_14.setObjectName("horizontalLayout_14")self.label_8 = QtWidgets.QLabel(parent=self.groupBox_4)self.label_8.setObjectName("label_8")self.horizontalLayout_14.addWidget(self.label_8)self.lineEdit_10 = QtWidgets.QLineEdit(parent=self.groupBox_4)self.lineEdit_10.setObjectName("lineEdit_10")self.horizontalLayout_14.addWidget(self.lineEdit_10)self.verticalLayout_10.addLayout(self.horizontalLayout_14)self.verticalLayout_12.addWidget(self.groupBox_4)self.horizontalLayout_16 = QtWidgets.QHBoxLayout()self.horizontalLayout_16.setObjectName("horizontalLayout_16")spacerItem10 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Policy.Expanding, QtWidgets.QSizePolicy.Policy.Minimum)self.horizontalLayout_16.addItem(spacerItem10)spacerItem11 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Policy.Expanding, QtWidgets.QSizePolicy.Policy.Minimum)self.horizontalLayout_16.addItem(spacerItem11)spacerItem12 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Policy.Expanding, QtWidgets.QSizePolicy.Policy.Minimum)self.horizontalLayout_16.addItem(spacerItem12)self.verticalLayout_12.addLayout(self.horizontalLayout_16)self.verticalLayout_17.addLayout(self.verticalLayout_12)self.verticalLayout_14 = QtWidgets.QVBoxLayout()self.verticalLayout_14.setObjectName("verticalLayout_14")self.textEdit_4 = QtWidgets.QTextEdit(parent=self.tab_2)self.textEdit_4.setObjectName("textEdit_4")self.verticalLayout_14.addWidget(self.textEdit_4)self.horizontalLayout_18 = QtWidgets.QHBoxLayout()self.horizontalLayout_18.setObjectName("horizontalLayout_18")self.lineEdit_12 = QtWidgets.QLineEdit(parent=self.tab_2)self.lineEdit_12.setObjectName("lineEdit_12")self.horizontalLayout_18.addWidget(self.lineEdit_12)self.pushButton_13 = QtWidgets.QPushButton(parent=self.tab_2)font = QtGui.QFont()font.setPointSize(11)self.pushButton_13.setFont(font)self.pushButton_13.setObjectName("pushButton_13")self.horizontalLayout_18.addWidget(self.pushButton_13)self.verticalLayout_14.addLayout(self.horizontalLayout_18)self.verticalLayout_17.addLayout(self.verticalLayout_14)self.horizontalLayout_19.addLayout(self.verticalLayout_17)self.tabWidget.addTab(self.tab_2, "")self.verticalLayout.addWidget(self.tabWidget)MainWindow.setCentralWidget(self.centralwidget)self.menubar = QtWidgets.QMenuBar(parent=MainWindow)self.menubar.setGeometry(QtCore.QRect(0, 0, 934, 22))self.menubar.setObjectName("menubar")MainWindow.setMenuBar(self.menubar)self.statusbar = QtWidgets.QStatusBar(parent=MainWindow)font = QtGui.QFont()font.setPointSize(11)self.statusbar.setFont(font)self.statusbar.setObjectName("statusbar")MainWindow.setStatusBar(self.statusbar)self.retranslateUi(MainWindow)self.tabWidget.setCurrentIndex(1)QtCore.QMetaObject.connectSlotsByName(MainWindow)def retranslateUi(self, MainWindow):_translate = QtCore.QCoreApplication.translateMainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))self.groupBox.setTitle(_translate("MainWindow", "服务器配置:"))self.label.setText(_translate("MainWindow", "服务端IP地址:"))self.lineEdit.setText(_translate("MainWindow", "127.0.0.1"))self.pushButton_3.setText(_translate("MainWindow", "使用本机IP地址"))self.label_2.setText(_translate("MainWindow", "服务器端口:"))self.lineEdit_2.setText(_translate("MainWindow", "12000"))self.pushButton.setText(_translate("MainWindow", "启动服务"))self.pushButton_2.setText(_translate("MainWindow", "停止服务"))self.pushButton_4.setText(_translate("MainWindow", "发送"))self.groupBox_2.setTitle(_translate("MainWindow", "客户端配置"))self.label_4.setText(_translate("MainWindow", "服务器IP:"))self.lineEdit_5.setText(_translate("MainWindow", "127.0.0.1"))self.label_5.setText(_translate("MainWindow", "服务器端口:"))self.lineEdit_6.setText(_translate("MainWindow", "12000"))self.pushButton_5.setText(_translate("MainWindow", "连接服务器"))self.pushButton_6.setText(_translate("MainWindow", "断开连接"))self.pushButton_7.setText(_translate("MainWindow", "发送"))self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("MainWindow", "TCP协议"))self.groupBox_3.setTitle(_translate("MainWindow", "服务器设置"))self.label_3.setText(_translate("MainWindow", "服务器IP地址:"))self.lineEdit_3.setText(_translate("MainWindow", "127.0.0.1"))self.label_6.setText(_translate("MainWindow", "服务器端口:"))self.lineEdit_8.setText(_translate("MainWindow", "12000"))self.pushButton_8.setText(_translate("MainWindow", "启动服务"))self.pushButton_12.setText(_translate("MainWindow", "发送"))self.groupBox_4.setTitle(_translate("MainWindow", "客户端设置"))self.label_7.setText(_translate("MainWindow", "服务器IP地址:"))self.lineEdit_9.setText(_translate("MainWindow", "127.0.0.1"))self.label_8.setText(_translate("MainWindow", "服务器端口:"))self.lineEdit_10.setText(_translate("MainWindow", "12000"))self.pushButton_13.setText(_translate("MainWindow", "发送"))self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), _translate("MainWindow", "UDP协议"))
7、程序启动代码
import sys
from module.main import MainWindow
from PyQt6.QtWidgets import QApplicationif __name__ == '__main__':app = QApplication(sys.argv)login = MainWindow()sys.exit(app.exec())