[python] view plain copy
import sys
import os
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
class Notepad(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
openAction = QAction('Open', self)
openAction.setShortcut('Ctrl+O')
openAction.setStatusTip('Open a file')
openAction.triggered.connect(self.openFile)
closeAction = QAction('Close', self)
closeAction.setShortcut('Ctrl+Q')
closeAction.setStatusTip('Close Notepad')
closeAction.triggered.connect(self.close)
menubar = self.menuBar()
fileMenu = menubar.addMenu('&File')
fileMenu.addAction(openAction)
fileMenu.addAction(closeAction)
self.textEdit = 基迅樱QTextEdit(self)
self.textEdit.setFocus()
self.textEdit.setReadOnly(True)
self.resize(700, 800)
self.setWindowTitle('Notepad')
self.setCentralWidget(self.textEdit)
self.show()
def openFile(self):
filename, _ = QFileDialog.getOpenFileName(self, 'Open File', os.getenv('HOME'))
fh = '昌悔'
if QFile.exists(filename):
fh = QFile(filename)
if not fh.open(QFile.ReadOnly):
QtGui.qApp.quit()
data = fh.readAll()
codec = QTextCodec.codecForUtfText(data)
unistr = codec.toUnicode(data)
tmp = ('Notepad: %s' % filename)
self.setWindowTitle(tmp)
self.textEdit.setText(unistr)
def main():
app = 搏丛QApplication(sys.argv)
notepad = Notepad()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Qstring fileName =QFileDialog::getOpenFileName(this, tr("open file"), "", tr("Allfile(*.*)mp3file(*.mp3)"))说明:这样就会产生一个对话框梁滚嫌,和系统的资源管理器差不多的。返回的是你选择文件的绝对路径。
参数1:父窗口
参数2:对话框的标题
参数橡手3:默认的打开的位置,如”我的文档“等
参数4:备裤文件的过滤器,注意文件类型之间用 ;; 分开
QFileDialog dlg(this,"open image file")dlg.resize(400,300)
dlg.setAcceptMode(QFileDialog::AcceptOpen)
dlg.setNameFilter("Images (*.png *.bmp *.jpg)")
dlg.exec()
QStringList files = dlg.selectedFiles()
if(!files.isEmpty()){
QString file = files.at(0)
...
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)