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

这篇具有很好参考价值的文章主要介绍了python socket编程7 - 使用PyQt6 开发UI界面新增实现UDP server和client单机通讯的例子。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

在第五篇中,简单实现了命令行下的 TCP/UDP server和client的单机通讯。
在第六篇中,实现了PyQt6开发界面,TCP协议实现的单机server和client的通讯功能。
这一篇,在第六篇的基础上,增加了UDP server和client的单机通讯功能。

一、界面

pyqt udp收发一体,python socket编程,python,PyQt6,python,udp,socket

二、对比命令行代码的封装示意

1、UDP Server 界面实现服务配置和数据提供

pyqt udp收发一体,python socket编程,python,PyQt6,python,udp,socket

2、UDP Server封装成2个部分:UDPServer和UDPServerSocketReceiveThread

pyqt udp收发一体,python socket编程,python,PyQt6,python,udp,socket文章来源地址https://www.toymoban.com/news/detail-827492.html

3、server 完整代码

import socket

from PyQt6.QtCore import QThread, pyqtSignal


class UDPServer:
    def __init__(self, ui, server_ip, server_hostname, server_port):
        self.clientAddr = None
        self.ui = ui  # 主界面
        self.ip = server_ip  # 服务器ip地址
        self.port = server_port  # 服务器端口号
        self.serverName = server_hostname  # 显示名称
        self.is_running = False  # 是否已经启动

        self.socket = None  # socket
        self.socketThread = None  # 新的 socket receive 线程

        self.start()

    def start(self):
        if not self.is_running:
            self.is_running = True
            self.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 = clientAddr

    def 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 = serverSocket
        self.clientSocket = None
        self.addr = None
        self.is_running = True

    def 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 = False
                break


class 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  # socket
        self.socketThread = None  # 新的 socket receive 线程

        self.start()

    def start(self):
        if not self.is_running:
            self.is_running = True
            self.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 = False
                if 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 = serverSocket
        self.clientSocket = None
        self.addr = None
        self.is_running = True

    def 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')  # 发送客户端断开通知到主界面
                    break
                self.sendClientDataToUi(data)
            except ConnectionResetError as reason:
                self.sendClientDataToUi("已经离开对话。")
                self.is_running = False
                self.emitConnectEvent('disconnect')  # 发送客户端断开通知到主界面
                break
        self.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 = False

    def emitConnectEvent(self, state):
        self.clientConnection.emit(state)

    def sendClientDataToUi(self, message):
        self.receivedClientData.emit(message)

4、client 完整代码

import socket
from PyQt6.QtCore import QThread, pyqtSignal


class UDPClient:

    def __init__(self, ui, ip, clientName, port):
        self.ui = ui
        self.ip = ip
        self.hostName = clientName
        self.port = port

        self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        self.socketThread = None
        self.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 = clientSocket
        self.addr = None
        self.is_running = True

    def 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 = False
                break


class TCPClient:

    def __init__(self, ui, ip, clientName, port):
        self.ui = ui
        self.ip = ip
        self.hostName = clientName
        self.port = port

        self.socket = None
        self.socketThread = None
        self.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 True
        except Exception as reason:
            print(reason)
            return False


class TCPClientSocketReceiveThread(QThread):
    receivedServerData: pyqtSignal = pyqtSignal(str)  # 向主线程发送接受到客户端的数据

    def __init__(self, clientSocket):
        super(TCPClientSocketReceiveThread, self).__init__()
        self.clientSocket = clientSocket
        self.is_running = True

    def stop(self):
        self.is_running = False
        self.clientSocket.close()

    def run(self):
        while self.is_running:
            try:
                msg = self.clientSocket.recv(1024).decode("utf-8")  # 接受服务端消息
                if not msg:
                    break
                self.receivedServerData.emit(msg)
            except Exception as reason:
                print(reason)
                break
        self.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_MainWindow


class 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 = None
        self.tcpClient = None

        self.udpServer = None
        self.udpClient = None

    def 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, QtWidgets


class 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.translate
        MainWindow.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 QApplication

if __name__ == '__main__':
    app = QApplication(sys.argv)
    login = MainWindow()
    sys.exit(app.exec())

