Solved: Automatically clearing a text editor after user reviewed card

Hey there!
I am currently working on an add-on. It is mainly a text-editor and I want to implement the function to automatically clear the text editor (delete all the text that is in the text editor) after the user reviewed a card and a specific checkbox is checked.

My code for this function:

check = QCheckBox("Auto Clear")
        check.stateChanged.connect(lambda: toggle_auto_clear(check))
        button_layout.addWidget(check)

def toggle_auto_clear(checkbox):
    if not checkbox.isChecked():
        checkbox.setText("Auto Clear OFF")
    else:
        checkbox.setText("Auto Clear ON")
        auto_clear_func()

def auto_clear_func(reviewer: Reviewer, card: Card, ease: Literal[1, 2, 3, 4]) -> None:
    global text_editor, numeric_list_active
    text_editor.clear()
    numeric_list_active = False
    text_editor.setFocus()

gui_hooks.reviewer_did_answer_card.append(auto_clear_func)

But if I check the checkbox I encounter the following error:

Debuginformationen:
Anki 2.1.65 (aa9a734f) Python 3.9.15 Qt 6.4.3 PyQt 6.4.0
Platform: Windows-10-10.0.19045
Flags: frz=True ao=True sv=3
Add-ons, last update check: 2023-10-05 10:07:43

Caught exception:
Traceback (most recent call last):
File “C:\Users\Joystick6123\AppData\Roaming\Anki2\addons21\Tab Note 2_init_.py”, line 67, in
check.stateChanged.connect(lambda: toggle_auto_clear(check))
File “C:\Users\Joystick6123\AppData\Roaming\Anki2\addons21\Tab Note 2_init_.py”, line 94, in toggle_auto_clear
auto_clear_func()
TypeError: auto_clear_func() missing 3 required positional arguments: ‘reviewer’, ‘card’, and ‘ease’

I would appreciate any help. Thank you in advance!

I was able to solve the problem in the meantime. For anyone who is interested my new code:

check = QCheckBox("Auto Clear")
        check.stateChanged.connect(lambda: toggle_auto_clear(check))
        button_layout.addWidget(check)

def toggle_auto_clear(checkbox):
    if not checkbox.isChecked():
        checkbox.setText("Auto Clear OFF")
    else:
        checkbox.setText("Auto Clear ON")
        reviewer = Reviewer
        card = Card
        ease = Literal[1, 2, 3, 4]
        auto_clear_func(reviewer, card, ease)

def auto_clear_func(reviewer: Reviewer, card: Card, ease: Literal[1, 2, 3, 4]) -> None:
    global text_editor, numeric_list_active
    text_editor.clear()
    numeric_list_active = False
    text_editor.setFocus()

gui_hooks.reviewer_did_answer_card.append(auto_clear_func)


If you don’t need to use any parameters of the hook, you can write the code more cleanly like this:

check = QCheckBox("Auto Clear")
        check.stateChanged.connect(lambda: toggle_auto_clear(check))
        button_layout.addWidget(check)

def toggle_auto_clear(checkbox):
    if not checkbox.isChecked():
        checkbox.setText("Auto Clear OFF")
    else:
        checkbox.setText("Auto Clear ON")
        auto_clear_func()

def auto_clear_func() -> None:
    global text_editor, numeric_list_active
    text_editor.clear()
    numeric_list_active = False
    text_editor.setFocus()

gui_hooks.reviewer_did_answer_card.append(lambda *args, **kwargs: auto_clear_func())
1 Like

Thanks for your reply abdo!
I never coded anything before, but I really wanted this feature in Anki so I just tried and got some help from chatgpt here and there.
So I really appreciate any feedback, just like yours!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.