@asanka424 Thanks a lot! Your suggestion works. After moving the connect line into the constructor, I also needed to change this:
filename = QFileDialog.getSaveFileName(self, "Select output file ","", '*.txt')to this:
filename = QFileDialog.getSaveFileName(None, "Select output file ","", '*.txt')