到了这里,关于python socket编程7 - 使用PyQt6 开发UI界面新增实现UDP server和client单机通讯的例子的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • PyQt6把QTDesigner生成的UI文件转成python源码,并运行

    锋哥原创的PyQt6视频教程: 2024版 PyQt6 Python桌面开发 视频教程(无废话版) 玩命更新中~_哔哩哔哩_bilibili 2024版 PyQt6 Python桌面开发 视频教程(无废话版) 玩命更新中~共计18条视频,包括:2024版 PyQt6 Python桌面开发 视频教程(无废话版) 玩命更新中~、第2讲 PyQt6库和工具库QTDesigner安装

    2024年04月11日
    浏览(41)
  • Python开发GUI常用库PyQt6和PySide6介绍之三:交互和通信方式讲解

    在PyQt6和PySide6中,事件(Event)和信号(Signal)是两个不同的概念,它们都是Qt框架中用于处理不同类型的应用程序响应机制。简言之,事件是对用户交互或系统状态变化的直接响应,而信号和槽是一种允许对象间通信的机制。在PyQt6和PySide6的应用程序中,合理地使用事件和信

    2024年02月04日
    浏览(40)
  • Python - PyQt6、QDesigner、pyuic5-tool 安装使用

    Python 开发可视化界面可以使用原生的 tkinter ,但是原生框架使用起来颇为不方便,所以最流行的还是 QT UI框架, QT 是使用 C++ 语言开发,Python 想使用需要对其进行封装,所以就出现了 PyQt 框架,这个框架使用极其方便,而且可以多端运行。 本人使用的是 Mac 2023款 Apple M1 芯片

    2024年02月09日
    浏览(39)
  • PyQt6 与 Pyecharts 交互:将鼠标点击 html 页面某点时的提示框信息在 PyQt 界面显示

    版本信息 PyQt6:6.6.1 pyqt6-tools:6.4.2.3.3 PyQt6-WebEngine:6.6.0 pyqt6 官方文档:Reference Guide — PyQt Documentation v6.6.0 在 PyQt 中,信号signal和槽slot是用来处理事件的机制。 信号是一个对象,当它被发射时,会通知所有连接的槽。槽是一个函数,当信号被发射时,它会被调用。 PyQt中常

    2024年04月26日
    浏览(43)
  • 基于PyQt5的UI界面开发——信号与槽

    PyQt5采用了一种被称为“信号与槽”机制的编程模式,用于处理对象间的通信和事件处理。在PyQt5中,信号(signal)是对象发出的特定事件,例如按钮被点击、文本被修改等。而槽(slot)是与信号相关联的特定动作或函数。当信号被触发时,与之相关联的槽会被自动调用。 槽

    2024年02月16日
    浏览(46)
  • 基于PyQt5的UI界面开发——对基本控件的介绍

    在PyQt中,控件是用户界面上的可见元素。控件可以包括按钮、标签、文本框、进度条等。每个控件都有自己的属性和方法,可以通过编程方式进行调整和操作。 以下是一些常用的PyQt控件: QLabel(标签):用于显示文本或图片的静态控件。 QPushButton(按钮):用于触发特定动

    2024年02月16日
    浏览(37)
  • VSCode + PyQt + Python 可视化制作UI界面

    之前都是使用Unity开发一些工具提供给策划使用 之前的做法 我会用python写脚本编译成exe文件 unity中通过命令行调用 很麻烦 索性不如直接用python写工具 Python写工具会简单很多 比如裁切 旋转 缩放 位移图片在Python中只需要几行代码就可以搞定 但是在Unity中要写很多代码 就很蛋

    2024年02月02日
    浏览(43)
  • 【Python】pyqt5入门教程之第一个UI界面

    1.pyqt5工具安装 (1)使用pip工具安装PyQt5工具: (2)安装Qt Designer图形界面开发工具: 安装完成后所在路径 (3)安装QtDesigner 安装完成后所在路径 designer.exe路径 启动QtDesigner 2.第一个QT窗口程序 Pycharm配置 找到py文件,右键External Tool — QTDesigner—就可以启动 QTDesigner .ui文件转换

    2024年02月15日
    浏览(46)
  • pyqt6 + pycharm 搭建+使用入门

            最近要写代码给小白使用,但是让对方装python环境还要运行添加各种库,对小白太不友好了,所以选择了使用pyqt6+pycharm配合生成可视化python程序,在打包成exe执行。         首先安装PyQt6和PyQt6-tools。使用如下命令:        但是运行后会报如下错误:          这

    2024年02月10日
    浏览(44)
  • PyQt6 QMessageBox的使用方法

    放置一个QPushButton就可以。 点击按钮显示QMessageBox

    2024年02月11日
    浏览(38)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包