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.

Figure: File dialog

时间: 2024-08-05 04:24:27

QtGui.QFileDialog的相关文章

PyQt4文件对话框QFileDialog

文件对话框允许用户选择文件或文件夹,被选择的文件可进行读或写操作. #!/usr/bin/python # -*- coding: utf-8 -*- import sys from PyQt4 import QtGui, QtCore class OpenFile(QtGui.QMainWindow): def __init__(self, parent = None): QtGui.QWidget.__init__(self, parent) self.setGeometry(300, 30

ZetCode PyQt4 tutorial Dialogs

#!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, we receive data from a QtGui.QInputDialog dialog. author: Jan Bodnar website: zetcode.com last edited: October 2011 """ import sys from PyQ

PyQt Phonon 模块初探

之前一直在windows上测试Phonon代码,刚刚回来用linux居然提示我没有Phonon模块 我使用的发行版是自带pyqt4的,但是不带Phonon模块,其实也说的通,Phonon本身也不是Qt的项目,详细请百度  sorry 是谷歌 我用的ubuntu系列,安装Phonon模块方法如下: sudo apt-get install python-qt4-phonon 其他版本请使用yum install ****  或下载源代码自行编译安装 下面这个简单的例子,道出了Phonon基本使用之

pyqt MainWindow记录内容

class Texts(QtGui.QMainWindow,Ui_MainWindow): def __init__(self,parne=None): super(Texts,self).__init__(parne) self.setupUi(self) self.setWindowTitle(u'文档或者其他') self.setWindowIcon((QtGui.QIcon(r'i/e.jpg'))) self.setFixedSize(500,550) self.action_3_cl

PyQT制作视频播放器

Python应用03 使用PyQT制作视频播放器 作者:Vamei 出处:http://www.cnblogs.com/vamei 严禁任何形式转载. 最近研究了Python的两个GUI包,Tkinter和PyQT.这两个GUI包的底层分别是Tcl/Tk和QT.相比之下,我觉得PyQT使用起来更加方便,功能也相对丰富.这一篇用PyQT实现一个视频播放器,并借此来说明PyQT的基本用法. 视频播放器 先把已经完成的代码放出来.代码基于Python 3.5: import time import s

Pyqt4学习笔记-对话框(更新ing)

QInputDialog:可交互输入单个值的对话框.输入值可以是一个字符串,一个数字或者列表的一项. #!/usr/bin/python # -*- coding: utf-8 -*- import sys from PyQt4 import QtGui from PyQt4 import QtCore class InputExample(QtGui.QWidget): def __init__(self): super(InputExample, self).__init__() self.

PyQt4的一些问题汇总

(1)PyQt4获取中文路径名字乱码问题 网址可以参见:http://permalink.gmane.org/gmane.comp.python.chinese/9916 处理方式的代码可以参考如下 #[1]第一种写法 filePath = unicode(QtGui.QFileDialog.getOpenFileName(None, "All Files (*)", self)) #[2]第二种写法 filechoose = QtGui.QFileDialog(self) fileP

pyqt练习x3.21

#!/usr/bin/env python2 "A web browser that will never exceed 128 lines of code. (not counting blanks)" import sys, os, json, tempfile from PyQt4 import QtGui, QtCore, QtWebKit, QtNetwork settings = QtCore.QSettings("ralsina", "dev

pyqt 学习基础5 - 笔记软件的编辑器

笔记软件的编辑器 几个疑问 今天在试着为笔记软件写个编辑器,然后参考qt自带的textedit的示例.下面是一个撤销操作的一段代码,我就疑惑了,这跟用QPushButton应该差不多吧. self.actionUndo = QtGui.QAction( QtGui.QIcon.fromTheme('edit-undo', QtGui.QIcon(rsrcPath + '/editundo.png')), "&Undo", self, shortcut=QtGui.QKeySeq