Solution: Let $f: K \to \mathbb{C} $ be a non-constant analytic function on $K$, where $K$ is a compact set. We will show that $\text{Re } f$ and $\text{Im } f$ attain their maxima and minima on the boundary of $K$. Consider the functions $e^{f(z)}$ and $e^{-\iota f(z)}$. Since $f$ is non-constant analytic on $K$, the functions $e^{f(z)}$ and $e^{-\iota f(z)}$ must be non-constant analytic on $K$. Also note that both functions are nonzero on $K$. Thus we can apply
Maximum Modulus Theorem and
Minimum Modulus Theorem (being nonzero is needed for minimum modulus theorem) to $e^{f(z)}$ and $e^{-\iota f(z)}$ to conclude that they attain their maxima and minima on the boundary of $\partial K$. Let $z_1, z_2 \in \partial K$ be such that for any $z \in K$
\begin{align*}
& \left\vert e^{f(z)} \right\vert \leq \left\vert e^{f(z_1)} \right\vert \quad \text{ and } \quad \left\vert e^{f(z)} \right\vert \geq \left\vert e^{f(z_2)} \right\vert \\
\implies & e^{\text{Re }f(z)} \leq e^{\text{Re }f(z_1)} \quad \text{ and } \quad e^{\text{Re } f(z)} \geq \ e^{\text{Re } f(z_2)}
\end{align*}
Combining with the fact that $e^x$ is an increasing function on $R$, we conclude that $\text{Re } f$ attains its maxima and minima on the boundary of $K$. Similarly, since
\[
\left\vert e^{-\iota f(z)} \right\vert = e^{\text{Im } f(z)},
\]
we can conclude that $\text{Im } f$ attains its maxima and minima on the boundary of $K$.