Example 26: Initialize the xmpp object to None so the property always exists.

This is not functionally different, but it will allow us to keep the upcoming code neat and tidy.
Example 27: Add a disconnect_xmpp method to clean up.

This will be called both on window closing and if login is unsuccessful.
Example 28: Hook up the on_stop handler to call the disconnect function.

It couldn't really be any simpler.
Example 29: If login fails, call disconnect_xmpp().

If it doesn't fail, leave it connected. The uglyness has been refactored into another method.