【数据库实验】,springboot 视频教程迅雷
self.userText.setFont(font)
self.userText.setAlignment(QtCore.Qt.AlignCenter)
self.userText.setObjectName("userText")
self.gridLayout.addWidget(self.userText, 0, 1, 1, 1)
self.label_3 = QtWidgets.QLabel(self.centralwidget)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.label_3.sizePolicy().hasHeightForWidth())
self.label_3.setSizePolicy(sizePolicy)
font = QtGui.QFont()
font.setPointSize(12)
self.label_3.setFont(font)
self.label_3.setObjectName("label_3")
self.gridLayout.addWidget(self.label_3, 1, 0, 1, 1)
self.pwdText = QtWidgets.QLineEdit(self.centralwidget)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.pwdText.sizePolicy().hasHeightForWidth())
self.pwdText.setSizePolicy(sizePolicy)
font = QtGui.QFont()
font.setPointSize(12)
self.pwdText.setFont(font)
self.pwdText.setAlignment(QtCore.Qt.AlignCenter)
self.pwdText.setObjectName("pwdText")
self.gridLayout.addWidget(self.pwdText, 1, 1, 1, 1)
self.gridLayout_2.addLayout(self.gridLayout, 1, 0, 1, 1)
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setContentsMargins(-1, -1, -1, 12)
self.horizontalLayout.setSpacing(20)
self.horizontalLayout.setObjectName("horizontalLayout")
self.okButton = QtWidgets.QPushButton(self.centralwidget)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.okButton.sizePolicy().hasHeightForWidth())
self.okButton.setSizePolicy(sizePolicy)
font = QtGui.QFont()
font.setPointSize(12)
self.okButton.setFont(font)
self.okButton.setObjectName("okButton")
self.horizontalLayout.addWidget(self.okButton)
self.cancelButton = QtWidgets.QPushButton(self.centralwidget)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.cancelButton.sizePolicy().hasHeightForWidth())
self.cancelButton.setSizePolicy(sizePolicy)
font = QtGui.QFont()
font.setPointSize(12)
self.cancelButton.setFont(font)
self.cancelButton.setObjectName("cancelButton")
self.horizontalLayout.addWidget(self.cancelButton)
self.gridLayout_2.addLayout(self.horizontalLayout, 2, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
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", " 登陆界面"))
self.label.setText(_translate("MainWindow", "请输入用户信息"))
self.label_2.setText(_translate("MainWindow", "用户名"))
self.label_3.setText(_translate("MainWindow", "密码"))
self.okButton.setText(_translate("MainWindow", "确定"))
self.cancelButton.setText(_translate("MainWindow", "取消"))
[](
)操作界面:operate.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(750, 460)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
self.tableWidget.setGeometry(QtCore.QRect(0, 90, 731, 161))
self.tableWidget.setObjectName("tableWidget")
self.tableWidget.setColumnCount(11)
self.tableWidget.setRowCount(9)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(0, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(1, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(2, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(3, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(4, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(5, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(6, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(7, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(8, item)
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)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(5, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(6, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(7, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(8, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(9, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(10, item)
self.layoutWidget = QtWidgets.QWidget(self.centralwidget)
self.layoutWidget.setGeometry(QtCore.QRect(100, 380, 208, 35))
self.layoutWidget.setObjectName("layoutWidget")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.layoutWidget)
self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout.setSpacing(20)
self.horizontalLayout.setObjectName("horizontalLayout")
self.pushButton = QtWidgets.QPushButton(self.layoutWidget)
font = QtGui.QFont()
font.setPointSize(13)
self.pushButton.setFont(font)
self.pushButton.setObjectName("pushButton")
self.horizontalLayout.addWidget(self.pushButton)
self.exitButton = QtWidgets.QPushButton(self.layoutWidget)
font = QtGui.QFont()
font.setPointSize(13)
self.exitButton.setFont(font)
self.exitButton.setObjectName("exitButton")
self.horizontalLayout.addWidget(self.exitButton)
self.layoutWidget1 = QtWidgets.QWidget(self.centralwidget)
self.layoutWidget1.setGeometry(QtCore.QRect(50, 290, 241, 74))
self.layoutWidget1.setObjectName("layoutWidget1")
self.verticalLayout = QtWidgets.QVBoxLayout(self.layoutWidget1)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setObjectName("verticalLayout")
self.horizontalLayout_3 = QtWidgets.QHBoxLayout()
self.horizontalLayout_3.setSpacing(20)
self.horizontalLayout_3.setObjectName("horizontalLayout_3")
self.label = QtWidgets.QLabel(self.layoutWidget1)
font = QtGui.QFont()
font.setPointSize(15)
self.label.setFont(font)
self.label.setObjectName("label")
self.horizontalLayout_3.addWidget(self.label)
self.lineEdit = QtWidgets.QLineEdit(self.layoutWidget1)
font = QtGui.QFont()
font.setPointSize(15)
self.lineEdit.setFont(font)
self.lineEdit.setObjectName("lineEdit")
self.horizontalLayout_3.addWidget(self.lineEdit)
self.verticalLayout.addLayout(self.horizontalLayout_3)
self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
self.horizontalLayout_2.setSpacing(20)
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.label_2 = QtWidgets.QLabel(self.layoutWidget1)
font = QtGui.QFont()
font.setPointSize(15)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.horizontalLayout_2.addWidget(self.label_2)
self.dateEdit = QtWidgets.QDateEdit(self.layoutWidget1)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.dateEdit.sizePolicy().hasHeightForWidth())
self.dateEdit.setSizePolicy(sizePolicy)
font = QtGui.QFont()
font.setPointSize(13)
self.dateEdit.setFont(font)
self.dateEdit.setObjectName("dateEdit")
self.horizontalLayout_2.addWidget(self.dateEdit)
self.verticalLayout.addLayout(self.horizontalLayout_2)
self.layoutWidget2 = QtWidgets.QWidget(self.centralwidget)
self.layoutWidget2.setGeometry(QtCore.QRect(200, 40, 321, 41))
self.layoutWidget2.setObjectName("layoutWidget2")
self.horizontalLayout_4 = QtWidgets.QHBoxLayout(self.layoutWidget2)
self.horizontalLayout_4.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout_4.setSpacing(20)
self.horizontalLayout_4.setObjectName("horizontalLayout_4")
self.pushButton_3 = QtWidgets.QPushButton(self.layoutWidget2)
self.pushButton_3.setObjectName("pushButton_3")
self.horizontalLayout_4.addWidget(self.pushButton_3)
self.pushButton_4 = QtWidgets.QPushButton(self.layoutWidget2)
self.pushButton_4.setObjectName("pushButton_4")
self.horizontalLayout_4.addWidget(self.pushButton_4)
self.pushButton_5 = QtWidgets.QPushButton(self.layoutWidget2)
self.pushButton_5.setObjectName("pushButton_5")
self.horizontalLayout_4.addWidget(self.pushButton_5)
MainWindow.setCentralWidget(self.centralwidget)
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", "操作界面"))
item = self.tableWidget.verticalHeaderItem(0)
item.setText(_translate("MainWindow", "1"))
item = self.tableWidget.verticalHeaderItem(1)
item.setText(_translate("MainWindow", "2"))
item = self.tableWidget.verticalHeaderItem(2)
item.setText(_translate("MainWindow", "3"))
item = self.tableWidget.verticalHeaderItem(3)
item.setText(_translate("MainWindow", "4"))
item = self.tableWidget.verticalHeaderItem(4)
item.setText(_translate("MainWindow", "5"))
item = self.tableWidget.verticalHeaderItem(5)
item.setText(_translate("MainWindow", "6"))
item = self.tableWidget.verticalHeaderItem(6)
item.setText(_translate("MainWindow", "7"))
item = self.tableWidget.verticalHeaderItem(7)
item.setText(_translate("MainWindow", "8"))
item = self.tableWidget.verticalHeaderItem(8)
item.setText(_translate("MainWindow", "9"))
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", "起飞时刻"))
item = self.tableWidget.horizontalHeaderItem(5)
item.setText(_translate("MainWindow", "到达时刻"))
item = self.tableWidget.horizontalHeaderItem(6)
item.setText(_translate("MainWindow", "票价"))
item = self.tableWidget.horizontalHeaderItem(7)
item.setText(_translate("MainWindow", "折扣票数"))
item = self.tableWidget.horizontalHeaderItem(8)
item.setText(_translate("MainWindow", "剩余座位数"))
item = self.tableWidget.horizontalHeaderItem(9)
item.setText(_translate("MainWindow", "折扣率"))
item = self.tableWidget.horizontalHeaderItem(10)
item.setText(_translate("MainWindow", "航班所属航空公司"))
self.pushButton.setText(_translate("MainWindow", "查询"))
self.exitButton.setText(_translate("MainWindow", "退出"))
self.label.setText(_translate("MainWindow", "航班号"))
self.label_2.setText(_translate("MainWindow", "日期"))
self.pushButton_3.setText(_translate("MainWindow", "增加"))
self.pushButton_4.setText(_translate("MainWindow", "修改"))
self.pushButton_5.setText(_translate("MainWindow", "删除"))
[](
)逻辑代码 app.py
==============================================================================
将以上的界面代码 与 下面的逻辑代码放在一个文件下就可以运行啦!(需要 PyQt 环境)
环境安装可以看这个:[【PyQt5】PyQt5 安装 以及使用 designer 开发 python GUI 界面](
)
import sys
from PyQt5 import QtCore, QtWidgets, QtCore
import pymysql
from hello import Ui_MainWindow as Hello_Ui
from login import Ui_MainWindow as Login_Ui
from operate import Ui_MainWindow as Operate_Ui
主窗口
class HelloWindow(QtWidgets.QMainWindow, Hello_Ui):
switch_window1 = QtCore.pyqtSignal()
switch_window2 = QtCore.pyqtSignal()
def init(self):
super(HelloWindow, self).init()
self.setupUi(self)
self.queryButton.clicked.connect(self.goOperate)
self.manageButton.clicked.connect(self.goLogin)
def goLogin(self):
self.switch_window1.emit()
def goOperate(self):
self.switch_window2.emit()
登录窗口
class LoginWindow(QtWidgets.QMainWindow, Login_Ui):
switch_window1 = QtCore.pyqtSignal()
switch_window2 = QtCore.pyqtSignal()
def init(self):
super(LoginWindow, self).init()
self.setupUi(self)
self.okButton.clicked.connect(self.ok)
self.cancelButton.clicked.connect(self.cancel)
def ok(self):
username = self.userText.text()
password = self.pwdText.text()
创建数据库连接
conn = pymysql.connect(
host = '127.0.0.1', # 连接主机, 默认 127.0.0.1
user = 'root', # 用户名
passwd = '1234', # 密码
port = 3306, # 端口,默认为 3306
db = 'sxn210224', # 数据库名称
charset = 'utf8' # 字符编码
)
生成游标对象 cursor
cursor = conn.cursor()
if (cursor.execute("SELECT * FROM user WHERE u_username='%s' AND u_password='%s'" %(username, password))):
self.switch_window1.emit()
else:
print("密码错误!!")
cursor.close()
conn.close()
def cancel(self):
self.switch_window2.emit()
操作窗口
class OperateWindow(QtWidgets.QMainWindow, Operate_Ui):
def init(self):
super(OperateWindow, self).init()
self.setupUi(self)
self.exitButton.clicked.connect(self.exit)
数据库连接对象
conn = pymysql.connect(host='localhost', port=3306, user='root', password="1234", db="sxn210224")
游标对象
cur = conn.cursor()
查询的 sql 语句
sql = "SELECT * FROM flight"
cur.execute(sql)
获取查询到的数据, 是以二维元组的形式存储的, 所以读取需要使用 data[i][j] 下标定位
data = cur.fetchall()
打印测试
print(data)
遍历二维元组, 将 id 和 name 显示到界面表格上
x = 0
for i in data:
y = 0
for j in i:
self.tableWidget.setItem(x, y, QtWidgets.QTableWidgetItem(str(data[x][y])))
y = y + 1
x = x + 1
cur.close()
conn.close()
def exit(self):
sys.exit(0)
利用一个控制器来控制页面的跳转
class Controller:
def init(self):
self.hello = HelloWindow()
self.login = LoginWindow()
self.operate = OperateWindow()
def show_hello(self):
self.hello = HelloWindow()
self.hello.switch_window1.connect(self.show_login)
self.hello.switch_window2.connect(self.show_operate)
self.hello.show()
self.login.close()
self.operate.close()
def show_login(self):
self.login = LoginWindow()
self.login.switch_window1.connect(self.show_operate)
self.login.switch_window2.connect(self.show_hello)
self.login.show()
self.hello.close()
self.operate.close()
def show_operate(self):
self.operate = OperateWindow()
self.operate.show()
self.hello.close()
self.login.close()
def main():
app = QtWidgets.QApplication(sys.argv)
controller = Controller()
controller.show_hello()
sys.exit(app.exec_())
if name == 'main':
main()
评论