Recursive context menu creation

29 views
Skip to first unread message

Claudia Frank

unread,
Sep 9, 2016, 1:12:11 AM9/9/16
to wxPython-users
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

niki

unread,
Sep 9, 2016, 3:42:17 AM9/9/16
to wxpytho...@googlegroups.com
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

Claudia Frank

unread,
Sep 9, 2016, 5:51:22 PM9/9/16
to wxPython-users, ni...@vintech.bg
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
Reply all
Reply to author
Forward
0 new messages