[Python] 纯文本查看 复制代码
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QMainWindow, QApplication, QTabWidget, QHeaderView, QMessageBox, QTableWidgetItem
import requests
from urllib.parse import quote
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(849, 666)
Form.setFixedSize(849, 666)
self.lineEdit = QtWidgets.QLineEdit(Form)
self.lineEdit.setGeometry(QtCore.QRect(280, 30, 121, 31))
self.lineEdit.setObjectName("lineEdit")
self.lineEdit_2 = QtWidgets.QLineEdit(Form)
self.lineEdit_2.setGeometry(QtCore.QRect(470, 30, 121, 31))
self.lineEdit_2.setObjectName("lineEdit_2")
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(390, 80, 81, 31))
self.pushButton.setObjectName("pushButton")
self.tableWidget = QtWidgets.QTableWidget(Form)
self.tableWidget.setGeometry(QtCore.QRect(0, 130, 851, 541))
self.tableWidget.setObjectName("tableWidget")
self.tableWidget.setColumnCount(9)
self.tableWidget.setRowCount(16)
self.tableWidget.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
self.tableWidget.setHorizontalHeaderLabels(['出发城市', '到达城市', '出发车站', '到达车站', '出发时间', '车型', '到达时间', '价格', '剩余票数'])
self.pushButton.clicked.connect(self.AskFor)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "查询车票"))
Form.setWindowIcon(QIcon('./aa.ico'))#这里放图标文件
self.lineEdit.setPlaceholderText(_translate("Form", "出发城市"))
self.lineEdit_2.setPlaceholderText(_translate("Form", "到达城市"))
self.pushButton.setText(_translate("Form", "查询"))
def AskFor(self):
self.tableWidget.clearContents()
SetOut = quote(self.lineEdit.text()) # 出发站
ArriveStation = quote(self.lineEdit_2.text()) # 终点站
req = requests.get(
f'https://api.jisuapi.com/bus/city2c?appkey=这里需要自己去申请api申请网站&start={SetOut}&end={ArriveStation}').json()#申请的网站https://www.jisuapi.com/ 有点做广告的嫌疑了= =
print(req)
if req["status"] == '203':
QMessageBox.information(w, '提示', '暂无此站点信息')
else:
result = req["result"] # 获取结果 返回的是一个列表 每一个切片都是一个字典
num = 0
for i in result:
newItem = QTableWidgetItem(i["startcity"]) # 出发城市
newItem2 = QTableWidgetItem(i["endcity"]) # 到达城市
newItem3 = QTableWidgetItem(i["startstation"]) # 出发车站
newItem4 = QTableWidgetItem(i["endstation"]) # 到达车站
newItem5 = QTableWidgetItem(i["starttime"]) # 出发时间
newItem6 = QTableWidgetItem(i["bustype"]) # 车型
newItem7 = QTableWidgetItem(i["distance"]) # 到达时间
newItem8 = QTableWidgetItem(f'{i["price"]}元') # 价格
newItem9 = QTableWidgetItem(i["ticket"]) # 剩余数量
self.tableWidget.setItem(num, 0, newItem)
self.tableWidget.setItem(num, 1, newItem2)
self.tableWidget.setItem(num, 2, newItem3)
self.tableWidget.setItem(num, 3, newItem4)
self.tableWidget.setItem(num, 4, newItem5)
self.tableWidget.setItem(num, 5, newItem6)
self.tableWidget.setItem(num, 6, newItem7)
self.tableWidget.setItem(num, 7, newItem8)
self.tableWidget.setItem(num, 8, newItem9)
num = num + 1
if __name__ == '__main__':
app = QApplication(sys.argv)
w = QMainWindow()
UI = Ui_Form()
UI.setupUi(w)
w.show()
app.exec_()