Hi, everyone!
I’m trying to make an Anki Addon that uses trackpad swipes on Anki Desktop on MacOS to advance a card and rate it. The code works fine in Python outside of Anki. When I add it to an Anki Addon, I just get the error:
Quartz path: /Users/username/Library/Application Support/Anki2/addons21/swipe_gestures/pyobjc_framework_Quartz
Failed to import Quartz: cannot import name ‘_objc’ from partially initialized module ‘objc’ (most likely due to a circular import) (/Users/username/Library/Application Support/Anki2/addons21/swipe_gestures/pyobjc_framework_Quartz/objc/init.py)
Here’s the code.
import os
import sys
# Add local library paths
addon_path = "/Users/username/Library/Application Support/Anki2/addons21/swipe_gestures"
pynput_path = os.path.join(addon_path, 'pynput')
# Ensure the paths are inserted at the beginning to avoid conflicts
sys.path.insert(0, pynput_path)
# Debugging: Print paths for verification
print(f"Addon path: {addon_path}")
print(f"pynput path: {pynput_path}")
# Attempt to import pynput
try:
import pynput
print("pynput imported successfully")
except ImportError as e:
print(f"Failed to import pynput: {e}")
from pynput import mouse
# Function to handle scroll events
def handle_scroll(x, y, dx, dy):
print("Scroll event detected:")
if dx != 0:
if dx > 0:
print("Scroll left")
else:
print("Scroll right")
if dy != 0:
if dy > 0:
print("Scroll up")
else:
print("Scroll down")
# Create a listener to capture scroll events
def create_event_listener():
print("Creating event listener...")
with mouse.Listener(
on_scroll=handle_scroll) as listener:
listener.join()
if __name__ == "__main__":
create_event_listener()