Rewrite display mode toggling in Python