1、首先通过QT designer实现如下界面
2、 通过命令转python代码
命令:pyuic5 -x -o http_req.py httpreq.ui
(http_req.py:是通过http_req.ui生成的python代码文件, http_req.ui,:通过QT designer设计的界面代码)
http_req.py代码如下:
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'http_req.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(960, 749) self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") #请求头 # self.hea = ['token','content-type'] # self.listWidget = QtWidgets.QListWidget(self.centralwidget) # self.listWidget.setGeometry(QtCore.QRect(10, 140, 931, 121)) # self.listWidget.addItems(self.hea) # self.listWidget.setObjectName("listWidget") self.label_4 = QtWidgets.QLabel(self.centralwidget) self.label_4.setGeometry(QtCore.QRect(40, 150, 54, 12)) self.label_4.setObjectName("label_4") # self.horizontalLayout_3.addWidget(self.label_4) self.token = QtWidgets.QLineEdit(self.centralwidget) self.token.setObjectName("token") self.token.setGeometry(QtCore.QRect(150, 150, 781, 21)) # self.horizontalLayout_3.addWidget(self.token) self.label_5 = QtWidgets.QLabel(self.centralwidget) self.label_5.setGeometry(QtCore.QRect(30, 190, 71, 16)) self.label_5.setObjectName("label_5") # self.horizontalLayout_3.addWidget(self.label_5) self.content_type = QtWidgets.QLineEdit(self.centralwidget) self.content_type.setObjectName("content_type",) self.content_type.setGeometry(QtCore.QRect(150, 190, 781, 21)) # self.horizontalLayout_3.addWidget(self.content_type) #标签1 self.label = QtWidgets.QLabel(self.centralwidget) self.label.setGeometry(QtCore.QRect(20, 120, 54, 12)) self.label.setObjectName("label") #标签2 self.label_2 = QtWidgets.QLabel(self.centralwidget) self.label_2.setGeometry(QtCore.QRect(20, 280, 54, 12)) self.label_2.setObjectName("label_2") #标签3 self.label_3 = QtWidgets.QLabel(self.centralwidget) self.label_3.setGeometry(QtCore.QRect(490, 280, 54, 12)) self.label_3.setObjectName("label_3") self.widget = QtWidgets.QWidget(self.centralwidget) self.widget.setGeometry(QtCore.QRect(30, 0, 861, 81)) self.widget.setObjectName("widget") #布局 self.horizontalLayout = QtWidgets.QHBoxLayout(self.widget) self.horizontalLayout.setContentsMargins(0, 0, 0, 0) self.horizontalLayout.setObjectName("horizontalLayout") #请求方式 self.comboBox = QtWidgets.QComboBox(self.widget) self.comboBox.setObjectName("comboBox") self.horizontalLayout.addWidget(self.comboBox) self.comboBox.addItems(['get','post']) self.comboBox.setCurrentIndex(0) #请求URl self.url = QtWidgets.QLineEdit(self.widget) self.url.setObjectName("url") self.horizontalLayout.addWidget(self.url) #执行 self.pushButton = QtWidgets.QPushButton(self.widget) self.pushButton.setAutoRepeatInterval(100) self.pushButton.setObjectName("pushButton") self.horizontalLayout.addWidget(self.pushButton) #清除 self.pushButton_2 = QtWidgets.QPushButton(self.widget) self.pushButton_2.setObjectName("pushButton_2") self.horizontalLayout.addWidget(self.pushButton_2) self.widget1 = QtWidgets.QWidget(self.centralwidget) self.widget1.setGeometry(QtCore.QRect(10, 300, 931, 401)) self.widget1.setObjectName("widget1") self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.widget1) self.horizontalLayout_2.setContentsMargins(0, 0, 0, 0) self.horizontalLayout_2.setObjectName("horizontalLayout_2") #请求体 self.textEdit = QtWidgets.QTextEdit(self.widget1) self.textEdit.setObjectName("textEdit") self.horizontalLayout_2.addWidget(self.textEdit) #响应体 self.textEdit_2 = QtWidgets.QTextEdit(self.widget1) self.textEdit_2.setObjectName("textEdit_2") self.textEdit_2.setReadOnly(True) self.horizontalLayout_2.addWidget(self.textEdit_2) MainWindow.setCentralWidget(self.centralwidget) self.menubar = QtWidgets.QMenuBar(MainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 960, 23)) 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.label.setText(_translate("MainWindow", "请求头")) self.label_2.setText(_translate("MainWindow", "请求体")) self.label_3.setText(_translate("MainWindow", "响应体")) self.label_4.setText(_translate("MainWindow", "token")) self.label_5.setText(_translate("MainWindow", "content_type")) self.pushButton.setText(_translate("MainWindow", "执行")) self.pushButton_2.setText(_translate("MainWindow", "清除")) if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) MainWindow = QtWidgets.QMainWindow() ui = Ui_MainWindow() ui.setupUi(MainWindow) MainWindow.show() sys.exit(app.exec_())
3、创建一个脚本,用于解耦ui中代码
http_main。py,代码如下:
import json import requests import urllib3 from PyQt5 import QtCore, QtWidgets from PyQt5.QtWidgets import * import sys import http_req class Myclass(object): def __init__(self): app = QtWidgets.QApplication(sys.argv) MainWindow = QtWidgets.QMainWindow() self.ui = http_req.Ui_MainWindow() self.ui.setupUi(MainWindow) #绑定pushButton_2_clicked清除方法 self.ui.pushButton_2.clicked.connect(self.pushButton_2_clicked) #绑定请求方法 self.ui.pushButton.clicked.connect(self.get_req) MainWindow.show() sys.exit(app.exec_()) def get_req(self): #获取url req_url = self.ui.url.text() #获取token req_token = self.ui.token.text() #获取content_type req_content_type = self.ui.content_type.text() header = {"token":req_token,"content-type":req_content_type} #获取data req_data = self.ui.textEdit.toPlainText() #去掉\ # req_data = req_data.replace("\\","") # req_datas=json.dumps(req_data) #获取请求方式 req = self.ui.comboBox.currentText() print(req) if req == 'get': response = requests.request(url=req_url,method="get",headers=header,data=req_data,verify=False) else: response = requests.request(url=req_url,method="post",data=req_data,headers=header,verify=False) #将返回值打印到textEdit_2中 self.ui.textEdit_2.setPlainText(str(response.text)) def pushButton_2_clicked(self): url = self.ui.url.clear() print(url) if __name__=="__main__": Myclass()