Return value from button click

Store the file_path in a class level variable and update that value in your button click method.

self.file_path = None
self.Button_open.clicked.connect(self.OpenTextFile)

And then,

def OpenTextFile(self):
    dialog = QtGui.QFileDialog()
    dialog.setWindowTitle("Choose a file to open")
    dialog.setFileMode(QtGui.QFileDialog.ExistingFile)
    dialog.setNameFilter("Text (*.txt);; All files (*.*)")
    dialog.setViewMode(QtGui.QFileDialog.Detail)

    filename = QtCore.QStringList()

    if(dialog.exec_()):
        file_name = dialog.selectedFiles()
    plain_text = open(file_name[0]).read()
    self.Editor.setPlainText(plain_text)
    self.file_path = str(file_name[0])

Also your

self.Button_save.clicked.connect(self.SaveTextFile(file_path))

should be

self.Button_save.clicked.connect(self.SaveTextFile)

and in your save click method

def SaveTextFile(self):
    save(self.file_path)     # Your code to save file

Leave a Comment