CollectibleDialog.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # CollectibleDialog.py
  2. import sqlite3
  3. from PyQt5.QtWidgets import QDialog, QVBoxLayout, QLabel, QLineEdit, QDialogButtonBox, QTreeWidgetItem, QCheckBox, \
  4. QMessageBox
  5. import logging
  6. logging.basicConfig(level=logging.DEBUG, format='%(asctime)s: %(message)s')
  7. logger = logging.getLogger(__name__)
  8. class CollectibleDialog(QDialog):
  9. def __init__(self, parent=None, title="添加收藏品", initial_code="", initial_name=""):
  10. super().__init__(parent)
  11. self.setWindowTitle(title)
  12. self.setGeometry(100, 100, 400, 150)
  13. layout = QVBoxLayout()
  14. # 收藏品编码输入框
  15. code_label = QLabel("收藏品编码")
  16. self.collectible_code_edit = QLineEdit()
  17. self.collectible_code_edit.setText(initial_code)
  18. layout.addWidget(code_label)
  19. layout.addWidget(self.collectible_code_edit)
  20. # 收藏品名称输入框
  21. name_label = QLabel("收藏品名称")
  22. self.collectible_name_edit = QLineEdit()
  23. self.collectible_name_edit.setText(initial_name)
  24. layout.addWidget(name_label)
  25. layout.addWidget(self.collectible_name_edit)
  26. button_box = QDialogButtonBox(QDialogButtonBox.StandardButton.Ok | QDialogButtonBox.StandardButton.Cancel)
  27. button_box.accepted.connect(self.accept)
  28. button_box.rejected.connect(self.reject)
  29. layout.addWidget(button_box)
  30. self.setLayout(layout)
  31. @staticmethod
  32. def add_collectible(self):
  33. dialog = CollectibleDialog(self, "添加收藏品")
  34. if dialog.exec():
  35. collectible_code = dialog.collectible_code_edit.text()
  36. collectible_name = dialog.collectible_name_edit.text()
  37. if collectible_code and collectible_name:
  38. item = QTreeWidgetItem(self.collectible_tree_widget)
  39. item.setText(1, collectible_code)
  40. item.setText(2, collectible_name)
  41. checkbox = QCheckBox()
  42. checkbox.setChecked(False)
  43. self.collectible_tree_widget.setItemWidget(item, 0, checkbox)
  44. @staticmethod
  45. def delete_collectible(self):
  46. selected_items = self.collectible_tree_widget.selectedItems()
  47. for item in selected_items:
  48. (item.parent() or self.collectible_tree_widget).takeTopLevelItem(self.collectible_tree_widget.indexOfTopLevelItem(item))
  49. @staticmethod
  50. def edit_collectible(self):
  51. selected_items = self.collectible_tree_widget.selectedItems()
  52. if len(selected_items) != 1:
  53. QMessageBox.warning(self, "编辑收藏品", "请选择一个收藏品进行编辑")
  54. return
  55. item = selected_items[0]
  56. dialog = CollectibleDialog(self, "编辑收藏品", item.text(1), item.text(2))
  57. if dialog.exec():
  58. new_collectible_code = dialog.collectible_code_edit.text()
  59. new_collectible_name = dialog.collectible_name_edit.text()
  60. if new_collectible_code and new_collectible_name:
  61. item.setText(1, new_collectible_code)
  62. item.setText(2, new_collectible_name)
  63. @staticmethod
  64. def save_collectibles(self):
  65. # 连接到SQLite数据库
  66. conn = sqlite3.connect('buff.db')
  67. cursor = conn.cursor()
  68. # 清空表中的数据
  69. cursor.execute('DELETE FROM collectibles')
  70. # 获取树形控件中的所有项
  71. for i in range(self.collectible_tree_widget.topLevelItemCount()):
  72. item = self.collectible_tree_widget.topLevelItem(i)
  73. collectible_code = item.text(1)
  74. collectible_name = item.text(2)
  75. checkbox = self.collectible_tree_widget.itemWidget(item, 0)
  76. selected = checkbox.isChecked() # 获取复选框的状态
  77. # 插入数据到表中
  78. cursor.execute('''
  79. INSERT INTO collectibles (code, name,selected)
  80. VALUES (?, ?,?)
  81. ''', (collectible_code, collectible_name,selected))
  82. # 提交更改并关闭连接
  83. conn.commit()
  84. conn.close()
  85. QMessageBox.information(self, "保存收藏品信息", "收藏品信息已成功保存")