Help needed: Congratulations screen as a hook

Hi,

I’m creating my first add-on, and I need some help, please.

I want to have a file executed every time I finish revising a deck and get to the congratulations screen. Furthermore, I want that the file opens, and then window focus immediately goes back to the Anki window.

For now, I have this working:

import os
from aqt import gui_hooks
import win32gui
import time

def myfunc():
  window_hwnd = win32gui.GetForegroundWindow()
  os.startfile("C:\Program Files (x86)\Hourglass\Hourglass2.exe.lnk")
  time.sleep(0.25)
  win32gui.SetForegroundWindow(window_hwnd)

gui_hooks.reviewer_will_end.append(myfunc)

The only problem is that it also activates when I go directly from the review screen to the decks screen.

I tried to get inspiration from the Confetti add-on (link to the relevant code line), because it does have the behavior that I want. But, it’s too difficult for me to understand. It uses _show_finished_screen, but I don’t know how to use it.

Please, help.
Thanks,

Try the state_did_change hook instead:

import os
from aqt import gui_hooks
import win32gui
import time


def myfunc(new_state, old_state):
    if old_state == "review" and new_state == "overview":
        window_hwnd = win32gui.GetForegroundWindow()
        os.startfile("C:\Program Files (x86)\Hourglass\Hourglass2.exe.lnk")
        time.sleep(0.25)
        win32gui.SetForegroundWindow(window_hwnd)


gui_hooks.state_did_change.append(myfunc)

4 Likes

Thanks! It works.

For future reference:

This is the state_did_change hook: https://github.com/ankitects/anki/blob/35be9a70dac42f67cc9153b3ab1bd2faec0f76e8/qt/tools/genhooks_gui.py#L588

And the states can be found at: https://github.com/ankitects/anki/blob/35be9a70dac42f67cc9153b3ab1bd2faec0f76e8/qt/aqt/main.py#L97

The possible states are: “startup”, “deckBrowser”, “overview”, “review”, “resetRequired”, “profileManager”