I want to create a floating window in Anki that displays the contents of a specific field, such as ‘Note,’ during card review. I want this because i have fairly large text in my cards, i want to see where this card belongs to. For example : i want this special field to show chapter name or historic sequence,…mind map, some repeated formula of this chapter… etc. I am a complete noob to coding, following is a code generated by Chatgpt. It’s able to create a pop window but it is always showing no card being reviewed.
Any help is appreciated!!
The pop up window should be something like this
Version 2.1.66 (70506aeb)
Python 3.9.15 Qt 6.5.0 PyQt 6.5.0
The following is a code from chatgpt but isn’t working.
from aqt import mw, gui_hooks
from anki.hooks import addHook
from PyQt6.QtWidgets import QDockWidget, QLabel, QVBoxLayout, QWidget
from PyQt6.QtCore import Qt
# ... (rest of your code)
# Function to create and display the floating QDockWidget
def display_field_contents_on_home_screen():
global target_field_name
# Check if a card is being actively reviewed
if mw.reviewer and mw.reviewer.card:
# Get the current card being reviewed
card_id = mw.reviewer.card.id
card = mw.col.getCard(card_id)
# Check if the target field exists on the current card
if target_field_name in card.keys():
field_contents = card[Note]
else:
field_contents = "Field not found on this card."
else:
field_contents = "No card being reviewed."
# Create a QDockWidget and set it to be floatable
dock = QtWidgets.QDockWidget()
dock.setFloating(True)
# Create a label to display the field contents
label = QtWidgets.QLabel(field_contents)
label.setAlignment(QtCore.Qt.AlignmentFlag.AlignTop)
# Set up the layout and add the label
layout = QtWidgets.QVBoxLayout()
layout.addWidget(label)
# Create a widget to hold the layout and add it to the dock
widget = QtWidgets.QWidget()
widget.setLayout(layout)
dock.setWidget(widget)
# Add the dock to the main window
mw.addDockWidget(QtCore.Qt.DockWidgetArea(QtCore.Qt.DockWidgetArea.RightDockWidgetArea), dock)
# Register a hook to call the display_field_contents_on_home_screen function
# when the Anki home screen is displayed
addHook("profileLoaded", display_field_contents_on_home_screen)
Debug info:
Anki 2.1.66 (70506aeb) Python 3.9.15 Qt 6.5.0 PyQt 6.5.0
Platform: macOS-14.1.1-arm64-arm-64bit
Flags: frz=True ao=True sv=3
Add-ons, last update check: 2023-11-27 20:49:48
Add-ons possibly involved: <U+2068>931346754<U+2069>
Caught exception:
Traceback (most recent call last):
File "aqt.progress", line 118, in handler
File "aqt.main", line 217, in on_window_init
File "aqt.main", line 316, in setupProfile
File "aqt.main", line 495, in loadProfile
File "_aqt.hooks", line 3983, in __call__
File "anki.hooks", line 34, in runHook
File "/Users/sai/Library/Application Support/Anki2/addons21/931346754/__init__.py", line 28, in display_field_contents_on_home_screen
dock = QtWidgets.QDockWidget()
NameError: name 'QtWidgets' is not defined