-
Notifications
You must be signed in to change notification settings - Fork 117
/
tcp_udp_web_ui.py
246 lines (222 loc) · 9.7 KB
/
tcp_udp_web_ui.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QDialog, QHBoxLayout, QVBoxLayout
import sys
class ToolsUi(QDialog):
# 信号槽机制:设置一个信号,用于触发接收区写入动作
signal_write_msg = QtCore.pyqtSignal(str)
def __init__(self, num):
"""
初始化窗口
:param num: 计数窗口
"""
super(ToolsUi, self).__init__()
self.num = num
self._translate = QtCore.QCoreApplication.translate
self.setObjectName("TCP-UDP")
self.resize(640, 480)
self.setAcceptDrops(False)
self.setSizeGripEnabled(False)
# 定义控件
self.pushButton_get_ip = QtWidgets.QPushButton()
self.pushButton_link = QtWidgets.QPushButton()
self.pushButton_unlink = QtWidgets.QPushButton()
self.pushButton_clear = QtWidgets.QPushButton()
self.pushButton_exit = QtWidgets.QPushButton()
self.pushButton_send = QtWidgets.QPushButton()
self.pushButton_dir = QtWidgets.QPushButton()
self.pushButton_else = QtWidgets.QPushButton()
self.label_port = QtWidgets.QLabel()
self.label_ip = QtWidgets.QLabel()
self.label_rev = QtWidgets.QLabel()
self.label_send = QtWidgets.QLabel()
self.label_sendto = QtWidgets.QLabel()
self.label_dir = QtWidgets.QLabel()
self.label_written = QtWidgets.QLabel()
self.lineEdit_port = QtWidgets.QLineEdit()
self.lineEdit_ip_send = QtWidgets.QLineEdit()
self.lineEdit_ip_local = QtWidgets.QLineEdit()
self.textEdit_send = QtWidgets.QTextEdit()
self.textBrowser_recv = QtWidgets.QTextBrowser()
self.comboBox_tcp = QtWidgets.QComboBox()
# 定义布局
self.h_box_1 = QHBoxLayout()
self.h_box_2 = QHBoxLayout()
self.h_box_3 = QHBoxLayout()
self.h_box_4 = QHBoxLayout()
self.h_box_recv = QHBoxLayout()
self.h_box_exit = QHBoxLayout()
self.h_box_all = QHBoxLayout()
self.v_box_set = QVBoxLayout()
self.v_box_send = QVBoxLayout()
self.v_box_web = QVBoxLayout()
self.v_box_exit = QVBoxLayout()
self.v_box_right = QVBoxLayout()
self.v_box_left = QVBoxLayout()
# 向选择功能的下拉菜单添加选项
self.comboBox_tcp.addItem("")
self.comboBox_tcp.addItem("")
self.comboBox_tcp.addItem("")
self.comboBox_tcp.addItem("")
self.comboBox_tcp.addItem("")
# 设置字体
font = QtGui.QFont()
font.setFamily("Yuppy TC")
font.setPointSize(20)
font.setBold(True)
font.setItalic(False)
font.setUnderline(True)
font.setWeight(75)
font.setStrikeOut(False)
self.label_rev.setFont(font)
self.label_send.setFont(font)
# 设置控件的初始属性
self.label_dir.hide()
self.label_sendto.hide()
self.pushButton_dir.hide()
self.lineEdit_ip_send.hide()
self.label_dir.setWordWrap(True) # 让label自动换行
self.pushButton_unlink.setEnabled(False)
self.textBrowser_recv.insertPlainText("这是窗口-%s\n" % self.num)
# 调用布局方法和控件显示文字的方法
self.layout_ui()
self.ui_translate()
self.connect()
def layout_ui(self):
"""
设置控件的布局
:return:
"""
# 左侧布局添加
self.h_box_1.addWidget(self.label_ip)
self.h_box_1.addWidget(self.lineEdit_ip_local)
self.h_box_1.addWidget(self.pushButton_get_ip)
self.h_box_2.addWidget(self.label_port)
self.h_box_2.addWidget(self.lineEdit_port)
self.h_box_2.addWidget(self.pushButton_else)
self.h_box_3.addWidget(self.label_sendto)
self.h_box_3.addWidget(self.lineEdit_ip_send)
self.h_box_4.addWidget(self.comboBox_tcp)
self.h_box_4.addWidget(self.pushButton_link)
self.h_box_4.addWidget(self.pushButton_unlink)
self.v_box_set.addLayout(self.h_box_1)
self.v_box_set.addLayout(self.h_box_2)
self.v_box_set.addLayout(self.h_box_3)
self.v_box_set.addLayout(self.h_box_4)
self.v_box_web.addWidget(self.label_dir)
self.v_box_web.addWidget(self.pushButton_dir)
self.v_box_send.addWidget(self.label_send)
self.v_box_send.addWidget(self.textEdit_send)
self.v_box_send.addLayout(self.v_box_web)
self.v_box_exit.addWidget(self.pushButton_send)
self.v_box_exit.addWidget(self.pushButton_exit)
self.h_box_exit.addWidget(self.label_written)
self.h_box_exit.addLayout(self.v_box_exit)
self.v_box_left.addLayout(self.v_box_set)
self.v_box_left.addLayout(self.v_box_send)
self.v_box_left.addLayout(self.h_box_exit)
# 右侧布局添加
self.h_box_recv.addWidget(self.label_rev)
self.h_box_recv.addWidget(self.pushButton_clear)
self.v_box_right.addLayout(self.h_box_recv)
self.v_box_right.addWidget(self.textBrowser_recv)
# 将左右布局添加到窗体布局
self.h_box_all.addLayout(self.v_box_left)
self.h_box_all.addLayout(self.v_box_right)
# 设置窗体布局到窗体
self.setLayout(self.h_box_all)
def ui_translate(self):
"""
控件默认显示文字的设置
:param : QDialog类创建的对象
:return: None
"""
# 设置各个控件显示的文字
# 也可使用控件的setText()方法设置文字
self.setWindowTitle(self._translate("TCP-UDP", "TCP/UDP网络测试工具-窗口%s" % self.num))
self.comboBox_tcp.setItemText(0, self._translate("TCP-UDP", "TCP服务端"))
self.comboBox_tcp.setItemText(1, self._translate("TCP-UDP", "TCP客户端"))
self.comboBox_tcp.setItemText(2, self._translate("TCP-UDP", "UDP服务端"))
self.comboBox_tcp.setItemText(3, self._translate("TCP-UDP", "UDP客户端"))
self.comboBox_tcp.setItemText(4, self._translate("TCP-UDP", "WEB服务端"))
self.pushButton_link.setText(self._translate("TCP-UDP", "连接网络"))
self.pushButton_unlink.setText(self._translate("TCP-UDP", "断开网络"))
self.pushButton_get_ip.setText(self._translate("TCP-UDP", "重新获取IP"))
self.pushButton_clear.setText(self._translate("TCP-UDP", "清除消息"))
self.pushButton_send.setText(self._translate("TCP-UDP", "发送"))
self.pushButton_exit.setText(self._translate("TCP-UDP", "退出系统"))
self.pushButton_dir.setText(self._translate("TCP-UDP", "选择路径"))
self.pushButton_else.setText(self._translate("TCP-UDP", "窗口多开another"))
self.label_ip.setText(self._translate("TCP-UDP", "本机IP:"))
self.label_port.setText(self._translate("TCP-UDP", "端口号:"))
self.label_sendto.setText(self._translate("TCP-UDP", "目标IP:"))
self.label_rev.setText(self._translate("TCP-UDP", "接收区域"))
self.label_send.setText(self._translate("TCP-UDP", "发送区域"))
self.label_dir.setText(self._translate("TCP-UDP", "请选择index.html所在的文件夹"))
self.label_written.setText(self._translate("TCP-UDP", "Written by Wangler2333"))
def connect(self):
"""
控件信号-槽的设置
:param : QDialog类创建的对象
:return: None
"""
self.signal_write_msg.connect(self.write_msg)
self.comboBox_tcp.currentIndexChanged.connect(self.combobox_change)
def combobox_change(self):
# 此函数用于选择不同功能时界面会作出相应变化
"""
combobox控件内容改变时触发的槽
:return: None
"""
self.close_all()
if self.comboBox_tcp.currentIndex() == 0 or self.comboBox_tcp.currentIndex() == 2:
self.label_sendto.hide()
self.label_dir.hide()
self.pushButton_dir.hide()
self.pushButton_send.show()
self.lineEdit_ip_send.hide()
self.textEdit_send.show()
self.label_port.setText(self._translate("TCP-UDP", "端口号:"))
if self.comboBox_tcp.currentIndex() == 1 or self.comboBox_tcp.currentIndex() == 3:
self.label_sendto.show()
self.label_dir.hide()
self.pushButton_dir.hide()
self.pushButton_send.show()
self.lineEdit_ip_send.show()
self.textEdit_send.show()
self.label_port.setText(self._translate("TCP-UDP", "目标端口:"))
if self.comboBox_tcp.currentIndex() == 4:
self.label_sendto.hide()
self.label_dir.show()
self.pushButton_dir.show()
self.pushButton_send.hide()
self.lineEdit_ip_send.hide()
self.textEdit_send.hide()
self.label_port.setText(self._translate("TCP-UDP", "端口号:"))
def write_msg(self, msg):
# signal_write_msg信号会触发这个函数
"""
功能函数,向接收区写入数据的方法
信号-槽触发
tip:PyQt程序的子线程中,直接向主线程的界面传输字符是不符合安全原则的
:return: None
"""
self.textBrowser_recv.insertPlainText(msg)
# 滚动条移动到结尾
self.textBrowser_recv.moveCursor(QtGui.QTextCursor.End)
def closeEvent(self, event):
"""
重写closeEvent方法,实现dialog窗体关闭时执行一些代码
:param event: close()触发的事件
:return: None
"""
self.close_all()
def close_all(self):
pass
if __name__ == '__main__':
"""
显示界面
"""
app = QApplication(sys.argv)
ui = ToolsUi(1)
ui.show()
sys.exit(app.exec_())