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. android 传递 类对象 序列化 Serializable

    public class Song implements Serializable { /** * */ private static final long serialVersionUID = 64 ...

  2. 【hihoCoder 1513】小Hi的烦恼

    http://hihocoder.com/problemset/problem/1513 h[j][i]记录第j个学科排名小于等于i的状态,bitset压位就可以了. #include<bits ...

  3. JZYZOJ1998 [bzoj3223] 文艺平衡树 splay 平衡树

    http://172.20.6.3/Problem_Show.asp?id=1998 平衡树区间翻转的板子,重新写一遍,给自己码一个板子. #include<iostream> #incl ...

  4. 谈HTTPS中间人攻击与证书校验(一)

    一.前言 随着安全的普及,https通信应用越发广泛,但是由于对https不熟悉导致开发人员频繁错误的使用https,例如最常见的是未校验https证书从而导致“中间人攻击”,并且由于修复方案也一直是 ...

  5. Centos 右上角面板里没有wired network图标的问题

    开了很多的网页查看解决这个问题,都不是很有效,最后很简单的改了下一个文件就ok了,自己记录下,以免忘记! 打入命令:sudo gedit /etc/NetworkManager/nm-system-s ...

  6. bzoj 1045

    确定初始状态(n与1直接谁给了谁几个),后面的就确定了,再根据总结出来的东西决定前面谁给谁几个最优. n=1000000!!! /*********************************** ...

  7. MySQL之分页问题解决

    最近遇到很多大分页的问题,服务器负载很高,SQL的执行时间也较长,非常的头痛.下面我们就说说分页问题的解决. 大家都知道对于mysql来说,select * from table where x=‘? ...

  8. Linux下swap分区多大才合适的问题探讨

    说明: 1.这个话题在每个Linux发行版中都各不相同,且在当下内存硬盘的时代下,再组个磁盘阵列之后速度相当. 2.我觉得硬盘大的情况下,有多大搞多大,我只要控制内存的使用率在99%时才使用swap, ...

  9. <摘录>linux signal 列表

    Linux 信号表   Linux支持POSIX标准信号和实时信号.下面给出Linux Signal的简表,详细细节可以查看man 7 signal. 默认动作的含义如下: 中止进程(Term) 忽略 ...

  10. Lower dc/dc-converter ripple by using optimum capacitor hookup

    Low-ripple-voltage positive-to-negative dc/dc converters find use in many of today's high- frequency ...