Grain boundaries in YBa$_2$Cu$_3$O$_{7-x}$ superconducting films are considered as Josephson junctions with a critical current density $j_c(x)$ alternating along the junction. A self-generated magnetic flux is treated both analytically and numerically for an almost periodic distribution of $j_c(x)$. We obtained a magnetic flux-pattern similar to the one which was recently observed experimentally.