Appending New Dropdown Option on MenuBar? (Pycharm, Windows)

Beginnger in the whole Python/Anki thing. Learning more about coding for fun and I have set up a debugging environment with pycharm and an anki profile with the bazelfixes and it seems to start anki up fine. I went through the hooks and the toolbar hook worked but it is not exactly what I was going for because it adds unto the toolbar not the menu. I want to add a new dropdown into the actual main menu next to tools, help, etc. connect them to functions I have made. Kind of like this

I have looked at others and sometimes they work partially but its like a fragmented puzzle so I want to figure out the first basic step. How do I, within this code on pycharm append an extra dropdown item on the menu bar. Is there a hook I am missing (not the init toolbar one)? I have also looked at the aqt mw thing but I am kind of confused how it works so I am not sure if that is a better approach to read up on? The whole class>def variable> mw.append type of structure I have been seeing in others code is a little confusing for me. Any help or advice is greatly appreciated!

Also here is the bazelfixes code I have been using if it helps show what I have been doing.
try:
import bazelfixes

bazelfixes.fix_pywin32_in_bazel()
bazelfixes.fix_extraneous_path_in_bazel()
bazelfixes.fix_run_on_macos()

except ImportError:
pass

if not os.environ.get(“ANKI_IMPORT_ONLY”):
aqt.run()

If you want to add a new menu to Anki’s main window, try something like this:

from aqt import mw
from aqt.qt import *

menu = QMenu("My Menu", mw)

action = QAction("Some action", menu)
qconnect(action.triggered, on_action)
menu.addAction(action)

another_action = QAction("Another action", menu)
qconnect(another_action.triggered, on_another_action)
menu.addAction(another_action)

mw.form.menubar.addMenu(menu)

2 Likes

Thank you! I put in the following to test

from aqt import mw
from aqt.qt import *
class mymainwindow(QApplication):
    def setupUi (self):
         menu = QMenu("My Menu", mw)

         action = QAction("Some action", menu)
         qconnect(action.triggered, QTextEdit)
         menu.addAction(action)

         another_action = QAction("Another action", menu)
         qconnect(another_action.triggered, QTextEdit)
         menu.addAction(another_action)

         mw.form.menubar.addMenu(menu)




if __name__ == "__main__":
       app = QApplication(sys.argv)
       My_Ui = mymainwindow
       My_Ui.setupUi(QMainWindow)
       QMainWindow.show()

and got this error

mw.form.menubar.addMenu(menu)
AttributeError: 'NoneType' object has no attribute 'form'

Is there a special way I am suppose to import mw.form? I remember having this issue before on my past attempt as well

No special way. If you put just my example code in __init__.py and run it as an add-on it should work. The problem is likely caused by something else in your code. Why are you creating QApplication here? I believe this will cause problems as Anki already creates an application and it’s incorrect to create any additional ones.

1 Like

I ran it by itself and got “Process finished with exit code -1073740791 (0xC0000409)”
Then I started it from the terminal and got the error "QWidget: Must construct a QApplication before a QWidget
"

Running the add-on from PyCharm won’t work. You need to copy/link the add-on’s folder to Anki’s add-ons’ folder and run Anki instead. Please see A Basic Add-on - Writing Anki Add-ons

2 Likes

I set it up this way and I think I see the cause now “on_action” is not defined I will try to add something else and see if it runs
Edit: I have it now! instead of on_action I put it under function and used self.undo like on the edit button and when I ran it see a extra menubar with the options available!

import aqt
from aqt import mw
from aqt.qt import *

def setupMenus(self):
     menu = QMenu("My Menu", mw)
     action = QAction("Some action", menu)
     qconnect(action.triggered, 
     self.undo)
     menu.addAction(action)

   another_action = QAction("Another 
   action", menu)
   qconnect(another_action.triggered, 
   self.redo)
   menu.addAction(another_action)

   mw.form.menubar.addMenu(menu)
setupMenus(mw)
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.