前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >PyQt5--QFileDiaglog

PyQt5--QFileDiaglog

作者头像
py3study
发布2020-01-19 11:21:19
4150
发布2020-01-19 11:21:19
举报
文章被收录于专栏:python3python3
代码语言:javascript
复制
 1 # -*- coding:utf-8 -*-
 2 '''
 3 Created on Sep 17, 2018
 4 
 5 @author: SaShuangYiBing
 6 
 7 Comment: 
 8 '''
 9 import sys
10 from PyQt5.QtGui import QIcon
11 from PyQt5.QtWidgets import QApplication,QMainWindow,QFileDialog,QTextEdit,QAction
12 
13 class New_test(QMainWindow):
14     def __init__(self):
15         super().__init__()
16         self.initUI()
17         
18     def initUI(self):
19         self.textEdit = QTextEdit()
20         self.setCentralWidget(self.textEdit)
21         self.statusBar()
22         
23         openFile = QAction(QIcon('exit.png'),'Open',self)
24         openFile.setShortcut('Crtl+O')
25         openFile.setStatusTip('Open new file')
26         openFile.triggered.connect(self.showdiaglog)
27         
28         menubar = self.menuBar()
29         fileMenu = menubar.addMenu('&File')
30         fileMenu.addAction(openFile)
31         
32         self.setGeometry(300,300,350,300)
33         self.setWindowTitle('File Diaglog')
34         self.show()
35         
36     def showdiaglog(self):
37         try:
38             fname = QFileDialog.getOpenFileName(self,'Open file','/home')
39             
40             if fname[0]:
41                 with open(fname[0],'r',encoding='utf-8') as f:   # 在Python3中使用open时后面需要带上编码方式,否则易引起打开非utf-8编码字符时会报错
42                     data = f.read()
43                     self.textEdit.setText(data)
44         except Exception as e:
45             print (e)
46                 
47 if __name__ == '__main__':
48     app = QApplication(sys.argv)
49     ex = New_test()
50     sys.exit(app.exec_())
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-03-12 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档