from PyQt5.QtWidgets import QApplication, QMainWindow, QTableWidgetItem, QHeaderView
from http_client import Ui_MainWindow
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.set_http_method()
self.set_http_head()
self.add_signals()
def set_http_method(self):
self.ui.http_method.addItems(['GET', 'POST', 'OPTION', 'HEAD'])
def set_http_head(self):
self.ui.http_head.setColumnCount(2)
self.ui.http_head.setRowCount(1)
self.ui.http_head.setHorizontalHeaderLabels(['名称', '值'])
self.ui.http_head.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
# newItem = QTableWidgetItem('张三') # 添加单元格元素
# self.ui.http_head.setItem(0, 0, newItem)
def add_signals(self):
self.ui.add_head.clicked.connect(self.add_http_head)
self.ui.delete_head.clicked.connect(self.delete_http_head)
self.ui.send_data.clicked.connect(self.send_http_request)
def add_http_head(self):
cur_row = self.ui.http_head.rowCount()
self.ui.http_head.setRowCount(cur_row+1)
def delete_http_head(self):
cur_row = self.ui.http_head.rowCount()
self.ui.http_head.setRowCount(cur_row-1)
def send_http_request(self):
method = self.ui.http_method.currentText()
url = self.ui.http_url.text()
for row in range(self.ui.http_head.rowCount()):
print(self.ui.http_head.item(row, 0).text(), self.ui.http_head.item(row, 1).text())
app = QApplication([])
mainw = MainWindow()
mainw.show()
app.exec_()
http_client 模块
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'http_client.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(634, 573)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout_3 = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout_3.setObjectName("verticalLayout_3")
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.http_method = QtWidgets.QComboBox(self.centralwidget)
self.http_method.setObjectName("http_method")
self.horizontalLayout.addWidget(self.http_method)
self.http_url = QtWidgets.QLineEdit(self.centralwidget)
self.http_url.setObjectName("http_url")
self.horizontalLayout.addWidget(self.http_url)
self.send_data = QtWidgets.QPushButton(self.centralwidget)
self.send_data.setObjectName("send_data")
self.horizontalLayout.addWidget(self.send_data)
self.verticalLayout_3.addLayout(self.horizontalLayout)
self.horizontalLayout_3 = QtWidgets.QHBoxLayout()
self.horizontalLayout_3.setObjectName("horizontalLayout_3")
self.verticalLayout = QtWidgets.QVBoxLayout()
self.verticalLayout.setObjectName("verticalLayout")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setObjectName("label")
self.horizontalLayout_2.addWidget(self.label)
self.add_head = QtWidgets.QPushButton(self.centralwidget)
self.add_head.setObjectName("add_head")
self.horizontalLayout_2.addWidget(self.add_head)
self.delete_head = QtWidgets.QPushButton(self.centralwidget)
self.delete_head.setObjectName("delete_head")
self.horizontalLayout_2.addWidget(self.delete_head)
self.verticalLayout.addLayout(self.horizontalLayout_2)
self.http_head = QtWidgets.QTableWidget(self.centralwidget)
self.http_head.setObjectName("http_head")
self.http_head.setColumnCount(0)
self.http_head.setRowCount(0)
self.verticalLayout.addWidget(self.http_head)
self.horizontalLayout_3.addLayout(self.verticalLayout)
self.line = QtWidgets.QFrame(self.centralwidget)
self.line.setFrameShape(QtWidgets.QFrame.VLine)
self.line.setFrameShadow(QtWidgets.QFrame.Sunken)
self.line.setObjectName("line")
self.horizontalLayout_3.addWidget(self.line)
self.verticalLayout_2 = QtWidgets.QVBoxLayout()
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.verticalLayout_4 = QtWidgets.QVBoxLayout()
self.verticalLayout_4.setContentsMargins(-1, 6, -1, 6)
self.verticalLayout_4.setObjectName("verticalLayout_4")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setObjectName("label_2")
self.verticalLayout_4.addWidget(self.label_2)
self.verticalLayout_2.addLayout(self.verticalLayout_4)
self.http_body = QtWidgets.QTableWidget(self.centralwidget)
self.http_body.setObjectName("http_body")
self.http_body.setColumnCount(0)
self.http_body.setRowCount(0)
self.verticalLayout_2.addWidget(self.http_body)
self.horizontalLayout_3.addLayout(self.verticalLayout_2)
self.verticalLayout_3.addLayout(self.horizontalLayout_3)
self.http_rsp = QtWidgets.QTextEdit(self.centralwidget)
self.http_rsp.setObjectName("http_rsp")
self.verticalLayout_3.addWidget(self.http_rsp)
self.horizontalLayout_4 = QtWidgets.QHBoxLayout()
self.horizontalLayout_4.setObjectName("horizontalLayout_4")
self.clear_http_rsp = QtWidgets.QPushButton(self.centralwidget)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.clear_http_rsp.sizePolicy().hasHeightForWidth())
self.clear_http_rsp.setSizePolicy(sizePolicy)
self.clear_http_rsp.setObjectName("clear_http_rsp")
self.horizontalLayout_4.addWidget(self.clear_http_rsp)
self.verticalLayout_3.addLayout(self.horizontalLayout_4)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 634, 22))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.send_data.setText(_translate("MainWindow", "发送"))
self.label.setText(_translate("MainWindow", "消息头"))
self.add_head.setText(_translate("MainWindow", "+"))
self.delete_head.setText(_translate("MainWindow", "-"))
self.label_2.setText(_translate("MainWindow", "消息体"))
self.clear_http_rsp.setText(_translate("MainWindow", "清除"))