欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于Python自制一個資源管理器

 更新時間:2025年02月19日 10:31:18   作者:席子哥哥的代碼庫  
這篇文章主要為大家詳細介紹了如何基于Python自制一個資源管理器,文中的示例代碼講解詳細,具有一定的借鑒價值,感興趣的小伙伴可以了解下

本文為大家詳細介紹了如何基于Python制作一個資源管理器,可以進行簡單的本地資源管理,有需要的可以參考下

最終效果圖

完整代碼

import sys
from PyQt5.QtWidgets import (
    QApplication, QMainWindow, QTreeView, QFileSystemModel, QSplitter, QTextEdit, QVBoxLayout, QWidget, QPushButton, QListWidget, QListWidgetItem, QHBoxLayout
)
from PyQt5.QtCore import QDir, QFileInfo
from PyQt5.QtGui import QIcon
 
class ResourceBrowser(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("資源瀏覽器")
        self.setGeometry(100, 100, 800, 600)
        self.setStyleSheet("""
            QMainWindow {
                background-color: #2E3440;
            }
            QListWidget {
                background-color: #3B4252;
                color: #ECEFF4;
                border: 1px solid #4C566A;
            }
            QPushButton {
                background-color: #81A1C1;
                color: #ECEFF4;
                padding: 5px;
                border: none;
            }
            QPushButton:hover {
                background-color: #5E81AC;
            }
        """)
 
        # 當前目錄路徑
        self.current_dir = QDir.rootPath()
 
        # 創(chuàng)建主布局
        main_widget = QWidget()
        self.setCentralWidget(main_widget)
        layout = QVBoxLayout(main_widget)
 
        # 創(chuàng)建分割器
        splitter = QSplitter()
        layout.addWidget(splitter)
 
        # 左側布局
        left_widget = QWidget()
        left_layout = QVBoxLayout(left_widget)
 
        # 返回上一級按鈕
        self.btn_back = QPushButton("返回上一級")
        self.btn_back.clicked.connect(self.go_back)
        left_layout.addWidget(self.btn_back)
 
        # 創(chuàng)建文件系統(tǒng)模型
        self.model = QFileSystemModel()
        self.model.setRootPath("")  # 設置根路徑為空,顯示所有磁盤
 
        # 創(chuàng)建樹視圖
        self.tree_view = QTreeView()
        self.tree_view.setModel(self.model)
        self.tree_view.setRootIndex(self.model.index(""))  # 設置根目錄為空,顯示所有磁盤
        self.tree_view.clicked.connect(self.on_item_clicked)  # 點擊事件
        left_layout.addWidget(self.tree_view)
 
        # 將左側布局添加到分割器
        splitter.addWidget(left_widget)
 
        # 創(chuàng)建右側布局
        right_widget = QWidget()
        right_layout = QVBoxLayout(right_widget)
 
        # 創(chuàng)建右側文件列表
        self.file_list = QListWidget()
        self.file_list.itemDoubleClicked.connect(self.on_file_double_clicked)  # 雙擊文件事件
        right_layout.addWidget(self.file_list)
 
        # 創(chuàng)建文本編輯框用于顯示文件內容
        self.text_edit = QTextEdit()
        self.text_edit.setReadOnly(True)  # 設置為只讀
        right_layout.addWidget(self.text_edit)
 
        # 將右側布局添加到分割器
        splitter.addWidget(right_widget)
 
        # 設置分割器初始比例
        splitter.setSizes([300, 500])
 
    def on_item_clicked(self, index):
        """處理樹視圖項的點擊事件"""
        file_path = self.model.filePath(index)
        if self.model.isDir(index):
            # 如果是文件夾,更新當前目錄并顯示文件列表
            self.current_dir = file_path
            self.update_file_list(file_path)
 
    def update_file_list(self, dir_path):
        """更新右側文件列表"""
        self.file_list.clear()
        dir = QDir(dir_path)
        for file_info in dir.entryInfoList(QDir.Files | QDir.Dirs | QDir.NoDotAndDotDot):
            item = QListWidgetItem(file_info.fileName())
            if file_info.isDir():
                item.setIcon(QIcon.fromTheme("folder"))  # 文件夾圖標
            else:
                item.setIcon(QIcon.fromTheme("text-x-generic"))  # 文件圖標
            self.file_list.addItem(item)
 
    def on_file_double_clicked(self, item):
        """處理文件列表項的雙擊事件"""
        file_name = item.text()
        file_path = QDir(self.current_dir).filePath(file_name)
        if QFileInfo(file_path).isDir():
            # 如果是文件夾,更新當前目錄并顯示文件列表
            self.current_dir = file_path
            self.update_file_list(file_path)
        else:
            # 如果是文件,顯示文件內容
            try:
                with open(file_path, "r", encoding="utf-8") as file:
                    self.text_edit.setPlainText(file.read())
            except Exception as e:
                self.text_edit.setPlainText(f"無法讀取文件: {str(e)}")
 
    def go_back(self):
        """返回上一級目錄"""
        current_dir = QDir(self.current_dir)
        if current_dir.cdUp():  # 嘗試返回上一級目錄
            self.current_dir = current_dir.absolutePath()  # 獲取上一級目錄的絕對路徑
            self.update_file_list(self.current_dir)
            # 更新樹視圖的選中狀態(tài)
            index = self.model.index(self.current_dir)
            self.tree_view.setCurrentIndex(index)
 
if __name__ == "__main__":
    app = QApplication(sys.argv)
    browser = ResourceBrowser()
    browser.show()
    sys.exit(app.exec())

到此這篇關于基于Python自制一個資源管理器的文章就介紹到這了,更多相關Python資源管理器內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論