else: item1 = menu.addAction(u"Return") action = menu.exec_(self.tableWidget.mapToGlobal(pos)) if self.tableWidget.rowCount() > 0: if action == item1: order_id = self.tableWidget.item(self.tableWidget.currentRow(), 0).text() sql = 'call book_return(%s)' self.sql_cursor.execute(sql, [order_id]) if self.sql_cursor.fetchall()[0][0] == '1': self.user_info_get() self.table_refresh_2() else: QMessageBox.information(self, "Remind", "The book has been returned!") def fun(self): if self.comboBox.currentIndex() == 0: if self.lineEdit.text() == '': self.table_refresh() else: self.boot_info_ret(self.lineEdit.text()) elif self.comboBox.currentIndex() == 1: self.table_refresh_2() def boot_info_ret(self, value): sql = [ "select * from book where locate(%s,book_name) > 0", "select * from book where locate(%s,editor) > 0", "select * from book where locate(%s,publisher) > 0" ] result = [] i = 0 while len(result) == 0 and i < 3: self.sql_cursor.execute(sql[i], value) result = self.sql_cursor.fetchall() i += 1 if len(result) == 0: QMessageBox.information(self, "Remind", "No relevant information was retrieved!") else: self.tableWidget.setColumnCount(7) self.tableWidget.setHorizontalHeaderLabels( ['Book number', 'Name', 'Writer', 'Publishing house', 'Number of books in collection', 'Number of pavilions', 'Pressmark']) rowlength = self.tableWidget.rowCount() for i in range(0, rowlength): self.tableWidget.removeRow(0) for i in range(len(result)): self.tableWidget.insertRow(i) self.tableWidget.setItem(i, 0, QTableWidgetItem(str(result[i][0]))) self.tableWidget.setItem(i, 1, QTableWidgetItem(result[i][1])) self.tableWidget.setItem(i, 2, QTableWidgetItem(result[i][2])) self.tableWidget.setItem(i, 3, QTableWidgetItem(str(result[i][3]))) self.tableWidget.setItem(i, 4, QTableWidgetItem(str(result[i][4]))) self.tableWidget.setItem(i, 5, QTableWidgetItem(str(result[i][5]))) self.tableWidget.setItem(i, 6, QTableWidgetItem(str(result[i][6]))) self.tableWidget.resizeColumnsToContents()每一行代码的意义和作用
时间: 2023-12-10 09:41:12 浏览: 103
这是一段 Python 代码,主要是实现一个图书馆管理系统的功能。下面是每一行代码的含义和作用:
```python
else: item1 = menu.addAction(u"Return") action = menu.exec_(self.tableWidget.mapToGlobal(pos))
```
这是一个条件语句,当不满足前一个条件时执行,定义了一个菜单项 item1,然后显示一个菜单。当用户在菜单中选择了一个操作,会将该操作赋值给 action。
```python
if self.tableWidget.rowCount() > 0:
if action == item1:
order_id = self.tableWidget.item(self.tableWidget.currentRow(), 0).text()
sql = 'call book_return(%s)'
self.sql_cursor.execute(sql, [order_id])
if self.sql_cursor.fetchall()[0][0] == '1':
self.user_info_get()
self.table_refresh_2()
else:
QMessageBox.information(self, "Remind", "The book has been returned!")
```
这是一个条件语句,判断表格中是否有数据,如果有,则根据用户选择的操作来执行不同的代码。如果用户选择了菜单项 item1,将获取当前行的书籍编号 order_id,并执行一个 SQL 查询,然后根据查询结果进行不同的操作。
```python
def fun(self):
if self.comboBox.currentIndex() == 0:
if self.lineEdit.text() == '':
self.table_refresh()
else:
self.boot_info_ret(self.lineEdit.text())
elif self.comboBox.currentIndex() == 1:
self.table_refresh_2()
```
这是一个函数,用于根据用户选择的下拉列表框中的选项来刷新表格。如果选项为0,则根据输入框中的内容进行模糊查询;如果选项为1,则刷新表格显示所有书籍信息。
```python
def boot_info_ret(self, value):
sql = [ "select * from book where locate(%s,book_name) > 0",
"select * from book where locate(%s,editor) > 0",
"select * from book where locate(%s,publisher) > 0" ]
result = []
i = 0
while len(result) == 0 and i < 3:
self.sql_cursor.execute(sql[i], value)
result = self.sql_cursor.fetchall()
i += 1
if len(result) == 0:
QMessageBox.information(self, "Remind", "No relevant information was retrieved!")
else:
self.tableWidget.setColumnCount(7)
self.tableWidget.setHorizontalHeaderLabels( ['Book number', 'Name', 'Writer', 'Publishing house', 'Number of books in collection', 'Number of pavilions', 'Pressmark'])
rowlength = self.tableWidget.rowCount()
for i in range(0, rowlength):
self.tableWidget.removeRow(0)
for i in range(len(result)):
self.tableWidget.insertRow(i)
self.tableWidget.setItem(i, 0, QTableWidgetItem(str(result[i][0])))
self.tableWidget.setItem(i, 1, QTableWidgetItem(result[i][1]))
self.tableWidget.setItem(i, 2, QTableWidgetItem(result[i][2]))
self.tableWidget.setItem(i, 3, QTableWidgetItem(str(result[i][3])))
self.tableWidget.setItem(i, 4, QTableWidgetItem(str(result[i][4])))
self.tableWidget.setItem(i, 5, QTableWidgetItem(str(result[i][5])))
self.tableWidget.setItem(i, 6, QTableWidgetItem(str(result[i][6])))
self.tableWidget.resizeColumnsToContents()
```
这是一个函数,用于根据输入的关键字进行模糊查询,并将查询结果显示在表格中。根据用户输入的内容,依次执行三个 SQL 查询语句,直到查询到结果为止。查询结果以列表形式返回,然后将结果显示在表格中。
```python
self.user_info_get()
```
这是一个函数,用于获取用户信息。
阅读全文