Issue #21695: Catch AttributeError created when user closes grep output window
while still being written to. With no console, this closed Idle.
Also add missing import and a few other changes.