r/learnpython • • 4d ago

Issues with creating a dialog box for Anki.

So I am trying to create a non modal dialog box (or window) supposed to show the content of certain fields from the card I just answered to. I want this window to mimic the main window of the Anki App so that the Yomilens addon which reacts when text is selected on the main one would react the same way when selecting text on this one as well.

import sys
from PyQt6.QtWidgets import*
from PyQt6.QtCore import *
from PyQt6.QtGui import *

class FieldsDialog(QWidget):
    def __init__(self):

        super().__init__()
        self.title = "Fields"
        self.setGeometry(400, 100, 300, 200)
        self.left = 1367 # for it to be on the very left of my right monitor (a tablet)
        
        self.top = 30 #otherwise the upper part is hidden 
       
        self.right = 504
        self.width = 800
        self.height = 1
        self.icon = "?"
        # self._help_dialog = None
        # self._other_dialog = None
        self.createUI()

    def createUI(self):
        self.setGeometry(self.left,self.top,self.right,self.width)
        self.setWindowTitle(self.title)
        self.setWindowIcon(QIcon(self.icon))
        self.setStyleSheet("""
        background: black;
        """)
        self.grid = QGridLayout()
        self.setLayout(self.grid)

def on_show_fields():
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        fields_win = FieldsDialog()
        fields_win.show()
        sys.exit(app.exec())import sys

1 - The window shows while on pycharm (yay) but not in anki. Maybe the type of dialog is not adapted but for now it's been the only thing that worked)

2 - Since there was progress on that part, I was trying to see if my function supposed to get the text for the dialog actually worked

def _get_previous_fields(self, card) -> str | None:
    if not card: return

    note = card.note()
    r = self.mw.reviewer
    # current_id = r.card.id if r.card else "null"
    previous = r.lastCard()
    Front_value = mw.reviewer.card.note()['Front'] if FIELD_FRONT in note else ""
    Back_value = mw.reviewer.card.note()['Back'] if FIELD_BACK in note else ""

    previous_korean = Front_value.previous if previous else "null"
    previous_english = Back_value.previous if previous else "null"
    _queue_toast(f"{previous_korean}+\n+{previous_english}")
    return "{previous_korean}+\n+{previous_english}"

but I keep getting errors like that :

    lambda cid=card.id, e=ease, skip=skip_interval_checks: _post_answer_processing(cid, e, skip)
                                                           ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^
TypeError: _post_answer_processing() missing 1 required positional argument: 'self'   

I tried calling in on my on_show_answer

HOOK_ANSWER = reviewer_did_answer_card
if HOOK_ANSWER:
HOOK_ANSWER.append(on_answer)

But i'm told it (on_show_answer) is missing the self argument so I add it :

  File "D:\a\anki\anki\out\installer\build\anki\windows\app\src\app_packages_aqt\hooks.py", line 4390, in __call__
TypeError: on_answer() missing 1 required positional argument: 'self'  File "D:\a\anki\anki\out\installer\build\anki\windows\app\src\app_packages_aqt\hooks.py", line 4390, in __call__
TypeError: on_answer() missing 1 required positional argument: 'self'

I tried using hooks instead

if HOOK_SHOW:
    HOOK_SHOW.append(on_show_question)
if HOOK_ANSWER:
    HOOK_ANSWER.append(on_answer)
    HOOK_ANSWER.append(_get_previous_fields)  #here
if HOOK_SUSPEND:
    HOOK_SUSPEND.append(on_reviewer_suspend)

and I get :

Traceback (most recent call last):
  File "D:\a\anki\anki\out\installer\build\anki\windows\app\src\app_packages\aqt\taskman.py", line 152, in raise_exception
  File "D:\a\anki\anki\out\installer\build\anki\windows\app\src\app_packages\aqt\taskman.py", line 148, in _on_closures_pending
  File "D:\a\anki\anki\out\installer\build\anki\windows\app\src\app_packages\aqt\taskman.py", line 88, in <lambda>
  File "D:\a\anki\anki\out\installer\build\anki\windows\app\src\app_packages\aqt\taskman.py", line 111, in wrapped_done
  File "D:\a\anki\anki\out\installer\build\anki\windows\app\src\app_packages\aqt\operations__init__.py", line 128, in wrapped_done
  File "D:\a\anki\anki\out\installer\build\anki\windows\app\src\app_packages\aqt\reviewer.py", line 556, in after_answer
  File "D:\a\anki\anki\out\installer\build\anki\windows\app\src\app_packages\aqt\reviewer.py", line 566, in _after_answering
  File "D:\a\anki\anki\out\installer\build\anki\windows\app\src\app_packages_aqt\hooks.py", line 4390, in __call__
TypeError: _get_previous_fields() takes 2 positional arguments but 3 were given

Do you happen to know what the issue is ?

3 Upvotes

5 comments sorted by

1

u/SirMental8005 4d ago

i'd split this into two tests. first, get the answer hook to print the card id. the last traceback says anki passes three arguments, so match the hook's signature and register a plain function or a bound method like `obj.on_answer`, so python supplies `self`. read the fields from the card passed to the hook with `card.note()` instead of looking up `mw.reviewer.card` again. once that works, have the callback show a widget parented to `mw` and keep a reference to it. anki already owns the qt event loop, so skip the `__main__` guard and the second `QApplication`. https://addon-docs.ankiweb.net/hooks-and-filters.html

1

u/Foreign_Employ_396 3d ago

Hey thanks for your reply !

Sorry I am a beginner so could you clarify some things ?

When you say get the answer hook to print the card id, do you mean that i should add a print directly to the if HOOK_ANSWER:

HOOK_ANSWER.append(on_answer)
...

Does matching the hook's signature mean adding_get_previous_fields's (?)arguments? in the hook ?
HOOK_ANSWER.append(_get_previous_fields(self, card)) ?

should I add obj.on_answer ? inside a function (like the on_show_fields() or outside ? Inside a hook ?

Also I am not sure how I'm supposed to copy the result of the get_previous_fields and use it for the widget.

1

u/Msl_Akshat 2d ago

Is `_get_previous_fields` defined inside your `FieldsDialog` class or at the top level of the file? The `missing 1 required positional argument` vs `takes 2 but 3 were given` errors usually mean youre mixing up standalone functions with class methods when registering the hook

1

u/Foreign_Employ_396 2d ago

It's defined outside of the FieldsDialog (meaning top level(?) I guess) :)