Python 使用PyQt5 完成选择文件或目录的对话框方法
更新时间:2019年06月27日 15:51:54 作者:qq_43311120
今天小编就为大家分享一篇Python 使用PyQt5 完成选择文件或目录的对话框方法。具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
如下所示:
import sys
from PyQt5.QtWidgets import QMainWindow,QApplication,QTextEdit,QAction,QFileDialog
from PyQt5.QtGui import QIcon
class Example(QMainWindow):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
self.textEdit = QTextEdit()
self.setCentralWidget(self.textEdit)
self.statusBar()
openfile = QAction(QIcon(r'C:\Users\Administrator\PycharmProjects\QT\picture\文件.jpg'),'open',self)
openfile.setShortcut("Ctrl + 0")
openfile.setStatusTip('open new file')
openfile.triggered.connect(self.showDialog)
menubar = self.menuBar()
filemune = menubar.addMenu('$File')
filemune.addAction(openfile)
self.setGeometry(300,300,300,300)
self.setWindowTitle('FIEL dialog')
self.show()
def showDialog(self):
fname = QFileDialog.getOpenFileName(self,'open file', '/')
if fname[0]:
try:
f = open(fname[0], 'r')
with f:
data = f.read()
self.textEdit.setText(data)
except:
self.textEdit.setText("打开文件失败,可能是文件内型错误")
if __name__ == "__main__":
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
以上这篇Python 使用PyQt5 完成选择文件或目录的对话框方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
利用Python Difflib库强大的文字比较功能快速轻松查重
这篇文章主要介绍了利用Python Difflib库强大的文字比较功能快速轻松查重实例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2024-01-01
使用setup.py安装python包和卸载python包的方法
这篇文章主要介绍了使用setup.py安装python包和卸载python包的方法,大家参考使用吧2013-11-11
python3中datetime库,time库以及pandas中的时间函数区别与详解
这篇文章主要介绍了python3中datetime库,time库以及pandas中的时间函数区别与详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2020-04-04


最新评论