In this paper, a consensus problem is first investigated for piecewise time-varying multi-agent systems with switching topologies. Due to the piecewise time-varying characteristics of system matrix, it is challenging to design an appropriate controller to stabilize the error state within each piecewise time period. To overcome this difficulty, a piecewise time-varying Lyapunov function (PTVLF) approach is proposed to analyze the piecewise time-varying systems. Then, a useful lemma guaranteeing the negative definiteness of matrix polynomials is first derived, which is utilized to prove the negative definiteness of the derivative for the PTVLF. Based on this, a novel controller with time-varying gain is presented to stabilize the error state within each piecewise time period. Then, by selecting the dwell time of each topology larger than a positive threshold, the overall consensus of such systems is guaranteed. Finally, a numerical simulation is shown to illustrate the theoretical results.