Page 1 of 1

Best way to use unload()

Posted: Thu Jan 22, 2015 6:43 pm
by BackRaw
Hi,

I'm not quite sure what's the best and most effective and clear way of using unload().
For example, I have a myaddon/menus.py file:

Syntax: Select all

from menus import Option
from menus import SimpleMenu


def menu1_callback(menu, index, option):
pass

def menu2_callback(menu, index, option):
pass


menu1 = SimpleMenu(select_callback=menu1_callback)

# add content

menu2 = SimpleMenu(select_callback=menu2_callback)

# add content
and a myaddon/myaddon.py file:

Syntax: Select all

from myaddon.menus import menu1
from myaddon.menus import menu2


# use ONLY menu1 in events

def unload():
menu1.clear()
menu2.clear()
Now, Would it be better and clearer to have

Syntax: Select all

def unload():
menu1.clear()
menu2.clear()
within myaddon/menus.py for clearing them? Is it even needed to clear them on unload, or is this method just available for the build callback? Thanks! :)

Posted: Thu Jan 22, 2015 6:57 pm
by Ayuto
Every menu is a subclass of a list, so this will just empty your menu. You would rather want to call close(). However, every menu is also a subclass of the AutoUnload class. This means that your menu will be closed automatically when your plugin has been unloaded. The menu just needs to be in the global scope of your main plugin file. And since you import it to your main plugin file, it's already in the global scope.

Posted: Thu Jan 22, 2015 7:10 pm
by BackRaw
Ayuto wrote:Every menu is a subclass of a list, so this will just empty your menu. You would rather want to call close(). However, every menu is also a subclass of the AutoUnload class. This means that your menu will be closed automatically when your plugin has been unloaded. The menu just needs to be in the global scope of your main plugin file. And since you import it to your main plugin file, it's already in the global scope.


That's what I needed. Thanks.