python socket编程7 - 使用PyQt6 开发UI界面新增实现UDP server和client单机通讯的例子

在第五篇中,简单实现了命令行下的 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())

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/199500.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【在英伟达nvidia的jetson-orin-nx上使用调试摄像头-同时开启多个摄像头-基础测试(2)】

【在英伟达nvidia的jetson-orin-nx上使用调试摄像头-同时开启多个摄像头-USB摄像头与Camera Conn.#0/#1接口-基础测试(2)】 1、概述2、实验环境3、 先前确认(1)USB摄像头,先确认可以单独打开(2)…

Leetcode题库(数据库合集)_ 难度:困难

目录 难度:困难1. 部门工资前三高的所有员工2. 行程和用户3. 体育馆的人流量4. 员工薪水的中位数5. 同一天的第一个电话和最后一个电话6. 查询员工的累计薪水7. 给定数字的频率查询中位数8. 学生地理信息报告9. Hopper 公司查询 ①10. 职员招聘人数11. 职员招聘人数…

MPC模型预测控制理论与实践

一、基本概念 最有控制的动机是在约束条件下达到最优的系统表现。 模型预测控制(MPC,Model Predictive Control)是通过模型来预测系统在某一未来时间段内的表现来进行优化控制,多用于数位控制,通常用离散型状态空间表…

python 堆与栈

【一】堆与栈 【 1 】简介 栈(stack),有些地方称为堆栈,是一种容器,可存入数据元素、访问元素、删除元素,它的特点在于只能允许在容器的一端(称为栈顶端指标,英语:top&a…

Java_ArrayList顺序表详解

目录 前言 顺序表 ​编辑 顺序表和数组 ArrayList简介 说明 ArrayList使用​编辑 ArrayList常见操作 ArrayList实现二维数组 ArrayList的遍历 ArrayList的扩容机制 总结 前言 一个高端的程序员,往往都是数据结构学的很好,判断一个程序的优劣也是看数据结构学的好与坏.…

原生video设置控制面板controls显示哪些控件

之前我们学习了如何使用原生video播放视频 今天来一个进阶版的——设置控制面板controls显示哪些控件 先看一下当我们使用原生video时,controls属性为true时,相关代码如下: 正常的控制面板默认显示的控件有:播放、时间线、音量调…

Android基础: 使用Java代码控制 Activity类页面相互之间进行跳转 Activity页面的的启动和结束

Android基础(Activity) Activity的启动和结束 我们主要看Java代码逻辑: 第一个页面的逻辑代码 public class StartActivity01 extends AppCompatActivity implements View.OnClickListener {Overrideprotected void onCreate(Bundle saved…

香港云服务器计算型和通用型的区别

在当今数字化时代,云服务器作为企业级应用的核心设备,其性能和类型对于企业的运营和数据处理至关重要。在常见的香港云服务器类型中,通用型和计算型是最为常见的两种。那么,这两种云服务器到底有什么区别呢? 设计目标和应用场景不…

HarmonyOS开发基础(一)

HarmonyOS开发基础(一) // :装饰器:用来装饰类结构、方法、变量 Entry // Entry:标记当前组件为入口组件 Component // Component:标记为自定义组件 // struct:自定义组件,可复用的…

【LeetCode】692. 前K个高频单词

692. 前K个高频单词 描述示例解题思路及事项思路一思路二 描述 给定一个单词列表 words 和一个整数 k ,返回前 k 个出现次数最多的单词。 返回的答案应该按单词出现频率由高到低排序。如果不同的单词有相同出现频率, 按字典顺序 排序 示例 示例1 输…

Python实现FA萤火虫优化算法优化BP神经网络回归模型(BP神经网络回归算法)项目实战

说明:这是一个机器学习实战项目(附带数据代码文档视频讲解),如需数据代码文档视频讲解可以直接到文章最后获取。 1.项目背景 萤火虫算法(Fire-fly algorithm,FA)由剑桥大学Yang于2009年提出 , …

15.Servlet [一篇通]

文章目录 1.Servlet 是什么2.第一个 Servlet 程序2.1创建项目2.2引入依赖2.3创建目录2.4编写代码2.5打包程序2.6部署程序2.7验证程序 3.更方便的部署方式3.1安装 Smart Tomcat 插件3.2配置 Smart Tomcat 插件 4.访问出错怎么办?4.1出现 4044.2出现 4054.3出现 5004.4出现 &quo…

移动端APP测试方法

1 APP测试基本流程 1.1 测试周期 测试周期可按项目的开发周期来确定测试时间,一般测试时间为两三周(即15个工作日),根据项目情况以及版本质量可适当缩短或延长测试时间。正式测试前先向主管确认项目排期。 1.2 测试资源 测试任…

冬天来了,波司登的高端化“春天”不远了?

最近,羽绒服频繁“贵”上热搜。 在众多热搜词条中,一条“国产羽绒服卖到7000元”的话题一度将波司登推上了舆论的风口浪尖。 对此,波司登在最新的业绩说明会上进行了回应,公司表示:“波司登旗下主品牌及子品牌将形成差…

mysql原理--InnoDB记录结构

1.InnoDB行格式 我们平时是以记录为单位来向表中插入数据的,这些记录在磁盘上的存放方式也被称为 行格式 或者 记录格式 。 设计 InnoDB 存储引擎的大叔们到现在为止设计了4种不同类型的 行格式 ,分别是 Compact 、 Redundant 、Dynamic 和 Compressed 行…

抖音直播间购物小黄车的商品详情数据SKU接口轻松拿下

我们都知道,抖音直播购物车的商品链接只能是抖音小店的,如果没有开通抖音小店,还能添加小店链接吗? 也是可以的。 抖音直播小黄车的链接可以是自己的小店商品,也可以是别人的小店商品。 抖音直播上链接有两种方式&a…

centos7防火墙开启端口

1.查看防火墙状态 firewall-cmd --state如果返回的not running,那么需要先开启防火墙 2.开启关闭防火墙 systemctl start firewalld.service systemctl stop firewalld.service systemctl restart firewalld.service3.开放指定端口 firewall-cmd --zonepublic -…

MYSQL8用户权限配置详解

单位的系统性能问题需要把Mysql5升级到Mysql8,需要用到Mysql8的一些特性来提升系统的性能。 配置用户权限过程中发现一些问题,学习并记录一下。 目录 一、环境 二、MySQL8 用户权限 2.1 账号管理权限 2.1.1 连接数据库 2.1.2 账号权限配置 2.2 密码…

Container容器技术简介

本文介绍了容器技术出现背景,docker技术与容器编排技术的简单说明 背景 在传统项目的生产环境中,迁移一个用户态进程往往非常麻烦,因为一个用户态进程背后会附带这非常多例如函数库、中间件等的依赖项,但又没有像apt和yum一样的…

洗地机哪个牌子好用?洗地机希亦、石头、添可、西屋谁的清洁力更强?

洗地机的出现极大地改善了清洁过程,提高了效率,减少了人力投入。但随着市场上洗地机的种类和功能不断增加,人们可能会感到困惑,不知道如何选择适合自己需求的机器。为了帮助消费者更好地了解洗地机的选择,今天我将带大…