The 't Hooft partition function $Z_{\mathrm{tH}}[E_i;B_{ij}]$ is a discrete Fourier transform of Yang--Mills partition functions in background $\mathbb{Z}_N$ 2-form gauge fields and encodes information on confinement, Higgs, Coulomb and oblique-confining phases.
We report a direct Monte Carlo strategy to measure $Z_{\mathrm{tH}}$ without reweighting, by extending hybrid Monte Carlo to include dynamical updates of the background flux variables.
As a first application we measure all flux sectors of four-dimensional $SU(2)$ lattice Yang--Mills on $T^4$ and observe the characteristic ``light/heavy'' behavior expected in the confining phase, together with the shift implied by the Witten effect at $\theta=2\pi$.
We also present a preliminary finite-temperature study and discuss outstanding issues on thermalization and separability between different flux sectors.

