123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- # 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
-
- 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):
- # 清空现有的 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))
|