[guided]Fix an integer $q \geq 1$, and write $Q := \{1,\dots,q\}$ for the set of available colors. The set of all colorings is
\begin{align*}
\mathcal{C} := \{f: V \to Q\}.
\end{align*}
For an edge $e \in E$, let $u_e,v_e \in V$ be its endpoints, and define
\begin{align*}
B_e := \{f \in \mathcal{C} : f(u_e)=f(v_e)\}.
\end{align*}
Thus $B_e$ is the set of colorings for which the edge $e$ violates properness. Since the graph has no loops, an edge is properly colored exactly when its two endpoints receive different colors, so a coloring is proper exactly when it lies in none of the sets $B_e$.
We now apply the finite [Inclusion-Exclusion Principle](/theorems/751) to the finite family $(B_e)_{e \in E}$. The family is finite because $E$ is finite by hypothesis. This gives
\begin{align*}
P_G(q)=\sum_{A \subset E}(-1)^{|A|}\left|\bigcap_{e \in A}B_e\right|.
\end{align*}
It remains to count the intersection for a fixed subset $A \subset E$. Let $c(V,A)$ be the number of connected components of the spanning subgraph $(V,A)$. A coloring lies in $\bigcap_{e \in A}B_e$ precisely when every edge in $A$ has equal-colored endpoints. Along a path in $(V,A)$, this equality propagates edge by edge, so the coloring is constant on each connected component of $(V,A)$. Conversely, if a coloring is constant on every connected component of $(V,A)$, then every edge in $A$ has equal-colored endpoints, so the coloring belongs to $\bigcap_{e \in A}B_e$.
Thus the only choices are the colors assigned to the connected components of $(V,A)$. There are $c(V,A)$ components, and each can be assigned any of the $q$ colors independently. Hence
\begin{align*}
\left|\bigcap_{e \in A}B_e\right|=q^{c(V,A)}.
\end{align*}
Substituting this into inclusion-exclusion yields
\begin{align*}
P_G(q)=\sum_{A \subset E}(-1)^{|A|}q^{c(V,A)}.
\end{align*}[/guided]