zoukankan      html  css  js  c++  java
  • 对话框——文件对话框

    QFileDialog是一个允许用户选择文件或目录的对话框。可用以选择打开或保存文件

    示例图如下:

     1 #!/usr/bin/python3
     2 # -*- coding: utf-8 -*-
     3 
     4 """
     5 ZetCode PyQt5 tutorial
     6 
     7 In this example, we select a file with a
     8 QFileDialog and display its contents
     9 in a QTextEdit.
    10 
    11 Author: Jan Bodnar
    12 Website: zetcode.com
    13 Last edited: August 2017
    14 """
    15 
    16 from PyQt5.QtWidgets import (QMainWindow, QTextEdit,
    17     QAction, QFileDialog, QApplication)
    18 from PyQt5.QtGui import QIcon
    19 import sys
    20 
    21 
    22 class Example(QMainWindow):
    23 
    24     def __init__(self):
    25         super().__init__()
    26 
    27         self.initUI()
    28 
    29     def initUI(self):
    30 
    31         self.textEdit = QTextEdit()
    32         # 把self.textEdit设置为主窗口的中心窗口部件
    33         self.setCentralWidget(self.textEdit)
    34         self.statusBar()
    35 
    36         openFile = QAction(QIcon('open.png'), 'Open', self)
    37         openFile.setShortcut('Ctrl+O')
    38         openFile.setStatusTip('Open new File')
    39         openFile.triggered.connect(self.showDialog)
    40 
    41         menubar = self.menuBar()
    42         fileMenu = menubar.addMenu('&File')
    43         fileMenu.addAction(openFile)
    44 
    45         self.setGeometry(300, 300, 350, 300)
    46         self.setWindowTitle('File dialog')
    47         self.show()
    48 
    49     def showDialog(self):
    50         
    51         # 弹出文件对话框
    52         # The first string in the getOpenFileName() method is the caption
    53         # The second string specifies the dialog working directory
    54         fname = QFileDialog.getOpenFileName(self, 'Open file', '/home')
    55 
    56         if fname[0]:
    57             f = open(fname[0], 'r')
    58             print(f)
    59 
    60             with f:
    61                 data = f.read()
    62                 print(data)
    63                 self.textEdit.setText(data)
    64 
    65 
    66 if __name__ == '__main__':
    67 
    68     app = QApplication(sys.argv)
    69     ex = Example()
    70     sys.exit(app.exec_())
  • 相关阅读:
    Spring学习(九)
    NPOI的一些基本操作
    WebClient请求接口,get和post方法
    树结构关系的数据导出为excel
    AOP实践--利用MVC5 Filter实现登录状态判断
    js小结
    (转)基于http协议的api接口对于客户端的身份认证方式以及安全措施
    C# 下载文件 只利用文件的存放路径来下载
    linux nginx启动 重启 关闭命令
    两种 js下载文件的方法(转)
  • 原文地址:https://www.cnblogs.com/fuqia/p/8742958.html
Copyright © 2011-2022 走看看