To get a filename using open file dialog, use:
filename = QtGui.QFileDialog.getOpenFileName(self, 'Open File', '.')
filename = QtGui.QFileDialog.getOpenFileName(self, 'Open File', '.')
fname = open(filename)
data = fname.read()
self.textEdit.setText(data)fname.close()
While to save data to file, from example from textEdit:
filename = QtGui.QFileDialog.getSaveFileName(self, 'Save File', '.')fname = open(filename, 'w')
fname.write(textEdit.toPlainText())
fname.close()
i am getting this Error :\
ReplyDeleteunhandled TypeError
"QFileDialog.getOpenFileName(QWidget parent=None, QString caption=QString(), QString directory=QString(), QString filter=QString(), QString selectedFilter=None, QFileDialog.Options options=0): argument 1 has unexpected type 'Ui_Dialog'"
i have the same problem as well. i used QT designer to layout but i got the same error liked you mentioned when i defined the pushButton. how to fix this??
Deletehey it worked when i typed in the first argument 'Dialog':
ReplyDeletefilename = QtGui.QFileDialog.getOpenFileName(Dialog, 'Open File', '.')
not 'self'
thanks
Deletei got same error but solved because of ur reply
This comment has been removed by the author.
ReplyDeleteThis works for an existing file in directory, but what about saving to a new file?
ReplyDeleteNever mind, :) , didn't notice the field at the top for typing in file name.
ReplyDeletedef open_file(self):
ReplyDeletefilename = QtGui.QFileDialog.getOpenFileName(None, 'Open File', '.')
fname = open(filename)
data = fname.read()
self.textEdit.setText(data)
fname.close()