马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import sys
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtSql import QSqlDatabase, QSqlTableModel
from PyQt5.QtCore import Qt
class BorrowStatusViewer(QWidget):
def __init__(self, studentId):
super(BorrowStatusViewer, self).__init__()
self.resize(700, 500)
self.studentId = studentId
self.setWindowTitle("欢迎使用图书馆管理系统")
self.setUpUI()
def setUpUI(self):
self.db = QSqlDatabase.addDatabase("QSQLITE")
self.db.setDatabaseName('./db/LibraryManagement.db')
self.db.open()
# 分为两块,上方是已借未归还书,下方是已归还书
self.layout = QVBoxLayout(self)
# Label设置
self.borrowedLabel = QLabel("未归还:")
self.returnedLabel = QLabel("已归还:")
self.borrowedLabel.setFixedHeight(32)
self.borrowedLabel.setFixedWidth(60)
self.returnedLabel.setFixedHeight(32)
self.returnedLabel.setFixedWidth(60)
font = QFont()
font.setPixelSize(18)
self.borrowedLabel.setFont(font)
self.returnedLabel.setFont(font)
# Table和Model
self.borrowedTableView = QTableView()
self.borrowedTableView.horizontalHeader().setStretchLastSection(True)
self.borrowedTableView.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
self.borrowedTableView.setEditTriggers(QAbstractItemView.NoEditTriggers)
self.returnedTableView = QTableView()
self.returnedTableView.horizontalHeader().setStretchLastSection(True)
self.returnedTableView.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
self.returnedTableView.setEditTriggers(QAbstractItemView.NoEditTriggers)
self.borrowedTableModel = QSqlTableModel()
self.returnedTableModel = QSqlTableModel()
self.borrowedTableView.setModel(self.borrowedTableModel)
self.returnedTableView.setModel(self.returnedTableModel)
self.borrowedTable()
self.borrowedTableModel.setHeaderData(0, Qt.Horizontal, "书名")
self.borrowedTableModel.setHeaderData(1, Qt.Horizontal, "书号")
self.borrowedTableModel.setHeaderData(2, Qt.Horizontal, "作者")
self.borrowedTableModel.setHeaderData(3, Qt.Horizontal, "分类")
self.borrowedTableModel.setHeaderData(4, Qt.Horizontal, "出版社")
self.borrowedTableModel.setHeaderData(5, Qt.Horizontal, "出版时间")
self.borrowedTableModel.setHeaderData(6, Qt.Horizontal, "借出时间")
self.returnedTable()
self.returnedTableModel.setHeaderData(0, Qt.Horizontal, "书名")
self.returnedTableModel.setHeaderData(1, Qt.Horizontal, "书号")
self.returnedTableModel.setHeaderData(2, Qt.Horizontal, "作者")
self.returnedTableModel.setHeaderData(3, Qt.Horizontal, "分类")
self.returnedTableModel.setHeaderData(4, Qt.Horizontal, "出版社")
self.returnedTableModel.setHeaderData(5, Qt.Horizontal, "出版时间")
self.returnedTableModel.setHeaderData(6, Qt.Horizontal, "借阅时间")
self.returnedTableModel.setHeaderData(7, Qt.Horizontal, "归还时间")
self.layout.addWidget(self.borrowedLabel)
self.layout.addWidget(self.borrowedTableView)
self.layout.addWidget(self.returnedLabel)
self.layout.addWidget(self.returnedTableView)
return
def borrowedTable(self):
self.model = QSqlTableModel(self)
self.borrowedTableModel.setTable('Book')
self.model.setEditStrategy(QSqlTableModel.OnFieldChange)
self.model.select()
return
def returnedTable(self):
self.model = QSqlTableModel(self)
self.returnedTableModel.setTable("Book")
self.model.setEditStrategy(QSqlTableModel.OnFieldChange)
self.model.select()
return
if __name__ == "__main__":
app = QApplication(sys.argv)
app.setWindowIcon(QIcon("./images/MainWindow_1.png"))
#app.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5())
mainMindow = BorrowStatusViewer("PB15000135")
mainMindow.show()
sys.exit(app.exec_())
为什么就是显示不了数据,是什么情况,求大哥解答!!! |