I must say I am still very new to the PyQt4 module, so this question might be painfully obvious. However, I have spent a good while trying to solve this particular problem and have run out of ideas.
Here is my code thus far:
...
self.btn = QtGui.QPushButton('Save Text', self)
self.btn.move(20, 20)
self.le = QtGui.QLineEdit(self)
self.le.move(130, 22)
self.btn.clicked.connect(self.save_text)
...
And then the function:
def save_text(self):
text, ok = QtGui.QInputDialog.getText(self, 'Input', 'Type text:')
filename = QtGui.QFileDialog.getSaveFileName(self, 'Save File', '.')
fname = open(filename, 'w')
fname.write(self.le.setText(str(text)))
fname.close()
This code works fine, but I am trying to refine it. What I am trying to do is save the text in the main window's input field (the self.le) directly to a file. Currently, whenever the Save Text button is pressed, it opens a new dialog and the user enters the text to save in the new dialog. Essentially, I would like to be able to use getText with the self.le and save that to the text variable, but I have been unable to do so. Is there any direct way to store text from the self.le to the text variable via a button click? Or save it directly to the file? I am running Python 2.7.