您好,登錄后才能下訂單哦!
本篇內容主要講解“PyQt5如何實現文件對話框”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“PyQt5如何實現文件對話框”吧!
文件對話框(QFileDialog)用于瀏覽文件并獲取文件路徑。
getSaveFileName(parent: typing.Optional[QWidget] = ..., caption: str = ..., directory: str = ..., filter: str = ..., initialFilter: str = ..., options: 'QFileDialog.Option' = ...) -> typing.Tuple[str, str]: ...
getOpenFileNames(parent: typing.Optional[QWidget] = ..., caption: str = ..., directory: str = ..., filter: str = ..., initialFilter: str = ..., options: 'QFileDialog.Option' = ...) -> typing.Tuple[typing.List[str], str]: ...
getOpenFileName(parent: typing.Optional[QWidget] = ..., caption: str = ..., directory: str = ..., filter: str = ..., initialFilter: str = ..., options: 'QFileDialog.Option' = ...) -> typing.Tuple[str, str]: ...
getExistingDirectoryUrl(parent: typing.Optional[QWidget] = ..., caption: str = ..., directory: QtCore.QUrl = ..., options: 'QFileDialog.Option' = ..., supportedSchemes: typing.Iterable[str] = ...) -> QtCore.QUrl: ...
getExistingDirectory(parent: typing.Optional[QWidget] = ..., caption: str = ..., directory: str = ..., options: 'QFileDialog.Option' = ...) -> str: ...
示例:
# _*_ coding:utf-8 _*_ import sys from PyQt6.QtWidgets import QApplication from PyQt6.QtWidgets import QWidget from PyQt6.QtWidgets import QMainWindow from PyQt6.QtWidgets import QFileDialog from PyQt6.QtWidgets import QPushButton from PyQt6.QtWidgets import QLabel from PyQt6.QtWidgets import QLineEdit from PyQt6.QtWidgets import QFormLayout from PyQt6.QtGui import QPalette from PyQt6.QtCore import Qt class QFileDialogDemo(QMainWindow): """字體對話框""" def __init__(self): """構造函數""" super(QFileDialogDemo, self).__init__() self.init_ui() def init_ui(self): self.setWindowTitle("QColorDialogDemo") self.resize(400, 200) # 獲取中央控件 self.centralwidget = QWidget() self.setCentralWidget(self.centralwidget) # 設置布局 self.formlayout = QFormLayout() self.formlayout.setAlignment(Qt.AlignmentFlag.AlignCenter) self.centralwidget.setLayout(self.formlayout) # 添加標簽和按鈕 self.label = QLabel("文件路徑:") self.ledit_filepath = QLineEdit() self.formlayout.addRow(self.label, self.ledit_filepath) self.btn_openfile = QPushButton("打開文件") self.btn_openfile.clicked.connect(self.openfile) self.formlayout.addWidget(self.btn_openfile) def openfile(self): """獲取顏色""" # filename->返回的文件路徑,filetypelist->設置的要打開的文件類型 filename, filetypelist = QFileDialog.getOpenFileName( self, "Open File", r"D:\Desktop", "文本文件(*.txt *.csv)") self.ledit_filepath.setText(filename + ";" + filetypelist) if __name__ == "__main__": """主程序運行""" app = QApplication(sys.argv) main = QFileDialogDemo() main.show() sys.exit(app.exec())
結果:
到此,相信大家對“PyQt5如何實現文件對話框”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。