PyQt5如何实现文件对话框
本篇内容主要讲解“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 sysfrom PyQt6.QtWidgets import QApplicationfrom PyQt6.QtWidgets import QWidgetfrom PyQt6.QtWidgets import QMainWindowfrom PyQt6.QtWidgets import QFileDialogfrom PyQt6.QtWidgets import QPushButtonfrom PyQt6.QtWidgets import QLabelfrom PyQt6.QtWidgets import QLineEditfrom PyQt6.QtWidgets import QFormLayoutfrom PyQt6.QtGui import QPalettefrom 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如何实现文件对话框”有了更深的了解,不妨来实际操作一番吧!这里是编程网网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341