I can add the menu through the following code
import pymel.core as pm
parent_object = pm.getPanel(sty="graphEditor")[0]
pm.menu("test_menu", parent=parent_object, label="Test Menu", tearOff=True
However as soon as I tear off the graph editor panel, I lose the menu. How can I make this permanent for the graph editor window or any other scripted panel?
I haven't been setting this stuff up in newer Maya releases (2016+) so my information may be out of date. But ya it is a bit tricky to make custom changes to Maya's interface and have them be persistent when the UI is modified. This is happening because a tear-off is recreating the widget and your injected modifications to the old widget are lost.
Some options you have to automatically introduce this change is to include setting an
event filter on one of the parent objects that don't change. This could end up just being the main window or the QApplication. The event filter would be watching for the
ChildAdded event, and if its the expected type of widget you can apply your customization.
I don't recall the MMessage API offering any hooks for when arbitrary UI's are created.