zoukankan      html  css  js  c++  java
  • QtGui.QFileDialog

    The QtGui.QFileDialog is a dialog that allows users to select files or directories. The files can be selected for both opening and saving.

    #!/usr/bin/python
    # -*- coding: utf-8 -*-
    
    """
    ZetCode PyQt4 tutorial 
    
    In this example, we select a file with a
    QtGui.QFileDialog and display its contents
    in a QtGui.QTextEdit.
    
    author: Jan Bodnar
    website: zetcode.com 
    last edited: October 2011
    """
    
    import sys
    from PyQt4 import QtGui
    
    
    class Example(QtGui.QMainWindow):
        
        def __init__(self):
            super(Example, self).__init__()
            
            self.initUI()
            
        def initUI(self):      
    
            self.textEdit = QtGui.QTextEdit()
            self.setCentralWidget(self.textEdit)
            self.statusBar()
    
            openFile = QtGui.QAction(QtGui.QIcon('open.png'), 'Open', self)
            openFile.setShortcut('Ctrl+O')
            openFile.setStatusTip('Open new File')
            openFile.triggered.connect(self.showDialog)
    
            menubar = self.menuBar()
            fileMenu = menubar.addMenu('&File')
            fileMenu.addAction(openFile)       
            
            self.setGeometry(300, 300, 350, 300)
            self.setWindowTitle('File dialog')
            self.show()
            
        def showDialog(self):
    
            fname = QtGui.QFileDialog.getOpenFileName(self, 'Open file', 
                    '/home')
            
            f = open(fname, 'r')
            
            with f:        
                data = f.read()
                self.textEdit.setText(data) 
                                    
            
    def main():
        
        app = QtGui.QApplication(sys.argv)
        ex = Example()
        sys.exit(app.exec_())
    
    
    if __name__ == '__main__':
        main()
    

    The example shows a menubar, centrally set text edit widget and a statusbar. The menu item shows the QtGui.QFileDialog which is used to select a file. The contents of the file are loaded into the text edit widget.

    class Example(QtGui.QMainWindow):
        
        def __init__(self):
            super(Example, self).__init__()
    

    The example is based on the QtGui.QMainWindow widget because we centrally set the text edit widget.

    fname = QtGui.QFileDialog.getOpenFileName(self, 'Open file', 
            '/home')
    

    We pop up the QtGui.QFileDialog. The first string in the getOpenFileName() method is the caption. The second string specifies the dialog working directory. By default, the file filter is set to All files (*).

    f = open(fname, 'r')
    
    with f:        
        data = f.read()
        self.textEdit.setText(data) 
    

    The selected file name is read and the contents of the file are set to the text edit widget.

    File DialogFigure: File dialog

  • 相关阅读:
    5.19 省选模拟赛 T1 小B的棋盘 双指针 性质
    5.15 省选模拟赛 容斥 生成函数 dp
    5.15 省选模拟赛 T1 点分治 FFT
    5.15 牛客挑战赛40 B 小V的序列 关于随机均摊分析 二进制
    luogu P4929 【模板】舞蹈链 DLX
    CF 878E Numbers on the blackboard 并查集 离线 贪心
    5.10 省选模拟赛 拍卖 博弈 dp
    5.12 省选模拟赛 T2 贪心 dp 搜索 差分
    5.10 省选模拟赛 tree 树形dp 逆元
    luogu P6088 [JSOI2015]字符串树 可持久化trie 线段树合并 树链剖分 trie树
  • 原文地址:https://www.cnblogs.com/hushaojun/p/4435712.html
Copyright © 2011-2022 走看看