本篇内容主要讲解“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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。