ACTIVE_WINDOW = ''
def on_press(key):
f = open('text.txt', 'a')
if ACTIVE_WINDOW != GetWindowText(GetForegroundWindow()):
ACTIVE_WINDOW = GetWindowText(GetForegroundWindow())
f.write(ACTIVE_WINDOW)
print(ACTIVE_WINDOW)
UnboundLocalError: local variable 'ACTIVE_WINDOW' referenced before assignment