Recursive context menu creation

Hello,

I’m fairly new to wxPython.
I already read a lot in the wiki but wasn’t able
to find an answer to my question.
In case I overlooked it please apologize.

My question is whether it is considered to be save what I’m doing.

class ContextMenu(wx.Menu):

def __init__(self, list_of_contextmenu_entries):
    super(ContextMenu, self).__init__()

    def create_menu_item(self, k, v):
        if k == '-':
            self.AppendSeparator()
        else:
            if type(v) is list:
                menu = wx.Menu()
                self.AppendSubMenu(menu, k)
                for k2, v2 in v:
                    create_menu_item(menu, k2,v2)
            else:
                item = wx.MenuItem(self, wx.NewId(), k)
                self.AppendItem(item)
                self.Bind(wx.EVT_MENU, v, item)                
           
    for k,v in list_of_contextmenu_entries:
        create_menu_item(self, k, v)

``

Within constructor I create a context menu with a recursive function create_menu_item.
As you see, I overwrite the self with menu (wx.Menu) once it has to create a submenu.
I guess this is a bit of a hack but it seems to work without problems.

Attached a sample program.

My configuration
ChaletOS (linux) and wxPython 3.0.2.0

Thank you very much
Claudia

context_menu.py (2.35 KB)

You have noting to worry about, this is not a hack. It's will work in C++ too. First argument to create_menu_item() is always wx.Menu or subclass of wx.Menu

Niki

···

On 9.09.2016 04:14, Claudia Frank wrote:

Hello,

I'm fairly new to wxPython.
I already read a lot in the wiki but wasn't able
to find an answer to my question.
In case I overlooked it please apologize.

My question is whether it is considered to be save what I'm doing.

>

classContextMenu(wx.Menu):

    def__init__(self,list_of_contextmenu_entries):
        super(ContextMenu,self).__init__()

        defcreate_menu_item(self,k,v):
            ifk =='-':
                self.AppendSeparator()
            else:
                iftype(v)islist:
                    menu =wx.Menu()
                    self.AppendSubMenu(menu,k)
                    fork2,v2 inv:
                        create_menu_item(menu,k2,v2)
                else:
                    item =wx.MenuItem(self,wx.NewId(),k)
                    self.AppendItem(item)
                    self.Bind(wx.EVT_MENU,v,item)

        fork,v inlist_of_contextmenu_entries:
            create_menu_item(self,k,v)
>

Within constructor I create a context menu with a recursive function
create_menu_item.
As you see, I overwrite the self with menu (wx.Menu) once it has to
create a submenu.
I guess this is a bit of a hack but it seems to work without problems.

Thank you very much for your answer.
I’m concerned about the self variable and its meaning.
Just wanted to be sure that wxPython is save about this,
as I read, recently, that one might be able to do strange things
with the self variable. There even was some request to remove
self or define it as keyword but Guido refused to do so because
of backward issues and other critical stuff.
But, as of today, I think also that it should be ok because
init is not the constructor, new is and therefore I
would reinitialize a value only.

Hope this makes sense to you.

Cheers
Claudia