1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- # file_system_watching.py 文件系统监控代码,用于监控comboBox的显示
- from watchdog.events import FileSystemEventHandler
- from PySide6.QtCore import QCoreApplication
- from watchdog.observers import Observer
- import os
-
-
- class ComboBoxUpdater(FileSystemEventHandler):
- def __init__(self, comboBox, sql_folder):
- super().__init__()
- self.comboBox = comboBox
- self.sql_folder = sql_folder
- self.selected_db = None # 新增属性来存储当前选中的数据库
-
- def on_modified(self, event):
- if event.is_directory:
- return
- if event.src_path.endswith('.db'):
- self.update_combo_box()
-
- def on_created(self, event):
- if event.is_directory:
- return
- if event.src_path.endswith('.db'):
- self.update_combo_box()
-
- def on_deleted(self, event):
- if event.is_directory:
- return
- if event.src_path.endswith('.db'):
- self.update_combo_box()
-
- def update_combo_box(self):
- # 保存当前选中的数据库
- self.selected_db = self.comboBox.currentText()
- # 清空现有的 comboBox 内容
- self.comboBox.clear()
-
- # 列出 SQL 文件夹中的所有 .db 文件
- db_files = [f for f in os.listdir(self.sql_folder) if f.endswith('.db')]
-
- # 将数据库文件名添加到 comboBox 中
- for db_file in db_files:
- db_name = os.path.splitext(db_file)[0] # 获取数据库名称并去掉.db后缀
- self.comboBox.addItem(QCoreApplication.translate("MainWindow", db_name, None))
-
- # 如果没有找到任何数据库文件,显示提示信息
- if not db_files:
- self.comboBox.addItem(QCoreApplication.translate("MainWindow", "未找到数据库文件", None))
-
- # 重新设置之前选中的数据库为选中状态
- if self.selected_db in [self.comboBox.itemText(i) for i in range(self.comboBox.count())]:
- self.comboBox.setCurrentText(self.selected_db)
|