1 Star 0 Fork 1

PassionateSleeper/pyqt5socket实现CS通信

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
window.py 7.98 KB
一键复制 编辑 原始数据 按行查看 历史
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file '1.ui'
#
# Created by: PyQt5 UI code generator 5.6
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
from server import Tcp, Udp
import threading
from server import get_host_ip
class Ui_mainWindow(QtWidgets.QMainWindow):
def __init__(self):
super(Ui_mainWindow,self).__init__()
self.setupUi(self)
self.retranslateUi(self)
def setupUi(self, mainWindow):
mainWindow.setObjectName("mainWindow")
mainWindow.resize(765, 600)
mainWindow.setAutoFillBackground(False)
mainWindow.setAnimated(False)
self.centralwidget = QtWidgets.QWidget(mainWindow)
self.centralwidget.setObjectName("centralwidget")
# self.tcp = Tcp()
self.send = QtWidgets.QTextBrowser(self.centralwidget)
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.rec = QtWidgets.QTextBrowser(self.centralwidget)
self.label = QtWidgets.QLabel(self.centralwidget)
self.tcp_udp = QtWidgets.QPushButton(self.centralwidget)
self.startBtn = QtWidgets.QPushButton(self.centralwidget)
self.ip = QtWidgets.QLabel(self.centralwidget)
self.state = QtWidgets.QLabel(self.centralwidget)
# self.button = QtWidgets.QPushButton(self.centralwidget)
# self.button.setGeometry(QtCore.QRect(210, 50, 94, 32))
# self.button.setObjectName("button")
# self.button.clicked.connect(self.btnclick)
#
# self.button2 = QtWidgets.QPushButton(self.centralwidget)
# self.button2.setGeometry(QtCore.QRect(300, 50, 94, 32))
# self.button2.setObjectName("button2")
# self.button2.clicked.connect(self.end)
self.tcp_udp.setGeometry(QtCore.QRect(210, 40, 94, 32))
self.tcp_udp.setObjectName("button")
self.tcp_udp.setCheckable(True)
self.startBtn.setCheckable(True)
self.protocol = 1 # 设定1为tcp 0为udp
self.tcp = Tcp()
self.udp = Udp()
self.startBtn.setGeometry(QtCore.QRect(310, 40, 94, 32))
self.tcp_udp.clicked[bool].connect(self.btnclick)
self.startBtn.clicked[bool].connect(self.btnclick)
self.ip.setGeometry(QtCore.QRect(230, 80, 120, 20))
self.state.setGeometry(QtCore.QRect(500, 60, 200, 20))
self.state.setObjectName("state")
self.label.setGeometry(QtCore.QRect(350, 130, 91, 31))
font = QtGui.QFont()
font.setPointSize(16)
self.label.setFont(font)
self.label.setObjectName("label")
self.rec.setGeometry(QtCore.QRect(180, 180, 421, 61))
self.rec.setObjectName("rec")
self.label_2.setGeometry(QtCore.QRect(350, 260, 101, 31))
font = QtGui.QFont()
font.setPointSize(16)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.send.setGeometry(QtCore.QRect(180, 310, 421, 151))
self.send.setObjectName("send")
mainWindow.setCentralWidget(self.centralwidget)
self.tcp.state_signal.connect(self.setState)
self.tcp.recv_signal.connect(self.setRec)
self.tcp.send_signal.connect(self.setSend)
self.udp.state_signal.connect(self.setState)
self.udp.recv_signal.connect(self.setRec)
self.udp.send_signal.connect(self.setSend)
self.retranslateUi(mainWindow)
QtCore.QMetaObject.connectSlotsByName(mainWindow)
def retranslateUi(self, mainWindow):
_translate = QtCore.QCoreApplication.translate
mainWindow.setWindowTitle(_translate("mainWindow", "服务器"))
self.tcp_udp.setText(_translate("mainWindow", "当前为tcp协议"))
self.startBtn.setText(_translate("mainWindow", "开始"))
self.state.setText(_translate("mainWindow", "未运行"))
self.rec.setText(_translate("mainWindow", "接收数据"))
self.send.setText(_translate("mainWindow", "发送数据"))
self.label.setText(_translate("mainWindow", "接收信息"))
self.label_2.setText(_translate("mainWindow", "发送信息"))
self.ip.setText(_translate("mainWindow", "{}: {}".format(get_host_ip(), self.tcp.port)))
def setState(self, s):
self.state.setText(s)
def setRec(self, s):
self.rec.setText(s)
def setSend(self, s):
self.send.setText(s)
# def btnclick(self, press):
# # if press:
# # self.button.setText("结束")
# # self.state.setText("已运行")
# # self.rec.setText("准备接收")
# # self.send.setText("等待发送")
# # try:
# # self.th = threading.Thread(target=self.serverThread)
# # self.th.start()
# # print(self.th.getName())
# # except e:
# # print(str(e))
# # pass
# # else:
# # self.button.setText("开始")
# # self.state.setText("终止")
# # print(self.th.getName()) #不做处理线程仍然存在
# # try:
# #
# # except:
# # pass
#
# # def serverThread(self):
# # while True:
# # try:
# # print("进入连接")
# # conn, addr = self.tcp.s.accept()
# # print("已连接,", addr)
# # conn.setblocking(False)
# #
# # self.state.setText("已连接{}:{}".format(addr[0], addr[1]))
# #
# # client_data = conn.recv(1024).decode('utf-8')
# # print("tcp clientdata", client_data)
# # self.rec.setText(client_data)
# # # 这里默认是tcp协议
# #
# # # 发送数据
# # print("发送数据")
# # msg = self.tcp.getmsg()
# # # self.rec.setText(msg)
# # conn.sendall(msg.encode('utf-8'))
# #
# # conn.close()
# # except e:
# # print(str(e))
# # pass
# if press:
# self.button.setText("结束")
# self.state.setText("服务器已运行")
# self.tcp.start()
# else:
# self.button.setText("开始")
# self.state.setText("断开连接")
# print(self.tcp.currentThread())
# self.tcp.terminate()
# print(self.tcp.currentThread())
def btnclick(self, press):
sender = self.sender()
if sender == self.tcp_udp:
if press:
self.tcp.terminate()
self.tcp_udp.setText("当前为udp协议")
self.startBtn.setText("已停止")
self.protocol = 0
else:
self.udp.terminate()
self.tcp_udp.setText("当前为tcp协议")
self.startBtn.setText("已停止")
self.protocol = 1
elif sender == self.startBtn:
if press:
self.startBtn.setText("结束")
if self.protocol == 1: # tcp
print("tcp开始")
self.state.setText("服务器正在运行tcp协议")
self.ip.setText("{}:{}".format(self.tcp.ip_port[0], self.tcp.ip_port[1]))
self.tcp.start()
else:
print("udp开始")
self.state.setText("服务器正在运行udp协议")
self.ip.setText("{}:{}".format(self.udp.ip_port[0], self.udp.ip_port[1]))
self.udp.start()
else:
self.startBtn.setText("开始")
if self.protocol == 1:
print("tcp结束")
pass
# self.tcp.terminate()
else:
print("udp结束")
pass
# self.udp.terminate()
self.state.setText("断开连接")
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/PassionateSleeper/pyqt5_socket_CS.git
git@gitee.com:PassionateSleeper/pyqt5_socket_CS.git
PassionateSleeper
pyqt5_socket_CS
pyqt5socket实现CS通信
master

搜索帮助