赞
踩
一个简易的机票预订系统,填写信息、预定机票、获得取票码、取票/更改机票,还拥有机票查询系统。
以下是完成后的文件目录
首先创建数据库,可以用代码,也可以用可视化工具(Navicat等等),这里给出代码
import pymysql conn = pymysql.connect(host='localhost', user='root', passwd='123456', db='bjx', charset='utf8') cur = conn.cursor() cur.execute("""CREATE TABLE IF NOT EXISTS informationmessage( real_name varchar(10) PRIMARY KEY, sex varchar(2), work_unit varchar(20), id varchar(20), travel_time varchar(11), start_place varchar(10), end_place varchar(10), class varchar(5), advance varchar(5), total_money varchar(5), code varchar(7))""") conn.commit() cur.execute("""CREATE TABLE IF NOT EXISTS flight( flight_number varchar(10) PRIMARY KEY, start_place varchar(10), end_place varchar(10), travel_time varchar(11), first_class varchar(4), business_class varchar(4), economy_class varchar(4), count_passenger varchar(4))""") conn.commit()
完成后可以用可视化工具查看
接下来编写界面
界面的图片可自行寻找,放在目录中的picture里,设置在没有预定机票成功之前,预定金交付按钮无法点击,以下是代码
from PyQt5 import QtCore, QtGui, QtWidgets class Ui_MainWindow1(object): def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(908, 474) self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.pushButton = QtWidgets.QPushButton(self.centralwidget) self.pushButton.setGeometry(QtCore.QRect(720, 60, 121, 41)) self.pushButton.setObjectName("pushButton") self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget) self.pushButton_2.setGeometry(QtCore.QRect(720, 170, 121, 41)) self.pushButton_2.setObjectName("pushButton_2") self.label = QtWidgets.QLabel(self.centralwidget) self.label.setGeometry(QtCore.QRect(0, 0, 701, 441)) self.label.setObjectName("label") MainWindow.setCentralWidget(self.centralwidget) self.menubar = QtWidgets.QMenuBar(MainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 908, 26)) 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.pushButton.setText(_translate("MainWindow", "预定机票")) self.pushButton_2.setText(_translate("MainWindow", "预订金交付")) self.label.setText(_translate("MainWindow", "picture_1"))
输入时间时必须与数据表中航班的时间相同,始发地、目的地也是如此,以下是代码
from PyQt5 import QtCore, QtGui, QtWidgets class Ui_Form1(object): def setupUi(self, Form): Form.setObjectName("Form") Form.resize(665, 381) self.pushButton = QtWidgets.QPushButton(Form) self.pushButton.setGeometry(QtCore.QRect(540, 310, 91, 41)) self.pushButton.setObjectName("pushButton") self.lineEdit = QtWidgets.QLineEdit(Form) self.lineEdit.setGeometry(QtCore.QRect(100, 30, 131, 31)) self.lineEdit.setObjectName("lineEdit") self.lineEdit_2 = QtWidgets.QLineEdit(Form) self.lineEdit_2.setGeometry(QtCore.QRect(100, 100, 51, 31)) self.lineEdit_2.setObjectName("lineEdit_2") self.lineEdit_3 = QtWidgets.QLineEdit(Form) self.lineEdit_3.setGeometry(QtCore.QRect(100, 160, 191, 31)) self.lineEdit_3.setObjectName("lineEdit_3") self.lineEdit_4 = QtWidgets.QLineEdit(Form) self.lineEdit_4.setGeometry(QtCore.QRect(100, 230, 161, 31)) self.lineEdit_4.setText("") self.lineEdit_4.setObjectName("lineEdit_4") self.lineEdit_5 = QtWidgets.QLineEdit(Form) self.lineEdit_5.setGeometry(QtCore.QRect(430, 30, 41, 31)) self.lineEdit_5.setText("") self.lineEdit_5.setObjectName("lineEdit_5") self.lineEdit_6 = QtWidgets.QLineEdit(Form) self.lineEdit_6.setGeometry(QtCore.QRect(510, 30, 31, 31)) self.lineEdit_6.setText("") self.lineEdit_6.setObjectName("lineEdit_6") self.lineEdit_7 = QtWidgets.QLineEdit(Form) self.lineEdit_7.setGeometry(QtCore.QRect(580, 30, 31, 31)) self.lineEdit_7.setText("") self.lineEdit_7.setObjectName("lineEdit_7") self.lineEdit_8 = QtWidgets.QLineEdit(Form) self.lineEdit_8.setGeometry(QtCore.QRect(430, 100, 121, 31)) self.lineEdit_8.setObjectName("lineEdit_8") self.lineEdit_9 = QtWidgets.QLineEdit(Form) self.lineEdit_9.setGeometry(QtCore.QRect(430, 160, 121, 31)) self.lineEdit_9.setObjectName("lineEdit_9") self.comboBox = QtWidgets.QComboBox(Form) self.comboBox.setGeometry(QtCore.QRect(430, 230, 101, 31)) self.comboBox.setObjectName("comboBox") self.comboBox.addItem("") self.comboBox.addItem("") self.comboBox.addItem("") self.label = QtWidgets.QLabel(Form) self.label.setGeometry(QtCore.QRect(20, 30, 41, 31)) self.label.setObjectName("label") self.label_2 = QtWidgets.QLabel(Form) self.label_2.setGeometry(QtCore.QRect(20, 100, 41, 31)) self.label_2.setObjectName("label_2") self.label_3 = QtWidgets.QLabel(Form) self.label_3.setGeometry(QtCore.QRect(20, 160, 71, 31)) self.label_3.setObjectName("label_3") self.label_4 = QtWidgets.QLabel(Form) self.label_4.setGeometry(QtCore.QRect(20, 230, 51, 31)) self.label_4.setObjectName("label_4") self.label_5 = QtWidgets.QLabel(Form) self.label_5.setGeometry(QtCore.QRect(370, 30, 31, 31)) self.label_5.setObjectName("label_5") self.label_6 = QtWidgets.QLabel(Form) self.label_6.setGeometry(QtCore.QRect(480, 30, 21, 31)) self.label_6.setObjectName("label_6") self.label_7 = QtWidgets.QLabel(Form) self.label_7.setGeometry(QtCore.QRect(550, 35, 21, 21)) self.label_7.setObjectName("label_7") self.label_8 = QtWidgets.QLabel(Form) self.label_8.setGeometry(QtCore.QRect(620, 35, 21, 21)) self.label_8.setObjectName("label_8") self.label_9 = QtWidgets.QLabel(Form) self.label_9.setGeometry(QtCore.QRect(370, 100, 51, 31)) self.label_9.setObjectName("label_9") self.label_10 = QtWidgets.QLabel(Form) self.label_10.setGeometry(QtCore.QRect(370, 160, 51, 31)) self.label_10.setObjectName("label_10") self.label_11 = QtWidgets.QLabel(Form) self.label_11.setGeometry(QtCore.QRect(370, 230, 31, 31)) self.label_11.setObjectName("label_11") self.retranslateUi(Form) QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate Form.setWindowTitle(_translate("Form", "Form")) self.pushButton.setText(_translate("Form", "预定")) self.comboBox.setItemText(0, _translate("Form", "头等舱")) self.comboBox.setItemText(1, _translate("Form", "商务舱")) self.comboBox.setItemText(2, _translate("Form", "经济舱")) self.label.setText(_translate("Form", "姓名")) self.label_2.setText(_translate("Form", "性别")) self.label_3.setText(_translate("Form", "工作单位")) self.label_4.setText(_translate("Form", "身份证")) self.label_5.setText(_translate("Form", "时间")) self.label_6.setText(_translate("Form", "年")) self.label_7.setText(_translate("Form", "月")) self.label_8.setText(_translate("Form", "日")) self.label_9.setText(_translate("Form", "始发地")) self.label_10.setText(_translate("Form", "目的地")) self.label_11.setText(_translate("Form", "舱位"))
点击预订,成功后就会出现提示,其代码在后续系统代码中给出
此时主界面的订金交付按钮可用,点击支付后,数据表中的advance属性更改为已交,以下是界面代码
from PyQt5 import QtCore, QtGui, QtWidgets class Ui_Form2(object): def setupUi(self, Form): Form.setObjectName("Form") Form.resize(242, 164) self.label = QtWidgets.QLabel(Form) self.label.setGeometry(QtCore.QRect(30, 40, 81, 21)) self.label.setObjectName("label") self.pushButton = QtWidgets.QPushButton(Form) self.pushButton.setGeometry(QtCore.QRect(120, 100, 71, 31)) self.pushButton.setObjectName("pushButton") self.label_2 = QtWidgets.QLabel(Form) self.label_2.setGeometry(QtCore.QRect(110, 40, 51, 21)) self.label_2.setObjectName("label_2") self.label_3 = QtWidgets.QLabel(Form) self.label_3.setGeometry(QtCore.QRect(170, 40, 21, 21)) self.label_3.setObjectName("label_3") self.retranslateUi(Form) QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate Form.setWindowTitle(_translate("Form", "Form")) self.label.setText(_translate("Form", "应交订金:")) self.pushButton.setText(_translate("Form", "支付")) self.label_2.setText(_translate("Form", "50")) self.label_3.setText(_translate("Form", "元"))
定金交付后会给出取票通知和账单界面,取票码为随机6位数,并存入数据表以便之后取票时的匹配,取票时间设定为航班的前一天,以下给出界面代码
from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtWidgets import * class Ui_Form3(object): def setupUi(self, Form): Form.setObjectName("Form") Form.resize(296, 492) self.label = QtWidgets.QLabel(Form) self.label.setGeometry(QtCore.QRect(20, 20, 51, 21)) self.label.setObjectName("label") self.label_2 = QtWidgets.QLabel(Form) self.label_2.setGeometry(QtCore.QRect(90, 20, 91, 21)) self.label_2.setObjectName("label_2") self.label_3 = QtWidgets.QLabel(Form) self.label_3.setGeometry(QtCore.QRect(20, 70, 41, 21)) self.label_3.setObjectName("label_3") self.label_4 = QtWidgets.QLabel(Form) self.label_4.setGeometry(QtCore.QRect(90, 70, 51, 21)) self.label_4.setObjectName("label_4") self.label_5 = QtWidgets.QLabel(Form) self.label_5.setGeometry(QtCore.QRect(20, 120, 71, 21)) self.label_5.setObjectName("label_5") self.label_6 = QtWidgets.QLabel(Form) self.label_6.setGeometry(QtCore.QRect(90, 120, 151, 21)) self.label_6.setObjectName("label_6") self.label_7 = QtWidgets.QLabel(Form) self.label_7.setGeometry(QtCore.QRect(20, 170, 61, 21)) self.label_7.setObjectName("label_7") self.label_8 = QtWidgets.QLabel(Form) self.label_8.setGeometry(QtCore.QRect(90, 170, 91, 21)) self.label_8.setObjectName("label_8") self.label_9 = QtWidgets.QLabel(Form) self.label_9.setGeometry(QtCore.QRect(20, 220, 61, 21)) self.label_9.setObjectName("label_9") self.label_10 = QtWidgets.QLabel(Form) self.label_10.setGeometry(QtCore.QRect(90, 220, 91, 21)) self.label_10.setObjectName("label_10") self.label_11 = QtWidgets.QLabel(Form) self.label_11.setGeometry(QtCore.QRect(20, 280, 71, 21)) self.label_11.setObjectName("label_11") self.label_12 = QtWidgets.QLabel(Form) self.label_12.setGeometry(QtCore.QRect(100, 280, 91, 21)) self.label_12.setObjectName("label_12") self.label_13 = QtWidgets.QLabel(Form) self.label_13.setGeometry(QtCore.QRect(20, 330, 41, 21)) self.label_13.setObjectName("label_13") self.label_14 = QtWidgets.QLabel(Form) self.label_14.setGeometry(QtCore.QRect(90, 330, 91, 21)) self.label_14.setObjectName("label_14") self.label_15 = QtWidgets.QLabel(Form) self.label_15.setGeometry(QtCore.QRect(20, 390, 71, 21)) self.label_15.setObjectName("label_15") self.label_16 = QtWidgets.QLabel(Form) self.label_16.setGeometry(QtCore.QRect(100, 390, 151, 21)) self.label_16.setObjectName("label_16") self.label_17 = QtWidgets.QLabel(Form) self.label_17.setGeometry(QtCore.QRect(20, 450, 61, 21)) self.label_17.setObjectName("label_17") self.label_18 = QtWidgets.QLabel(Form) self.label_18.setGeometry(QtCore.QRect(90, 450, 91, 16)) self.label_18.setObjectName("label_18") self.retranslateUi(Form) QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate Form.setWindowTitle(_translate("Form", "Form")) self.label.setText(_translate("Form", "姓名:")) self.label_2.setText(_translate("Form", "name")) self.label_3.setText(_translate("Form", "性别:")) self.label_4.setText(_translate("Form", "gender")) self.label_5.setText(_translate("Form", "身份证:")) self.label_6.setText(_translate("Form", "id")) self.label_7.setText(_translate("Form", "始发地:")) self.label_8.setText(_translate("Form", "startplace")) self.label_9.setText(_translate("Form", "目的地:")) self.label_10.setText(_translate("Form", "endplace")) self.label_11.setText(_translate("Form", "舱位要求:")) self.label_12.setText(_translate("Form", "roomlevel")) self.label_13.setText(_translate("Form", "金额:")) self.label_14.setText(_translate("Form", "aftermoney")) self.label_15.setText(_translate("Form", "取票时间:")) self.label_16.setText(_translate("Form", "time")) self.label_17.setText(_translate("Form", "取票码:")) self.label_18.setText(_translate("Form", "code")) class Inform_Bill(QWidget): def __init__(self): QWidget.__init__(self) self.informui_bill = Ui_Form3() self.informui_bill.setupUi(self)
与预订机票界面类似,以下给出界面代码
from PyQt5 import QtCore, QtGui, QtWidgets class Ui_MainWindow2(object): def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(1086, 464) self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.pushButton = QtWidgets.QPushButton(self.centralwidget) self.pushButton.setGeometry(QtCore.QRect(840, 20, 121, 41)) self.pushButton.setObjectName("pushButton") self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget) self.pushButton_2.setGeometry(QtCore.QRect(840, 130, 121, 41)) self.pushButton_2.setObjectName("pushButton_2") self.label = QtWidgets.QLabel(self.centralwidget) self.label.setGeometry(QtCore.QRect(0, 0, 821, 411)) self.label.setObjectName("label") MainWindow.setCentralWidget(self.centralwidget) self.menubar = QtWidgets.QMenuBar(MainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 1086, 26)) 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.pushButton.setText(_translate("MainWindow", "取机票")) self.pushButton_2.setText(_translate("MainWindow", "更改航班")) self.label.setText(_translate("MainWindow", "picture_2"))
输入取票码,在数据表中进行匹配,以下给出界面代码
from PyQt5 import QtCore, QtGui, QtWidgets class Ui_Form4(object): def setupUi(self, Form): Form.setObjectName("Form") Form.resize(410, 244) self.label = QtWidgets.QLabel(Form) self.label.setGeometry(QtCore.QRect(40, 70, 111, 31)) self.label.setObjectName("label") self.lineEdit = QtWidgets.QLineEdit(Form) self.lineEdit.setGeometry(QtCore.QRect(160, 70, 181, 31)) self.lineEdit.setObjectName("lineEdit") self.pushButton = QtWidgets.QPushButton(Form) self.pushButton.setGeometry(QtCore.QRect(250, 160, 91, 41)) self.pushButton.setObjectName("pushButton") self.retranslateUi(Form) QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate Form.setWindowTitle(_translate("Form", "Form")) self.label.setText(_translate("Form", "请输入取票码:")) self.pushButton.setText(_translate("Form", "取票"))
取票码匹配成功时,显示机票界面,以下给出界面代码
from PyQt5 import QtCore, QtGui, QtWidgets class Ui_Form6(object): def setupUi(self, Form): Form.setObjectName("Form") Form.resize(296, 492) self.label = QtWidgets.QLabel(Form) self.label.setGeometry(QtCore.QRect(20, 20, 51, 21)) self.label.setObjectName("label") self.label_2 = QtWidgets.QLabel(Form) self.label_2.setGeometry(QtCore.QRect(90, 20, 91, 21)) self.label_2.setObjectName("label_2") self.label_3 = QtWidgets.QLabel(Form) self.label_3.setGeometry(QtCore.QRect(20, 70, 41, 21)) self.label_3.setObjectName("label_3") self.label_4 = QtWidgets.QLabel(Form) self.label_4.setGeometry(QtCore.QRect(90, 70, 51, 21)) self.label_4.setObjectName("label_4") self.label_5 = QtWidgets.QLabel(Form) self.label_5.setGeometry(QtCore.QRect(20, 120, 71, 21)) self.label_5.setObjectName("label_5") self.label_6 = QtWidgets.QLabel(Form) self.label_6.setGeometry(QtCore.QRect(90, 120, 151, 21)) self.label_6.setObjectName("label_6") self.label_7 = QtWidgets.QLabel(Form) self.label_7.setGeometry(QtCore.QRect(20, 170, 61, 21)) self.label_7.setObjectName("label_7") self.label_8 = QtWidgets.QLabel(Form) self.label_8.setGeometry(QtCore.QRect(90, 170, 91, 21)) self.label_8.setObjectName("label_8") self.label_9 = QtWidgets.QLabel(Form) self.label_9.setGeometry(QtCore.QRect(20, 220, 61, 21)) self.label_9.setObjectName("label_9") self.label_10 = QtWidgets.QLabel(Form) self.label_10.setGeometry(QtCore.QRect(90, 220, 91, 21)) self.label_10.setObjectName("label_10") self.label_11 = QtWidgets.QLabel(Form) self.label_11.setGeometry(QtCore.QRect(20, 280, 71, 21)) self.label_11.setObjectName("label_11") self.label_12 = QtWidgets.QLabel(Form) self.label_12.setGeometry(QtCore.QRect(100, 280, 91, 21)) self.label_12.setObjectName("label_12") self.label_13 = QtWidgets.QLabel(Form) self.label_13.setGeometry(QtCore.QRect(20, 330, 41, 21)) self.label_13.setObjectName("label_13") self.label_14 = QtWidgets.QLabel(Form) self.label_14.setGeometry(QtCore.QRect(90, 330, 91, 21)) self.label_14.setObjectName("label_14") self.label_15 = QtWidgets.QLabel(Form) self.label_15.setGeometry(QtCore.QRect(20, 390, 71, 21)) self.retranslateUi(Form) QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate Form.setWindowTitle(_translate("Form", "Form")) self.label.setText(_translate("Form", "姓名:")) self.label_2.setText(_translate("Form", "name")) self.label_3.setText(_translate("Form", "性别:")) self.label_4.setText(_translate("Form", "gender")) self.label_5.setText(_translate("Form", "身份证:")) self.label_6.setText(_translate("Form", "id")) self.label_7.setText(_translate("Form", "始发地:")) self.label_8.setText(_translate("Form", "startplace")) self.label_9.setText(_translate("Form", "目的地:")) self.label_10.setText(_translate("Form", "endplace")) self.label_11.setText(_translate("Form", "舱位要求:")) self.label_12.setText(_translate("Form", "roomlevel")) self.label_13.setText(_translate("Form", "金额:")) self.label_14.setText(_translate("Form", "totalmoney"))
如果当前时间不是取票时间,则会提示
与订票时输入的信息类似,以下给出界面代码
from PyQt5 import QtCore, QtGui, QtWidgets class Ui_Form8(object): def setupUi(self, Form): Form.setObjectName("Form") Form.resize(442, 346) self.label = QtWidgets.QLabel(Form) self.label.setGeometry(QtCore.QRect(40, 40, 41, 31)) self.label.setObjectName("label") self.label_2 = QtWidgets.QLabel(Form) self.label_2.setGeometry(QtCore.QRect(150, 40, 21, 31)) self.label_2.setObjectName("label_2") self.label_3 = QtWidgets.QLabel(Form) self.label_3.setGeometry(QtCore.QRect(220, 40, 21, 21)) self.label_3.setObjectName("label_3") self.label_4 = QtWidgets.QLabel(Form) self.label_4.setGeometry(QtCore.QRect(300, 40, 21, 21)) self.label_4.setObjectName("label_4") self.label_5 = QtWidgets.QLabel(Form) self.label_5.setGeometry(QtCore.QRect(30, 110, 61, 31)) self.label_5.setObjectName("label_5") self.label_6 = QtWidgets.QLabel(Form) self.label_6.setGeometry(QtCore.QRect(30, 180, 61, 31)) self.label_6.setObjectName("label_6") self.label_7 = QtWidgets.QLabel(Form) self.label_7.setGeometry(QtCore.QRect(10, 240, 71, 31)) self.label_7.setObjectName("label_7") self.lineEdit = QtWidgets.QLineEdit(Form) self.lineEdit.setGeometry(QtCore.QRect(100, 40, 41, 31)) self.lineEdit.setObjectName("lineEdit") self.lineEdit_2 = QtWidgets.QLineEdit(Form) self.lineEdit_2.setGeometry(QtCore.QRect(180, 40, 31, 31)) self.lineEdit_2.setText("") self.lineEdit_2.setObjectName("lineEdit_2") self.lineEdit_3 = QtWidgets.QLineEdit(Form) self.lineEdit_3.setGeometry(QtCore.QRect(260, 40, 31, 31)) self.lineEdit_3.setText("") self.lineEdit_3.setObjectName("lineEdit_3") self.lineEdit_4 = QtWidgets.QLineEdit(Form) self.lineEdit_4.setGeometry(QtCore.QRect(100, 110, 151, 31)) self.lineEdit_4.setObjectName("lineEdit_4") self.lineEdit_5 = QtWidgets.QLineEdit(Form) self.lineEdit_5.setGeometry(QtCore.QRect(100, 180, 151, 31)) self.lineEdit_5.setObjectName("lineEdit_5") self.pushButton = QtWidgets.QPushButton(Form) self.pushButton.setGeometry(QtCore.QRect(320, 290, 91, 41)) self.pushButton.setObjectName("pushButton") self.comboBox = QtWidgets.QComboBox(Form) self.comboBox.setGeometry(QtCore.QRect(100, 240, 101, 31)) self.comboBox.setObjectName("comboBox") self.comboBox.addItem("") self.comboBox.addItem("") self.comboBox.addItem("") self.retranslateUi(Form) QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate Form.setWindowTitle(_translate("Form", "Form")) self.label.setText(_translate("Form", "时间:")) self.label_2.setText(_translate("Form", "年")) self.label_3.setText(_translate("Form", "月")) self.label_4.setText(_translate("Form", "日")) self.label_5.setText(_translate("Form", "始发地:")) self.label_6.setText(_translate("Form", "目的地:")) self.label_7.setText(_translate("Form", "舱位要求:")) self.pushButton.setText(_translate("Form", "预定")) self.comboBox.setItemText(0, _translate("Form", "头等舱")) self.comboBox.setItemText(1, _translate("Form", "商务舱")) self.comboBox.setItemText(2, _translate("Form", "经济舱"))
最后一切都准备完成后就进入交款界面,以下给出界面代码
from PyQt5 import QtCore, QtGui, QtWidgets class Ui_Form5(object): def setupUi(self, Form): Form.setObjectName("Form") Form.resize(242, 164) self.label = QtWidgets.QLabel(Form) self.label.setGeometry(QtCore.QRect(30, 40, 81, 21)) self.label.setObjectName("label") self.pushButton = QtWidgets.QPushButton(Form) self.pushButton.setGeometry(QtCore.QRect(120, 100, 71, 31)) self.pushButton.setObjectName("pushButton") self.label_2 = QtWidgets.QLabel(Form) self.label_2.setGeometry(QtCore.QRect(110, 40, 51, 21)) self.label_2.setObjectName("label_2") self.label_3 = QtWidgets.QLabel(Form) self.label_3.setGeometry(QtCore.QRect(170, 40, 21, 21)) self.label_3.setObjectName("label_3") self.retranslateUi(Form) QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate Form.setWindowTitle(_translate("Form", "Form")) self.label.setText(_translate("Form", "应交款:")) self.pushButton.setText(_translate("Form", "支付")) self.label_2.setText(_translate("Form", "money")) self.label_3.setText(_translate("Form", "元"))
以下系统代码,代码注释较清楚,这里就不多解释了,直接上代码
from 预订机票主界面 import * from 输入旅客信息界面 import * from 预订金支付界面 import * from 取票通知和账单界面 import * from 创建数据表 import * import sys import random import pandas as pd from PyQt5.QtWidgets import * from PyQt5.QtGui import QPixmap class BOOK_Planeticket(QMainWindow): def __init__(self): QMainWindow.__init__(self) self.mainui_agency = Ui_MainWindow1() self.mainui_agency.setupUi(self) # 给按钮绑定方法 btn_book = self.mainui_agency.pushButton btn_book.clicked.connect(travelinfor.show) self.btn_paybook = self.mainui_agency.pushButton_2 self.btn_paybook.clicked.connect(advance.show) # 设置按钮不可用 self.btn_paybook.setEnabled(False) # 主界面显示 def book(self): # 主界面图片 self.mainui_agency.label.setPixmap(QPixmap('./picture/picture1.jpg')) # 完全填充 self.mainui_agency.label.setScaledContents(True) mainbook.show() class Traveller_Information(QWidget): def __init__(self): QWidget.__init__(self) self.inforui_travel = Ui_Form1() self.inforui_travel.setupUi(self) # 给按钮绑定方法 btn_inforbook = self.inforui_travel.pushButton btn_inforbook.clicked.connect(self.save_information) def save_information(self): # 获取信息值 self.name = self.inforui_travel.lineEdit.text() self.sex = self.inforui_travel.lineEdit_2.text() self.work_unit = self.inforui_travel.lineEdit_3.text() self.id = self.inforui_travel.lineEdit_4.text() self.year = self.inforui_travel.lineEdit_5.text() self.month = self.inforui_travel.lineEdit_6.text() self.day = self.inforui_travel.lineEdit_7.text() self.start_place = self.inforui_travel.lineEdit_8.text() self.end_place = self.inforui_travel.lineEdit_9.text() self.cabin = self.inforui_travel.comboBox.currentText() # 匹配航班 self.match_information() def match_information(self): self.time = self.year + '-' + self.month + '-' + self.day sql = "select * from flight where travel_time='%s' and start_place='%s' and end_place='%s'" % (self.time, self.start_place, self.end_place) cur.execute(sql) if cur.execute(sql): QMessageBox.information(self, '恭喜您', '已成功安排航班,请交定金!', QMessageBox.Yes) travelinfor.hide() mainbook.btn_paybook.setEnabled(True) else: QMessageBox.information(self, '很遗憾', '没有合适的航班!', QMessageBox.Yes) class Pay_Advance(QWidget): def __init__(self): QWidget.__init__(self) self.payui_advance = Ui_Form2() self.payui_advance.setupUi(self) # 给按钮绑定方法 btn_payadvance = self.payui_advance.pushButton btn_payadvance.clicked.connect(self.pay_advance) # 定义取票码 self.six_code = '' # 定义乘载人数 self.count_passenger = 0 def pay_advance(self): # 获取匹配航班的舱位价格 sql = "select first_class, business_class, economy_class from flight where travel_time='%s' and start_place='%s' and end_place='%s'"\ % (travelinfor.time, travelinfor.start_place, travelinfor.end_place) cur.execute(sql) # 生成价格framedata表 df = pd.read_sql(sql, conn) # 获取framedata单个元素 first_price = df.iloc[0, 0] business_price = df.iloc[0, 1] economy_price = df.iloc[0, 2] button = QMessageBox.information(self, '恭喜您', '已成功预定航班!', QMessageBox.Yes) self.count_passenger += 1 # 生成六位随机数的取票码 for i in range(6): self.six_code += chr(random.randrange(ord('0'), ord('9') + 1)) # 将乘客信息放进数据表 sql1 = "insert into informationmessage(real_name, sex, work_unit, id, travel_time, start_place, end_place, class, advance, total_money, code)" \ " values(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)" cur.execute(sql1, (travelinfor.name, travelinfor.sex, travelinfor.work_unit, travelinfor.id, travelinfor.time, travelinfor.start_place, travelinfor.end_place, travelinfor.cabin, '已交', '未交', self.six_code)) # 更新乘载人数 sql2 = "update flight set count_passenger='%s' where travel_time='%s' and start_place='%s' and end_place='%s'" % \ (str(self.count_passenger), travelinfor.time, travelinfor.start_place, travelinfor.end_place) cur.execute(sql2) conn.commit() if button == QMessageBox.Yes: # 取票界面信息显示 inbill.informui_bill.label_2.setText(travelinfor.name) inbill.informui_bill.label_4.setText(travelinfor.sex) inbill.informui_bill.label_6.setText(travelinfor.id) inbill.informui_bill.label_8.setText(travelinfor.start_place) inbill.informui_bill.label_10.setText(travelinfor.end_place) inbill.informui_bill.label_12.setText(travelinfor.cabin) if travelinfor.cabin == '头等舱': price = first_price elif travelinfor.cabin == '商务舱': price = business_price elif travelinfor.cabin == '经济舱': price = economy_price afterprice = str(int(price) - 50) # 价格显示 inbill.informui_bill.label_14.setText(afterprice) # 显示前一天时间 inbill.informui_bill.label_16.setText(travelinfor.year + '-' + travelinfor.month + '-' + str(int(travelinfor.day) - 1)) inbill.informui_bill.label_18.setText(self.six_code) advance.hide() inbill.show() if __name__ == "__main__": app = QApplication(sys.argv) inbill = Inform_Bill() advance = Pay_Advance() travelinfor = Traveller_Information() mainbook = BOOK_Planeticket() mainbook.book() sys.exit(app.exec_())
from 旅客操作主界面 import * from 取票界面 import * from 交款界面 import * from 机票界面 import * from 更改航班界面 import * from 创建数据表 import * from 取票通知和账单界面 import * import sys import pandas as pd import random import time from PyQt5.QtWidgets import * from PyQt5.QtGui import QPixmap class Passenger_Operate(QMainWindow): def __init__(self): QMainWindow.__init__(self) self.mainui_passenger = Ui_MainWindow2() self.mainui_passenger.setupUi(self) # 按钮绑定方法 btn_getticket = self.mainui_passenger.pushButton btn_getticket.clicked.connect(gticket.show) btn_changeflight = self.mainui_passenger.pushButton_2 btn_changeflight.clicked.connect(gticket.click) # 主界面显示 def operate(self): self.mainui_passenger.label.setPixmap(QPixmap('./picture/picture2.jpg')) # 完全填充 self.mainui_passenger.label.setScaledContents(True) pasoperate.show() # 是否已点击更改按钮 self.i = 0 class Get_Ticket(QWidget): def __init__(self): QWidget.__init__(self) self.getui_ticket = Ui_Form4() self.getui_ticket.setupUi(self) btn_getticket = self.getui_ticket.pushButton btn_getticket.clicked.connect(self.gettic) # 是否已点击主界面上更改航班按钮 self.n = 0 def click(self): self.n = 1 gticket.show() def gettic(self): # 获取输入的取票码并匹配 code = self.getui_ticket.lineEdit.text() sql1 = "select * from informationmessage where code='%s'" % code cur.execute(sql1) if cur.execute(sql1): gticket.hide() QMessageBox.information(self, '提示', '取票码正确!', QMessageBox.Yes) df = pd.read_sql(sql1, conn) self.name = df.iloc[0, 0] self.sex = df.iloc[0, 1] self.id = df.iloc[0, 3] self.travel_time = df.iloc[0, 4] self.strat_place = df.iloc[0, 5] self.end_place = df.iloc[0, 6] self.cabin = df.iloc[0, 7] self.advance = df.iloc[0, 8] self.total_money = df.iloc[0, 9] self.code = df.iloc[0, 10] if self.n == 0: self.getpri() # 取票日期为航班日期前一天 spldate = self.travel_time.split('-') getdate = spldate[0] + '-' + spldate[1] + '-' + str(int(spldate[2]) - 1) spltime = time.strftime('%Y-%m-%d', time.localtime(time.time())).split('-') gettime = spltime[0] + '-' + str(int(spltime[1])) + '-' + str(int(spltime[2])) # 判断取票时间是否正确 if gettime == getdate: payment.current_pay() else: QMessageBox.information(self, '对不起', '取票时间已过,请更改航班!', QMessageBox.Yes) elif self.n == 1: chflight.show() else: QMessageBox.information(self, '对不起', '取票码错误,请重新输入!', QMessageBox.Yes) def getpri(self): # 获取航班价格和已乘载人数 sql2 = "select first_class, business_class, economy_class, count_passenger from flight where travel_time='%s' and start_place='%s' and end_place='%s'" \ % (self.travel_time, self.strat_place, self.end_place) cur.execute(sql2) # 判断航班是否取消 if cur.execute(sql2): df = pd.read_sql(sql2, conn) first_price = df.iloc[0, 0] business_price = df.iloc[0, 1] economy_price = df.iloc[0, 2] self.count_passenger = df.iloc[0, 3] if self.cabin == '头等舱': self.price = first_price elif self.cabin == '商务舱': self.price = business_price elif self.cabin == '经济舱': self.price = economy_price self.afterprice = str(int(self.price) - 50) else: QMessageBox.information(self, '对不起', '该航班已取消,请更改航班!', QMessageBox.Yes) class Payment(QWidget): def __init__(self): QWidget.__init__(self) self.payment_ui = Ui_Form5() self.payment_ui.setupUi(self) btn_payment = self.payment_ui.pushButton btn_payment.clicked.connect(ticket.plane_ticket) def current_pay(self): # 显示更改前或后的交款金额 if pasoperate.i == 0: self.payment_ui.label_2.setText(gticket.afterprice) elif pasoperate.i == 1: self.payment_ui.label_2.setText(chflight.afterprice) payment.show() class Ticket(QWidget): def __init__(self): QWidget.__init__(self) self.ticket_ui = Ui_Form6() self.ticket_ui.setupUi(self) def plane_ticket(self): self.ticket_ui.label_2.setText(gticket.name) self.ticket_ui.label_4.setText(gticket.sex) self.ticket_ui.label_6.setText(gticket.id) # 显示更改前或后的信息 if pasoperate.i == 0: self.ticket_ui.label_8.setText(gticket.strat_place) self.ticket_ui.label_10.setText(gticket.end_place) self.ticket_ui.label_12.setText(gticket.cabin) self.ticket_ui.label_14.setText(gticket.price) elif pasoperate.i == 1: self.ticket_ui.label_8.setText(chflight.newstart_place) self.ticket_ui.label_10.setText(chflight.newend_place) self.ticket_ui.label_12.setText(chflight.newcabin) self.ticket_ui.label_14.setText(chflight.price) payment.hide() ticket.show() # 重置更改按钮是否点击 pasoperate.i = 0 # 更新信息表的全额为已交 sql = "update informationmessage set total_money='%s' where code='%s'" % ('已交', gticket.code) cur.execute(sql) conn.commit() class Change_Fliht(QWidget): def __init__(self): QWidget.__init__(self) self.changeui_flight = Ui_Form8() self.changeui_flight.setupUi(self) btn_changeflight = self.changeui_flight.pushButton btn_changeflight.clicked.connect(self.changefli) # 定义取票码 self.six_code = '' def changefli(self): # 更改按钮已点击 pasoperate.i = 1 newyear = self.changeui_flight.lineEdit.text() newmonth = self.changeui_flight.lineEdit_2.text() newday = self.changeui_flight.lineEdit_3.text() self.newstart_place = self.changeui_flight.lineEdit_4.text() self.newend_place = self.changeui_flight.lineEdit_5.text() self.newcabin = self.changeui_flight.comboBox.currentText() self.time = newyear + '-' + newmonth + '-' + newday # 获取航班价格、乘载人数、航班代码 sql1 = "select first_class, business_class, economy_class, count_passenger, flight_number from flight where travel_time='%s' and start_place='%s' and end_place='%s'"\ % (self.time, self.newstart_place, self.newend_place) cur.execute(sql1) if cur.execute(sql1): df = pd.read_sql(sql1, conn) first_price = df.iloc[0, 0] business_price = df.iloc[0, 1] economy_price = df.iloc[0, 2] count_passenger = df.iloc[0, 3] number = df.iloc[0, 4] # 生成六位随机数的取票码 for i in range(6): self.six_code += chr(random.randrange(ord('0'), ord('9') + 1)) # 更新乘客信息 sql2 = "update informationmessage set travel_time='%s',start_place='%s',end_place='%s',class='%s',code='%s' where real_name='%s' and sex='%s'" \ " and id='%s'" % (self.time, self.newstart_place, self.newend_place, self.newcabin, self.six_code, gticket.name, gticket.sex, gticket.id) cur.execute(sql2) # 更新原航班乘载人数 sql3 = "update flight set count_passenger='%s' where travel_time='%s' and start_place='%s' and end_place='%s'" \ % (str(int(gticket.count_passenger) - 1), gticket.travel_time, gticket.strat_place, gticket.end_place) cur.execute(sql3) # 更新新航班乘载人数 sql4 = "update flight set count_passenger='%s' where flight_number='%s'" % (str(int(count_passenger) + 1), number) cur.execute(sql4) conn.commit() button = QMessageBox.information(self, '恭喜您', '已更改航班!', QMessageBox.Yes) if button == QMessageBox.Yes: # 显示取票界面信息 inbill.informui_bill.label_2.setText(gticket.name) inbill.informui_bill.label_4.setText(gticket.sex) inbill.informui_bill.label_6.setText(gticket.id) inbill.informui_bill.label_8.setText(self.newstart_place) inbill.informui_bill.label_10.setText(self.newend_place) inbill.informui_bill.label_12.setText(self.newcabin) if self.newcabin == '头等舱': self.price = first_price elif self.newcabin == '商务舱': self.price = business_price elif self.newcabin == '经济舱': self.price = economy_price self.afterprice = str(int(self.price) - 50) inbill.informui_bill.label_14.setText(self.afterprice) inbill.informui_bill.label_16.setText(newyear + '-' + newmonth + '-' + str(int(newday) - 1)) inbill.informui_bill.label_18.setText(self.six_code) chflight.hide() inbill.show() gticket.n = 0 else: QMessageBox.information(self, '对不起', '没有合适的航班!', QMessageBox.Yes) if __name__ == "__main__": app = QApplication(sys.argv) inbill = Inform_Bill() gticket = Get_Ticket() ticket = Ticket() payment = Payment() chflight = Change_Fliht() pasoperate = Passenger_Operate() pasoperate.operate() sys.exit(app.exec_())
连接数据库、数据表成功后,点击浏览数据按钮,显示航班信息,可添加和删除航班,也可双击表格对航班某个值进行修改,以下给出代码
from PyQt5 import QtWidgets, QtCore, QtSql from PyQt5.QtWidgets import * import sys class MainUi(QtWidgets.QMainWindow): def __init__(self): super().__init__() self.initUi() # 初始化界面 def initUi(self): # 设置窗口标题 self.setWindowTitle("数据库管理") # 设置窗口大小 self.resize(1150, 500) # 创建一个窗口部件 self.widget = QtWidgets.QWidget() # 创建一个网格布局 self.grid_layout = QtWidgets.QGridLayout() # 设置窗口部件的布局为网格布局 self.widget.setLayout(self.grid_layout) # 创建一个按钮组 self.group_box = QtWidgets.QGroupBox('数据库按钮') self.group_box_layout = QtWidgets.QVBoxLayout() self.group_box.setLayout(self.group_box_layout) # 创建一个表格部件 self.table_widget = QtWidgets.QTableView() # 将上述两个部件添加到网格布局中 self.grid_layout.addWidget(self.group_box, 0, 0) self.grid_layout.addWidget(self.table_widget, 0, 1) # 创建按钮组的按钮 self.b_connect_data = QtWidgets.QPushButton("连接数据库") self.b_connect_data.clicked.connect(self.connect_data) self.b_view_data = QtWidgets.QPushButton("浏览数据") self.b_view_data.clicked.connect(self.view_data) self.b_add_row = QtWidgets.QPushButton("添加一行") self.b_add_row.clicked.connect(self.add_row_data) self.b_delete_row = QtWidgets.QPushButton("删除一行") self.b_delete_row.clicked.connect(self.del_row_data) self.b_close = QtWidgets.QPushButton("退出") self.b_close.clicked.connect(self.close) # 添加按钮到按钮组中 self.group_box_layout.addWidget(self.b_connect_data) self.group_box_layout.addWidget(self.b_view_data) self.group_box_layout.addWidget(self.b_add_row) self.group_box_layout.addWidget(self.b_delete_row) self.group_box_layout.addWidget(self.b_close) # 设置UI界面的核心部件 self.setCentralWidget(self.widget) self.b_view_data.setDisabled(True) self.b_add_row.setDisabled(True) self.b_delete_row.setDisabled(True) # 连接数据库 def connect_data(self): self.db = QtSql.QSqlDatabase.addDatabase('QMYSQL') self.db.setHostName("localhost") self.db.setDatabaseName("bjx") self.db.setUserName("root") self.db.setPassword("123456") if self.db.open(): QMessageBox.information(self, "恭喜你", "连接数据库成功", QMessageBox.Yes) else: QMessageBox.information(self, "很遗憾", "连接数据库错误", QMessageBox.Yes) self.b_view_data.setDisabled(False) # 浏览数据 def view_data(self): # 实例化一个可编辑数据模型 self.model = QtSql.QSqlTableModel() self.table_widget.setModel(self.model) # 设置模型的数据表 self.model.setTable('flight') # 允许字段更改 self.model.setEditStrategy(QtSql.QSqlTableModel.OnFieldChange) # 查询所有数据 self.model.select() # 设置表格头 self.model.setHeaderData(0, QtCore.Qt.Horizontal, '航班代号') self.model.setHeaderData(1, QtCore.Qt.Horizontal, '始发地') self.model.setHeaderData(2, QtCore.Qt.Horizontal, '目的地') self.model.setHeaderData(3, QtCore.Qt.Horizontal, '航班时间') self.model.setHeaderData(4, QtCore.Qt.Horizontal, '头等舱价格') self.model.setHeaderData(5, QtCore.Qt.Horizontal, '商务舱价格') self.model.setHeaderData(6, QtCore.Qt.Horizontal, '经济舱价格') self.model.setHeaderData(7, QtCore.Qt.Horizontal, '乘载人数') self.b_add_row.setDisabled(False) self.b_delete_row.setDisabled(False) # 添加一行数据 def add_row_data(self): # 如果存在实例化的数据模型对象 if self.model: self.model.insertRows(self.model.rowCount(), 1) else: self.connect_data() # 删除一行数据 def del_row_data(self): if self.model: self.model.removeRow(self.table_widget.currentIndex().row()) else: self.connect_data() if __name__ == "__main__": app = QtWidgets.QApplication(sys.argv) myshow = MainUi() myshow.initUi() myshow.show() sys.exit(app.exec_())
到这里就结束了,功能很简易,有什么问题欢迎留言
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。