重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
PyQt5+requests实现一个车票查询工具,供大家参考,具体内容如下
网站建设哪家好,找成都创新互联!专注于网页设计、网站建设、微信开发、小程序开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了江宁免费建站欢迎大家使用!结构图
效果图
思路
1、search(QPushButton)点击信号(clicked)连接到自定义的槽函数(event.search)
2、槽函数(event.search)接收四个参数:QTableWidget对象的引用、两个QLabel中的内容(站台名称)、QDateEdit格式化日期
3、槽函数(event.search)调用爬虫类(TrainService)获取车次信息,添加到QTableWidget对象中。
代码
1、ui.py(ui界面)
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'E:\Python\Qt\ui_0.ui' # # Created by: PyQt5 UI code generator 5.11.2 # # WARNING! All changes made in this file will be lost! from PyQt5 import QtCore, QtGui, QtWidgets from Event import EventClass class Ui_MainWindow(object): def setupUi(self, MainWindow): event=EventClass() MainWindow.setObjectName("MainWindow") MainWindow.resize(793, 604) MainWindow.setStyleSheet("font: 10pt \"Microsoft YaHei UI\";") self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.widget = QtWidgets.QWidget(self.centralwidget) self.widget.setGeometry(QtCore.QRect(0, 0, 791, 43)) self.widget.setObjectName("widget") self.horizontalLayout = QtWidgets.QHBoxLayout(self.widget) self.horizontalLayout.setSizeConstraint(QtWidgets.QLayout.SetNoConstraint) self.horizontalLayout.setContentsMargins(10, 1, 1, 3) self.horizontalLayout.setSpacing(15) self.horizontalLayout.setObjectName("horizontalLayout") self.label = QtWidgets.QLabel(self.widget) self.label.setObjectName("label") self.horizontalLayout.addWidget(self.label) self.lineEdit = QtWidgets.QLineEdit(self.widget) self.lineEdit.setMaximumSize(QtCore.QSize(742, 16777215)) self.lineEdit.setObjectName("lineEdit") self.horizontalLayout.addWidget(self.lineEdit) self.label_2 = QtWidgets.QLabel(self.widget) self.label_2.setObjectName("label_2") self.horizontalLayout.addWidget(self.label_2) self.lineEdit_2 = QtWidgets.QLineEdit(self.widget) self.lineEdit_2.setObjectName("lineEdit_2") self.horizontalLayout.addWidget(self.lineEdit_2) self.dateEdit = QtWidgets.QDateEdit(self.widget) self.dateEdit.setDate(QtCore.QDate(2019, 1, 1)) self.dateEdit.setObjectName("dateEdit") self.horizontalLayout.addWidget(self.dateEdit) self.pushButton = QtWidgets.QPushButton(self.widget) self.pushButton.setObjectName("pushButton") self.horizontalLayout.addWidget(self.pushButton) self.scrollArea = QtWidgets.QScrollArea(self.centralwidget) self.scrollArea.setGeometry(QtCore.QRect(0, 40, 791, 561)) self.scrollArea.setWidgetResizable(True) self.scrollArea.setObjectName("scrollArea") self.scrollAreaWidgetContents = QtWidgets.QWidget() self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 789, 559)) self.scrollAreaWidgetContents.setObjectName("scrollAreaWidgetContents") self.tableWidget = QtWidgets.QTableWidget(self.scrollAreaWidgetContents) self.tableWidget.setGeometry(QtCore.QRect(0, 0, 791, 561)) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.tableWidget.sizePolicy().hasHeightForWidth()) self.tableWidget.setSizePolicy(sizePolicy) self.tableWidget.setBaseSize(QtCore.QSize(0, 0)) self.tableWidget.setObjectName("tableWidget") self.tableWidget.setColumnCount(5) self.tableWidget.setRowCount(0) item = QtWidgets.QTableWidgetItem() self.tableWidget.setHorizontalHeaderItem(0, item) item = QtWidgets.QTableWidgetItem() self.tableWidget.setHorizontalHeaderItem(1, item) item = QtWidgets.QTableWidgetItem() self.tableWidget.setHorizontalHeaderItem(2, item) item = QtWidgets.QTableWidgetItem() self.tableWidget.setHorizontalHeaderItem(3, item) item = QtWidgets.QTableWidgetItem() self.tableWidget.setHorizontalHeaderItem(4, item) self.tableWidget.horizontalHeader().setDefaultSectionSize(155) self.tableWidget.verticalHeader().setDefaultSectionSize(47) self.tableWidget.verticalHeader().setMinimumSectionSize(45) self.line = QtWidgets.QFrame(self.scrollAreaWidgetContents) self.line.setGeometry(QtCore.QRect(0, 23, 784, 31)) self.line.setToolTipDuration(0) self.line.setLineWidth(1) self.line.setFrameShape(QtWidgets.QFrame.HLine) self.line.setFrameShadow(QtWidgets.QFrame.Sunken) self.line.setObjectName("line") self.scrollArea.setWidget(self.scrollAreaWidgetContents) MainWindow.setCentralWidget(self.centralwidget) self.retranslateUi(MainWindow) self.pushButton.clicked.connect(lambda :event.search(self.tableWidget, self.lineEdit.text(), self.lineEdit_2.text(), self.dateEdit.date().toPyDate())) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow")) self.label.setText(_translate("MainWindow", "From")) self.label_2.setText(_translate("MainWindow", "To")) self.pushButton.setText(_translate("MainWindow", "search")) item = self.tableWidget.horizontalHeaderItem(0) item.setText(_translate("MainWindow", "车次")) item = self.tableWidget.horizontalHeaderItem(1) item.setText(_translate("MainWindow", "出发时间")) item = self.tableWidget.horizontalHeaderItem(2) item.setText(_translate("MainWindow", "到站时间")) item = self.tableWidget.horizontalHeaderItem(3) item.setText(_translate("MainWindow", "硬卧")) item = self.tableWidget.horizontalHeaderItem(4) item.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_())