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
QtGui.QFileDialog的更多相关文章
- PyQt4文件对话框QFileDialog
文件对话框允许用户选择文件或文件夹,被选择的文件可进行读或写操作. #!/usr/bin/python # -*- coding: utf-8 -*- import sys from PyQt4 im ...
- Python应用03 使用PyQT制作视频播放器
作者:Vamei 出处:http://www.cnblogs.com/vamei 严禁任何形式转载. 最近研究了Python的两个GUI包,Tkinter和PyQT.这两个GUI包的底层分别是Tcl/ ...
- PyQt4入门学习笔记(五)
PyQt4里的对话框 对话框是大多数GUI应用中不可分割的一部分.一个对话框是两者或多者的会话.在GUI内,对话框是应用向人说话的方式.一个对话框可以用来输入数据,修改数据,改变应用设置等等. QtG ...
- Pyqt 获取打包二进制文件中的资源
记得有一次打开一个单独exe程序,点击btn中的一个帮助说明按钮,在同级目录下就多出一个help.chm 文件并自动打开. 那这个exe肯定是把help.chm 打包到exe中,当我触发“帮助”按钮的 ...
- Pyqt 屏幕截图工具
从Pyqt的examples中看到一段截图代码, (路径:examplas\desktop\screenshot.py) 所以想自己UI下界面,手动练习下 通过UI生成的: Screenshot.py ...
- Pyqt Smtplib实现Qthread多线程发送邮件
一. smtplib 的介绍 smtplib.SMTP([host[, port[, local_hostname[, timeout]]]]) SMTP类构造函数,表示与SMTP服务器之间的连接 ...
- Pyqt 音视频播放器
在寻找如何使用Pyqt做一个播放器时首先找到的是openCV2 openCV2 貌似太强大了,各种关于图像处理的事情它都能完成,如 读取摄像头.图像识别.人脸识别. 图像灰度处理 . 播放视频等,强 ...
- PyQT制作视频播放器
Python应用03 使用PyQT制作视频播放器 作者:Vamei 出处:http://www.cnblogs.com/vamei 严禁任何形式转载. 最近研究了Python的两个GUI包,Tki ...
- PyQt4学习记录之事件和信号
事件是任何 GUI程序中很重要的部分.所有 Python GUI 应用都是事件驱动的.一个应用对其生命期产生的不同的事件类型做出反应.事件是主要由应用的用户产生.但是,也可以通过其他方法产生,比如,网 ...
随机推荐
- bzoj 2483: Pku2279 Mr. Young's Picture Permutations -- 钩子公式
2483: Pku2279 Mr. Young's Picture Permutations Time Limit: 1 Sec Memory Limit: 128 MB Description ...
- 关于收到谷歌邮件 Googlebot can't access your site 的解决方法
最近一段时间一直都收到谷歌的邮件,而且“新锐工作室”的关键字在谷歌收录及排名都没有了.下面图为谷歌蜘蛛无法抓取网站的截图,如果你在谷歌网管工具里收到类似消息,说明也中招了.[Webmaster Too ...
- Spring EL hello world实例
Spring EL与OGNL和JSF EL相似,计算评估或在bean创建时执行.此外,所有的Spring表达式都可以通过XML或注解. 在本教程中,我们将学习如何使用Spring表达式语言(SpEL) ...
- 字符串转base64,base64转字符串
[JavaScript原生提供两个Base64相关方法] btoa():字符串或二进制值转为Base64编码 atob():Base64编码转为原来的编码 备注:利用这两个原生方法,我们来封装一下,标 ...
- mysql世界国家省市地区的信息中英文
项目的主要业务是在国外, 所以以前的省市数据不仅不全, 而且没有英文翻译, 幸好在网上找到一个符合要求的数据 感谢: https://github.com/moolighty/geo
- CDC之CreateCompatibleDC与BitBlt
CreateCompatibleDC 创建一个与指定设备一致的内存设备描写叙述表. HDC CreateCompatibleDC(HDC hdc //设备描写叙述表句柄); 參数 hdc 现有的设备描 ...
- SSM框架中出现的几种注解的理解
转自IT·达人原文 Spring5:@Autowired注解.@Resource注解和@Service注解,有删改. 传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物 ...
- Oracle经典查询案例
1.创建一个学生管理数据库,名称为student 2.最少包括一下三张表 S (SNO,SNAME)学生关系.SNO 为学号,SNAME 为姓名 C (CNO,CNAME,CTEACHER) 课程关 ...
- xheditor
完整按钮表 |:分隔符 /:强制换行 Cut:剪切 Copy:复制 Paste:粘贴 Pastetext:文本粘贴 Blocktag:段落标签 Fontface:字体 FontSize:字体大小 Bo ...
- 更改Thunderbird的默认语言
使用的Thunderbird Poratable版本是英文的,可以用以下方式修改为中文界面: 1.下载中文语言包 在官方网站的https://addons.mozilla.org/en-US/thun ...