Anki 插件开发:创建编辑器按钮时出错
Anki addon development: Error creating an editor button
我正在尝试在 Anki 工具栏中添加一个按钮。
成功了!
但是,当我点击按钮时,它显示了一条错误消息,似乎是一个事件侦听器。
我需要做什么?
我尝试实现onBridgeCmd,但是它不起作用(我不知道如何实现)。
def blur_text(editor):
showdialogtitle(editor.currentField, 'anki')
def addBlurButton(buttons,editor):
editor._links['blur_text'] = blur_text
buttons += [editor._addButton(ICON_PATH, "blur","Blur Text")]
return buttons
addHook("setupEditorButtons",addBlurButton)
错误:
Debug info: Anki 2.1.35 (4e12c7ef) Python 3.8.5 Qt 5.12.2 PyQt 5.12.1
Platform: Linux Flags: frz=False ao=True sv=2 Add-ons, last update
check: 2021-01-21 14:27:50 Add-ons possibly involved: Anki IPA,
blur_text, search_in_dictionaries
Caught exception: Traceback (most recent call last): File
"/snap/anki-woodrow/27/lib/python3.8/site-packages/aqt/webview.py",
line 37, in cmd
return json.dumps(self.onCmd(str)) File "/snap/anki-woodrow/27/lib/python3.8/site-packages/aqt/webview.py",
line 123, in _onCmd
return self._onBridgeCmd(str) File "/snap/anki-woodrow/27/lib/python3.8/site-packages/aqt/webview.py",
line 547, in _onBridgeCmd
return self.onBridgeCmd(cmd) File "", line 2, in onBridgeCmd File
"/snap/anki-woodrow/27/lib/python3.8/site-packages/anki/hooks.py",
line 638, in decorator_wrapper
return repl(*args, **kwargs) File "/snap/anki-woodrow/27/lib/python3.8/site-packages/anki/hooks.py",
line 635, in repl
return new(_old=old, *args, **kwargs) File "/home/element/snap/anki-woodrow/27/.local/share/Anki2/addons21/search_in_dictionaries/init.py",
line 230, in on_bridge_cmd
_old(editor, command) File "", line 2, in onBridgeCmd File
"/snap/anki-woodrow/27/lib/python3.8/site-packages/anki/hooks.py",
line 638, in decorator_wrapper
return repl(*args, **kwargs) File "/snap/anki-woodrow/27/lib/python3.8/site-packages/anki/hooks.py",
line 635, in repl
return new(_old=old, *args, **kwargs) File "/home/element/snap/anki-woodrow/27/.local/share/Anki2/addons21/blur_text/init.py",
line 59, in on_bridge_cmd
_old(editor, command) File "", line 2, in onBridgeCmd File
"/snap/anki-woodrow/27/lib/python3.8/site-packages/anki/hooks.py",
line 638, in decorator_wrapper
return repl(*args, **kwargs) File "/snap/anki-woodrow/27/lib/python3.8/site-packages/anki/hooks.py",
line 635, in repl
return new(_old=old, *args, **kwargs) File "/home/element/snap/anki-woodrow/27/.local/share/Anki2/addons21/799647424/main.py",
line 183, in on_bridge_cmd
_old(editor, command) File "/snap/anki-woodrow/27/lib/python3.8/site-packages/aqt/editor.py",
line 385, in onBridgeCmd
(type, ord, nid, txt) = cmd.split(":", 3) ValueError: not enough values to unpack (expected 4, got 1)
问题是按钮的事件名称,我需要更改它,出于某种原因,“blur_text”在 Anki 系统中使用。
def contrast_text(editor: Editor) -> None:
...
def add_contrast_button(buttons: List[str], editor: Editor) -> List[str]:
button = editor.addButton(ICON_PATH, "contrast", contrast_text)
buttons.append(button)
return buttons
addHook("setupEditorButtons", add_contrast_button)
我正在尝试在 Anki 工具栏中添加一个按钮。
成功了!
但是,当我点击按钮时,它显示了一条错误消息,似乎是一个事件侦听器。
我需要做什么?
我尝试实现onBridgeCmd,但是它不起作用(我不知道如何实现)。
def blur_text(editor):
showdialogtitle(editor.currentField, 'anki')
def addBlurButton(buttons,editor):
editor._links['blur_text'] = blur_text
buttons += [editor._addButton(ICON_PATH, "blur","Blur Text")]
return buttons
addHook("setupEditorButtons",addBlurButton)
错误:
Debug info: Anki 2.1.35 (4e12c7ef) Python 3.8.5 Qt 5.12.2 PyQt 5.12.1 Platform: Linux Flags: frz=False ao=True sv=2 Add-ons, last update check: 2021-01-21 14:27:50 Add-ons possibly involved: Anki IPA, blur_text, search_in_dictionaries
Caught exception: Traceback (most recent call last): File "/snap/anki-woodrow/27/lib/python3.8/site-packages/aqt/webview.py", line 37, in cmd return json.dumps(self.onCmd(str)) File "/snap/anki-woodrow/27/lib/python3.8/site-packages/aqt/webview.py", line 123, in _onCmd return self._onBridgeCmd(str) File "/snap/anki-woodrow/27/lib/python3.8/site-packages/aqt/webview.py", line 547, in _onBridgeCmd return self.onBridgeCmd(cmd) File "", line 2, in onBridgeCmd File "/snap/anki-woodrow/27/lib/python3.8/site-packages/anki/hooks.py", line 638, in decorator_wrapper return repl(*args, **kwargs) File "/snap/anki-woodrow/27/lib/python3.8/site-packages/anki/hooks.py", line 635, in repl return new(_old=old, *args, **kwargs) File "/home/element/snap/anki-woodrow/27/.local/share/Anki2/addons21/search_in_dictionaries/init.py", line 230, in on_bridge_cmd _old(editor, command) File "", line 2, in onBridgeCmd File "/snap/anki-woodrow/27/lib/python3.8/site-packages/anki/hooks.py", line 638, in decorator_wrapper return repl(*args, **kwargs) File "/snap/anki-woodrow/27/lib/python3.8/site-packages/anki/hooks.py", line 635, in repl return new(_old=old, *args, **kwargs) File "/home/element/snap/anki-woodrow/27/.local/share/Anki2/addons21/blur_text/init.py", line 59, in on_bridge_cmd _old(editor, command) File "", line 2, in onBridgeCmd File "/snap/anki-woodrow/27/lib/python3.8/site-packages/anki/hooks.py", line 638, in decorator_wrapper return repl(*args, **kwargs) File "/snap/anki-woodrow/27/lib/python3.8/site-packages/anki/hooks.py", line 635, in repl return new(_old=old, *args, **kwargs) File "/home/element/snap/anki-woodrow/27/.local/share/Anki2/addons21/799647424/main.py", line 183, in on_bridge_cmd _old(editor, command) File "/snap/anki-woodrow/27/lib/python3.8/site-packages/aqt/editor.py", line 385, in onBridgeCmd (type, ord, nid, txt) = cmd.split(":", 3) ValueError: not enough values to unpack (expected 4, got 1)
问题是按钮的事件名称,我需要更改它,出于某种原因,“blur_text”在 Anki 系统中使用。
def contrast_text(editor: Editor) -> None:
...
def add_contrast_button(buttons: List[str], editor: Editor) -> List[str]:
button = editor.addButton(ICON_PATH, "contrast", contrast_text)
buttons.append(button)
return buttons
addHook("setupEditorButtons", add_contrast_button)