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

QtGui.QFileDialog的更多相关文章

  1. PyQt4文件对话框QFileDialog

    文件对话框允许用户选择文件或文件夹,被选择的文件可进行读或写操作. #!/usr/bin/python # -*- coding: utf-8 -*- import sys from PyQt4 im ...

  2. Python应用03 使用PyQT制作视频播放器

    作者:Vamei 出处:http://www.cnblogs.com/vamei 严禁任何形式转载. 最近研究了Python的两个GUI包,Tkinter和PyQT.这两个GUI包的底层分别是Tcl/ ...

  3. PyQt4入门学习笔记(五)

    PyQt4里的对话框 对话框是大多数GUI应用中不可分割的一部分.一个对话框是两者或多者的会话.在GUI内,对话框是应用向人说话的方式.一个对话框可以用来输入数据,修改数据,改变应用设置等等. QtG ...

  4. Pyqt 获取打包二进制文件中的资源

    记得有一次打开一个单独exe程序,点击btn中的一个帮助说明按钮,在同级目录下就多出一个help.chm 文件并自动打开. 那这个exe肯定是把help.chm 打包到exe中,当我触发“帮助”按钮的 ...

  5. Pyqt 屏幕截图工具

    从Pyqt的examples中看到一段截图代码, (路径:examplas\desktop\screenshot.py) 所以想自己UI下界面,手动练习下 通过UI生成的: Screenshot.py ...

  6. Pyqt Smtplib实现Qthread多线程发送邮件

    一. smtplib 的介绍 smtplib.SMTP([host[, port[, local_hostname[, timeout]]]])   SMTP类构造函数,表示与SMTP服务器之间的连接 ...

  7. Pyqt 音视频播放器

    在寻找如何使用Pyqt做一个播放器时首先找到的是openCV2 openCV2 貌似太强大了,各种关于图像处理的事情它都能完成,如 读取摄像头.图像识别.人脸识别.  图像灰度处理 . 播放视频等,强 ...

  8. PyQT制作视频播放器

    Python应用03 使用PyQT制作视频播放器   作者:Vamei 出处:http://www.cnblogs.com/vamei 严禁任何形式转载. 最近研究了Python的两个GUI包,Tki ...

  9. PyQt4学习记录之事件和信号

    事件是任何 GUI程序中很重要的部分.所有 Python GUI 应用都是事件驱动的.一个应用对其生命期产生的不同的事件类型做出反应.事件是主要由应用的用户产生.但是,也可以通过其他方法产生,比如,网 ...

随机推荐

  1. MySQL注射绕过技巧(二)

    这次说到注入时空格的过滤 ①注释绕过空格 我们在查看user()的时候需要用到空格 如: select user(): 然后我们用 /**/ 来绕过 ②加号绕过空格(不太建议使用除个别情况) 其实   ...

  2. python爬取基础网页图片

    python基础爬虫总结 1.爬取信息原理 与浏览器客户端类似,向网站的服务器发送一个请求,该请求一般是url,也就是网址.之后服务器响应一个html页面给客户端,当然也有其他数据类型的信息,这些就是 ...

  3. Loj10172 涂抹果酱

    题目描述 Tyvj 两周年庆典要到了,Sam 想为 Tyvj 做一个大蛋糕.蛋糕俯视图是一个 N×M 的矩形,它被划分成 N×M 个边长为 1×1 的小正方形区域(可以把蛋糕当成 NNN 行 MMM列 ...

  4. Pandas中Series和DataFrame的索引

    在对Series对象和DataFrame对象进行索引的时候要明确这么一个概念:是使用下标进行索引,还是使用关键字进行索引.比如list进行索引的时候使用的是下标,而dict索引的时候使用的是关键字. ...

  5. PSCollectionView瀑布流实现

    [-] 一基本原理 二具体实现 相关数据结构 视图更新方式 relayoutViews方法 removeAndAddCellsIfNecessary方法 select方法 重用数据块视图机制 三使用方 ...

  6. MySQL MyISAM和InNodb备份与恢复技巧

    1. 为什么要备份数据库 对数据库来说,最重要也最容易被忽视的就是备份.由于不可预测性,偶然的事件可能会导致非常惨重的损失. 数据越是重要,数据的变化越频繁,备份越发需要经常进行. 备份周期根据不同业 ...

  7. java多线程知识点汇总(二)多线程实例解析

    本实验主要考察多线程对单例模式的操作,和多线程对同一资源的读取,两个知识.实验涉及到三个类: 1)一个pojo类Student,包括set/get方法. 2)一个线程类,设置student的成员变量a ...

  8. 虚拟机网络配置详解(NAT、桥接、Hostonly) z

    http://www.cnblogs.com/beginmind/p/6379881.html VirtualBox中有四种网络连接方式: NAT Bridged Adapter Internal H ...

  9. css方法实现div固定浏览器底端

    CSS: ;;background-image:url(text.txt);/*for IE6 防抖,也可以用一张图片URL*/ background-attachment:fixed;} /*是否存 ...

  10. ASP.NET MVC:看 MVC 源码,学习:如何将 Area 中的 Controller 放到独立的程序集?

    背景 本文假设您已经熟悉了 ASP.NET MVC 的常规开发方式.执行模型和关键扩展点,这里主要说一下如何使用 ASP.NET MVC 的源代码解决一些问题. 如何将 Area 中的 Control ...