-
Notifications
You must be signed in to change notification settings - Fork 36
/
addBookDialog.py
158 lines (140 loc) · 6.41 KB
/
addBookDialog.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import qdarkstyle
import time
from PyQt5.QtSql import *
class addBookDialog(QDialog):
add_book_success_signal = pyqtSignal()
def __init__(self, parent=None):
super(addBookDialog, self).__init__(parent)
self.setUpUI()
self.setWindowModality(Qt.WindowModal)
self.setWindowTitle("添加书籍")
def setUpUI(self):
# 书名,书号,作者,分类,添加数量.出版社,出版日期
# 书籍分类:哲学类、社会科学类、政治类、法律类、军事类、经济类、文化类、教育类、体育类、语言文字类、艺术类、历史类、地理类、天文学类、生物学类、医学卫生类、农业类
BookCategory = ["哲学", "社会科学", "政治", "法律", "军事", "经济", "文化", "教育", "体育", "语言文字", "艺术", "历史"
, "地理", "天文学", "生物学", "医学卫生", "农业"]
self.resize(300, 400)
self.layout = QFormLayout()
self.setLayout(self.layout)
# Label控件
self.titlelabel = QLabel(" 添加书籍")
self.bookNameLabel = QLabel("书 名:")
self.bookIdLabel = QLabel("书 号:")
self.authNameLabel = QLabel("作 者:")
self.categoryLabel = QLabel("分 类:")
self.publisherLabel = QLabel("出 版 社:")
self.publishDateLabel = QLabel("出版日期:")
self.addNumLabel = QLabel("数 量:")
# button控件
self.addBookButton = QPushButton("添 加")
# lineEdit控件
self.bookNameEdit = QLineEdit()
self.bookIdEdit = QLineEdit()
self.authNameEdit = QLineEdit()
self.categoryComboBox = QComboBox()
self.categoryComboBox.addItems(BookCategory)
self.publisherEdit = QLineEdit()
self.publishTime = QDateTimeEdit()
self.publishTime.setDisplayFormat("yyyy-MM-dd")
# self.publishDateEdit = QLineEdit()
self.addNumEdit = QLineEdit()
self.bookNameEdit.setMaxLength(10)
self.bookIdEdit.setMaxLength(6)
self.authNameEdit.setMaxLength(10)
self.publisherEdit.setMaxLength(10)
self.addNumEdit.setMaxLength(12)
self.addNumEdit.setValidator(QIntValidator())
# 添加进formlayout
self.layout.addRow("", self.titlelabel)
self.layout.addRow(self.bookNameLabel, self.bookNameEdit)
self.layout.addRow(self.bookIdLabel, self.bookIdEdit)
self.layout.addRow(self.authNameLabel, self.authNameEdit)
self.layout.addRow(self.categoryLabel, self.categoryComboBox)
self.layout.addRow(self.publisherLabel, self.publisherEdit)
self.layout.addRow(self.publishDateLabel, self.publishTime)
self.layout.addRow(self.addNumLabel, self.addNumEdit)
self.layout.addRow("", self.addBookButton)
# 设置字体
font = QFont()
font.setPixelSize(20)
self.titlelabel.setFont(font)
font.setPixelSize(14)
self.bookNameLabel.setFont(font)
self.bookIdLabel.setFont(font)
self.authNameLabel.setFont(font)
self.categoryLabel.setFont(font)
self.publisherLabel.setFont(font)
self.publishDateLabel.setFont(font)
self.addNumLabel.setFont(font)
self.bookNameEdit.setFont(font)
self.bookIdEdit.setFont(font)
self.authNameEdit.setFont(font)
self.publisherEdit.setFont(font)
self.publishTime.setFont(font)
self.categoryComboBox.setFont(font)
self.addNumEdit.setFont(font)
# button设置
font.setPixelSize(16)
self.addBookButton.setFont(font)
self.addBookButton.setFixedHeight(32)
self.addBookButton.setFixedWidth(140)
# 设置间距
self.titlelabel.setMargin(8)
self.layout.setVerticalSpacing(10)
self.addBookButton.clicked.connect(self.addBookButtonCicked)
def addBookButtonCicked(self):
bookName = self.bookNameEdit.text()
bookId = self.bookIdEdit.text()
authName = self.authNameEdit.text()
bookCategory = self.categoryComboBox.currentText()
publisher = self.publisherEdit.text()
publishTime = self.publishTime.text()
addBookNum = self.addNumEdit.text()
if (
bookName == "" or bookId == "" or authName == "" or bookCategory == "" or publisher == "" or publishTime == "" or addBookNum == ""):
print(QMessageBox.warning(self, "警告", "有字段为空,添加失败", QMessageBox.Yes, QMessageBox.Yes))
return
else:
addBookNum = int(addBookNum)
db = QSqlDatabase.addDatabase("QSQLITE")
db.setDatabaseName('./db/LibraryManagement.db')
db.open()
query = QSqlQuery()
# 如果已存在,则update Book表的现存量,剩余可借量,不存在,则insert Book表,同时insert buyordrop表
sql = "SELECT * FROM Book WHERE BookId='%s'" % (bookId)
query.exec_(sql)
if (query.next()):
sql = "UPDATE Book SET NumStorage=NumStorage+%d,NumCanBorrow=NumCanBorrow+%d WHERE BookId='%s'" % (
addBookNum, addBookNum, bookId)
else:
sql = "INSERT INTO book VALUES ('%s','%s','%s','%s','%s','%s',%d,%d,0)" % (
bookName, bookId, authName, bookCategory, publisher, publishTime, addBookNum, addBookNum)
query.exec_(sql)
db.commit()
# 插入droporinsert表
timenow = time.strftime('%Y-%m-%d', time.localtime(time.time()))
sql = "INSERT INTO buyordrop VALUES ('%s','%s',1,%d)" % (bookId, timenow, addBookNum)
query.exec_(sql)
db.commit()
print(QMessageBox.information(self, "提示", "添加书籍成功!", QMessageBox.Yes, QMessageBox.Yes))
self.add_book_success_signal.emit()
self.close()
self.clearEdit()
return
def clearEdit(self):
self.bookNameEdit.clear()
self.bookIdEdit.clear()
self.authNameEdit.clear()
self.addNumEdit.clear()
self.publisherEdit.clear()
if __name__ == "__main__":
app = QApplication(sys.argv)
app.setWindowIcon(QIcon("./images/MainWindow_1.png"))
app.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5())
mainMindow = addBookDialog()
mainMindow.show()
sys.exit(app.exec_())