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_())
  • 相关阅读:
    [Shoi2007]Vote 善意的投票
    [CQOI2015]任务查询系统
    [SDOI2013]森林
    HttpClient 教程 (二)
    /system改成可写
    Netbeans 6.8 + apktool_2.0.0b9 动态调试smali文件
    把中文版NetBeans改成英文版
    ZjDroid工具介绍及脱壳详细示例
    grep过滤搜索
    android Log图文详解(Log.v,Log.d,Log.i,Log.w,Log.e)
  • 原文地址:https://www.cnblogs.com/fuqia/p/8742958.html
Copyright © 2011-2022 走看看