Add the following to
my_first_addon/__init__.py in your add-ons folder:
# import the main window object (mw) from aqt from aqt import mw # import the "show info" tool from utils.py from aqt.utils import showInfo, qconnect # import all of the Qt GUI library from aqt.qt import * # We're going to add a menu item below. First we want to create a function to # be called when the menu item is activated. def testFunction() -> None: # get the number of cards in the current collection, which is stored in # the main window cardCount = mw.col.cardCount() # show a message box showInfo("Card count: %d" % cardCount) # create a new menu item, "test" action = QAction("test", mw) # set it to call testFunction when it's clicked qconnect(action.triggered, testFunction) # and add it to the tools menu mw.form.menuTools.addAction(action)
Restart Anki, and you should find a 'test' item in the tools menu. Running it will display a dialog with the card count.
If you make a mistake when entering in the plugin, Anki will show an error message on startup indicating where the problem is.