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.rhkb.cn/news/210548.html

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

相关文章

四川云汇优想:抖音直播等级怎么升级?

抖音直播,作为当前最热门的社交平台之一,其等级体系一直备受用户关注。如何在抖音直播中迅速提升等级,成为众多用户探讨的话题。在这篇文章中,我们将深入探讨抖音直播等级的升级机制、好处以及一些实用的技巧,助你在抖…

三、C语言常见概念

目录 1. C语言是什么? 3. 编译器的选择 3.1 编译和链接 3.2 编译器的对比 6. main函数 7. printf 和 库函数 8. 关键字介绍 8.1 什么是预编译? 8.2 static 的关键词作用? 8.3 const 的作用 8.4 voliate 的作用 8.5 typedef 的作用…

吉他初学者学习网站搭建系列(5)——如何做一个在线节拍器

文章目录 背景实现TransportLoop代码 在线尝试 背景 我们看吉他谱时,经常看到拍号,例如6/8。它的含义是一拍是一个八分音符,一小节有六拍。四分音符的时长是一秒,即60拍/分钟。基于这样的背景知识,我们就可以根据一些…

基于springboot的校园二手市场

博主主页:猫头鹰源码 博主简介:Java领域优质创作者、CSDN博客专家、公司架构师、全网粉丝5万、专注Java技术领域和毕业设计项目实战 主要内容:毕业设计(Javaweb项目|小程序等)、简历模板、学习资料、面试题库、技术咨询 文末联系获取 项目介绍…

vFW搭建IRF

正文共:2328字 40图,预估阅读时间:5 分钟 IRF(Intelligent Resilient Framework,智能弹性架构)技术通过将多台设备连接在一起,虚拟化成一台设备,集成多台设备的硬件资源和软件处理能…

【网络安全】-常见的网站攻击方式详解

文章目录 介绍1. SQL 注入攻击攻击原理攻击目的防范措施 2. 跨站脚本攻击(XSS)攻击原理攻击目的防范措施 3. CSRF 攻击攻击原理攻击目的防范措施 4. 文件上传漏洞攻击原理攻击目的防范措施 5. 点击劫持攻击原理攻击目的防范措施 结论 介绍 在数字时代&a…

2023年【T电梯修理】考试总结及T电梯修理证考试

题库来源:安全生产模拟考试一点通公众号小程序 T电梯修理考试总结考前必练!安全生产模拟考试一点通每个月更新T电梯修理证考试题目及答案!多做几遍,其实通过T电梯修理证考试很简单。 1、【多选题】《特种设备安全法》规定&#x…

安美数字酒店宽带运营系统 SQL注入漏洞复现

0x01 产品简介 HiBOS酒店宽带运营系统隶属于安美世纪(北京)科技有限公司开发的一套酒店宽带管理系统。 0x02 漏洞概述 安美数字酒店宽带运营系统 online_status.php、language.php等接口处存在SQL注入漏洞,未经身份认证的攻击者可以通过此漏洞获取数据库权限,进一步利用可…

自定义 el-select 和 el-input 样式

文章目录 需求分析el-select 样式el-input 样式el-table 样式 需求 自定义 选择框的下拉框的样式和输入框 分析 el-select 样式 .select_box{// 默认placeholder:deep .el-input__inner::placeholder {font-size: 14px;font-weight: 500;color: #3E534F;}// 默认框状态样式更…

mysql中删除数据后,新增数据时id会跳跃,主键自增id不连续

引言: 在使用MySQL数据库时,有时候我们需要删除某些记录,但是删除记录后可能会导致表中的id不再连续排序。 如何实现删除记录后让id重新排序的功能。 如图: 删除数据后,中间的id不会自动连续。 下面有两种方法进行重…

【matlab程序】matlab画螺旋图|旋转图

%% 数学之美====》螺旋线 % 海洋与大气科学 % 20231205 clear;clc;close all; n=10; t=0:0.01:2pin; R=1; xx=nan(length(t),1);yy=nan(length(t),1); for i=1:length(t) xx(i)=Rcos(t(i)); yy(i)=Rsin(t(i)); R=R+1; end figure set(gcf,‘position’,[50 50 1200 1200],‘col…

java电子班牌人脸识别管理平台源码,SaaS云平台端、智慧校园管理平台端、家长/教师微信移动端、智慧班牌学生端

什么是电子班牌? 电子班牌用来显示班级信息,班级活动信息以及学校的通知信息。信息内容为文字、图片、视频等,为生和老师提供新颖的师生交流及校园服务平台。融合了多媒体信息发布、家校互通、物联控制、教务管理、日常办公等一系列应用&…

知识管理平台Confluence:win10安装confluence

文章目录 介绍主要功能 安装教程安装java运行平台JRE安装数据库Postgresql在Postgresql创建confluence使用的数据库创建数据库用户创建数据库 安装confluence注册confluence启动confluence 参考链接 介绍 Confluence 是由澳大利亚软件公司 Atlassian 开发的企业协作平台。它提…

外包干了2个月,技术倒退2年。。。。。

先说一下自己的情况,本科生,20年通过校招进入深圳某软件公司,干了接近4年的功能测试,今年国庆,感觉自己不能够在这样下去了,长时间呆在一个舒适的环境会让一个人堕落!而我已经在一个企业干了四年的功能测试…

处理k8s中创建ingress失败

创建ingress: 如果在创建过程中出错了: 处理方法就是: kubectl get ValidatingWebhookConfiguration kubectl delete -A ValidatingWebhookConfiguration ingress-nginx-admission 然后再次创建,发现可以:

计算机视觉之手势、面部、姿势捕捉以Python Mediapipe为工具

计算机视觉之手势、面部、姿势捕捉以 Python Mediapipe为工具 文章目录 1.Mediapipe库概述2.手势捕捉(hands)3.面部捕捉(face)4.姿势捕捉(pose) 1.Mediapipe库概述 Mediapipe是一个开源且强大的Python库,由Google开发和维护。它提供了丰富的工具和功能&#xff0c…

使用K-means把人群分类

1.前言 K-mean 是无监督的聚类算法 算法分类: 2.实现步骤 1.数据加工:把数据转为全数字(比如性别男女,转换为0 和 1) 2.模型训练 fit 3.预测 3.代码 原数据类似这样(source:http:img-blog.csdnimg.cn…

Chat-GPT原理

Chat-GPT原理核心:基于Transformer 架构 ​ 以下是参考文献的部分截图原文说明: ​ Transformers are based on the “attention mechanism,” which allows the model to pay more attention to some inputs than others, regardless of where they show up in t…

vite初识

Vite是伴随着Vue3正式版一起发布的,最开始Vite 1.0的版本是为Vue3服务的,并不是跨框架的。之后半年时间左右,出现了Vite 2.0版本,Vite 2.0真正脱离了和Vue3的强关联,以插件的方式,可以集成到目前流行的主流…

技巧-GPU显存和利用率如何提高和batch_size/num_works等参数的实验测试

目录 简介实验测试显存占用问题GPU占用率波动问题num_work不是越大越好 总结 本专栏为深度学习的一些技巧,方法和实验测试,偏向于实际应用,后续不断更新,感兴趣童鞋可关,方便后续推送 简介 在PyTorch中使用多个GPU进行模型训练时,各个参数和指标之间存在一定的关系…