popup menu...

Hi...
I'm using this code to generate a popup menu from a text file with a dict.structure...
Its working ok, except when it creates a submenu, the submenu is created but I dont get any event...

Any tip how to solve it?

···

----------------------------------------------------------------------------------------------------------------
def initMenu(self):
    taskbarMenu = wx.Menu()
    for menuList in self.myLinks:
        self.createMenu(menuList, taskbarMenu)
        taskbarMenu.AppendSeparator()
    return taskbarMenu

def createMenu(self, menuList, menu):
    for key in menuList:
        if type(menuList[key]) == types.DictType:
            newMenu = wx.Menu()
            self.createMenu(menuList[key], newMenu)
            menu.AppendSubMenu(newMenu, key)
        else:
            newid = wx.NewId()
            menuItem = wx.MenuItem(menu, newid, key, '', wx.ITEM_NORMAL, None)
            if menuList[key][0] == 1:
                menuItem.SetBitmap(SHApp.getBitmap())
            if menuList[key][0] == 2:
                menuItem.SetBitmap(SHFolder.getBitmap())
            if menuList[key][0] == 3:
                menuItem.SetBitmap(SHWorld.getBitmap())
            menu.AppendItem(menuItem)
            menu.Bind(wx.EVT_MENU, self.onRun, id=newid)
----------------------------------------------------------------------------------------------------------------

Tnx
/Holmis

Holmis wrote:

Hi...
I'm using this code to generate a popup menu from a text file with a
dict.structure...
Its working ok, except when it creates a submenu, the submenu is created but
I dont get any event...

Any tip how to solve it?

----------------------------------------------------------------------------------------------------------------
def initMenu(self):
    taskbarMenu = wx.Menu()
    for menuList in self.myLinks:
        self.createMenu(menuList, taskbarMenu)
        taskbarMenu.AppendSeparator()
    return taskbarMenu

def createMenu(self, menuList, menu):
    for key in menuList:
        if type(menuList[key]) == types.DictType:
            newMenu = wx.Menu()
            self.createMenu(menuList[key], newMenu)
            menu.AppendSubMenu(newMenu, key)
        else:
            newid = wx.NewId()
            menuItem = wx.MenuItem(menu, newid, key, '', wx.ITEM_NORMAL,
None)
            if menuList[key][0] == 1:
                menuItem.SetBitmap(SHApp.getBitmap())
            if menuList[key][0] == 2:
                menuItem.SetBitmap(SHFolder.getBitmap())
            if menuList[key][0] == 3:
                menuItem.SetBitmap(SHWorld.getBitmap())
            menu.AppendItem(menuItem)
            menu.Bind(wx.EVT_MENU, self.onRun, id=newid)

I think you meant:
            self.Bind(wx.EVT_MENU, self.onRun, menuItem, id=newid)

You want the event bound to the window, but it needs to be for this
specific menuItem.

···

--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

Using both menuItem and id=newid is redundant in this case. Using just one or the other is fine.

···

On 3/17/11 4:04 PM, Tim Roberts wrote:

I think you meant:
             self.Bind(wx.EVT_MENU, self.onRun, menuItem, id=newid)

You want the event bound to the window, but it needs to be for this
specific menuItem.

--
Robin Dunn
Software Craftsman

hmm, the taskbarmenu is not working at all with self.Bind....

I made an example (see myCode), the event is working but not in the
subMenu...
tempDict['SubMenu'] = {'subMenu1': 's1', 'subMenu2': 's2', 'subMenu3': 's3'}

myCode... (copy, paste and run)

···

----------------------------------------------------------------------------
import wx
import types

from wx.lib.embeddedimage import PyEmbeddedImage

class myFrame(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent, -1, '')

        self.loadMenuSetup()
        self.taskbarMenu = self.initMenu()

        icon = self.getImage()
        self.taskbar = wx.TaskBarIcon()
        self.taskbar.SetIcon(icon, '')
        self.taskbar.Bind(wx.EVT_TASKBAR_RIGHT_UP, self.onTaskBarMenu)

    def getImage(self):
        t = PyEmbeddedImage(
            "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABHNCSVQICAgIfAhkiAAAAWNJ"
            "REFUOI3Fk7FOlkEQRc/sLgVBbbU18Q2gpPAVbLSwtTE2dj6BiVYSEh5CGh+HEixoqRAM+829"
            "Frsf+YnVHwsn2ewUu+feO8nAP1a8+vj9w+HL/a9Xt+zZgIQlUollZOFMZONMUuLJTv91dnb5"
            "6cfRm5N2cLh/vLP7qDzd3Up47/p2OQZO2k2n3Fxdb239d6cANKf5/PbF1oB3X34yABYAd0ti"
            "wDIpEEYyKZGLx7G4S/P82WNSOQDYAEQJwsYlcJhQUEoQUYhIopqqQlvGey0TsJIK4AgIqAYF"
            "SKYaSlRaQIbJMhwrNSNoEIkA+wEoApxGAQ6gBFV1ANYIazMYMZoNkFrQJOThUvOJNB0svd87"
            "CMA2EYE3QJRCBdKmxHC89GVGmFkqjE8zygNHgIAagafyerfVCkDMw8bHtep9M2cwRdrl+cXp"
            "+2/9tTJRisxEEtk7ksYOKMcwPfZkaPr0L5X/Un8Ay9zpBmk1I4QAAAAASUVORK5CYII=")
        return t.GetIcon()

    def loadMenuSetup(self):
        self.popM =
        tempDict = {}
        tempDict['Test1'] = 'n1'
        tempDict['Test2'] = 'n2'
        tempDict['Test3'] = 'n3'
        self.popM.append(tempDict)
        tempDict = {}
        tempDict['SubMenu'] = {'subMenu1': 's1',
                             'subMenu2': 's2',
                             'subMenu3': 's3'}
        self.popM.append(tempDict)

    def onTaskBarMenu(self, event):
        self.taskbar.PopupMenu(self.taskbarMenu)

    def initMenu(self):
        taskbarMenu = wx.Menu()
        for menuList in self.popM:
            self.createMenu(menuList, taskbarMenu)
            taskbarMenu.AppendSeparator()
        newid = wx.NewId()
        menuItem = wx.MenuItem(taskbarMenu, newid, 'Exit', '',
wx.ITEM_NORMAL, None)
        taskbarMenu.AppendItem(menuItem)
        taskbarMenu.Bind(wx.EVT_MENU, self.onExit, id=newid)
        return taskbarMenu

    def createMenu(self, menuList, menu):
        for key in menuList:
            if type(menuList[key]) == types.DictType:
                newMenu = wx.Menu()
                self.createMenu(menuList[key], newMenu)
                menu.AppendSubMenu(newMenu, key)
            else:
                newid = wx.NewId()
                menuItem = wx.MenuItem(menu, newid, key, '', wx.ITEM_NORMAL,
None)
                menu.AppendItem(menuItem)
                menu.Bind(wx.EVT_MENU, self.onRun, id=newid)

    def onRun(self, event):
        print '...'
        key = event.GetEventObject().GetLabel(event.GetId())
        print key

    def onExit(self, event):
        self.Destroy()

class myApp(wx.App):
    def OnInit(self):
        self.main = myFrame(None)
        self.main.Hide()
        return True

def main():
    application = myApp(0)
    application.MainLoop()

if __name__ == '__main__':
    main()
----------------------------------------------------------------------------

Tnx
Holmis

"Robin Dunn" wrote in message news:4D82AD48.4020908@alldunn.com...

On 3/17/11 4:04 PM, Tim Roberts wrote:

I think you meant:
             self.Bind(wx.EVT_MENU, self.onRun, menuItem, id=newid)

You want the event bound to the window, but it needs to be for this
specific menuItem.

Using both menuItem and id=newid is redundant in this case. Using just
one or the other is fine.

--
Robin Dunn
Software Craftsman

--
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en

Hi Steve,

Looking at your code, the main issue is that you try to bind the event
to the menu.
You need to bind the event to the control receiving the event, in your
case, the taskbar.

Also, I view menus as transient objects, created on the fly and
destroyed after they are used.

I've attached 2 files:
test.2.py is your code with the modification made to make it work
(changed the event binders, the order of creation and added the
taskbar.Destroy() as this is needed for the app to exit)

test.1.py contains an alternate implementation using a different menu
structure that maps commands to menu names. This is done using lists
and tuples as this ensures proper order (the dictionary keys do not)
You can bypass the use of tuples and use just texts doing decoding
only by menu item names but this will less flexible (you cannot have 2
menu items with the same name)

Sorry for the mixed indentations (my code is set to 4).

I hope this helps. :wink:

test.2.py (2.99 KB)

test.1.py (2.71 KB)

···

On Mon, Mar 21, 2011 at 12:41 AM, Holmis <stefan_holmgren@msn.com> wrote:

hmm, the taskbarmenu is not working at all with self.Bind....

I made an example (see myCode), the event is working but not in the
subMenu...
tempDict['SubMenu'] = {'subMenu1': 's1', 'subMenu2': 's2', 'subMenu3': 's3'}

myCode... (copy, paste and run)
----------------------------------------------------------------------------
import wx
import types

from wx.lib.embeddedimage import PyEmbeddedImage

class myFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, -1, '')

  self\.loadMenuSetup\(\)
  self\.taskbarMenu = self\.initMenu\(\)

  icon = self\.getImage\(\)
  self\.taskbar = wx\.TaskBarIcon\(\)
  self\.taskbar\.SetIcon\(icon, &#39;&#39;\)
  self\.taskbar\.Bind\(wx\.EVT\_TASKBAR\_RIGHT\_UP, self\.onTaskBarMenu\)

def getImage(self):
t = PyEmbeddedImage(

"iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABHNCSVQICAgIfAhkiAAAAWNJ"

"REFUOI3Fk7FOlkEQRc/sLgVBbbU18Q2gpPAVbLSwtTE2dj6BiVYSEh5CGh+HEixoqRAM+829"

"Frsf+YnVHwsn2ewUu+feO8nAP1a8+vj9w+HL/a9Xt+zZgIQlUollZOFMZONMUuLJTv91dnb5"

"6cfRm5N2cLh/vLP7qDzd3Up47/p2OQZO2k2n3Fxdb239d6cANKf5/PbF1oB3X34yABYAd0ti"

"wDIpEEYyKZGLx7G4S/P82WNSOQDYAEQJwsYlcJhQUEoQUYhIopqqQlvGey0TsJIK4AgIqAYF"

"SKYaSlRaQIbJMhwrNSNoEIkA+wEoApxGAQ6gBFV1ANYIazMYMZoNkFrQJOThUvOJNB0svd87"

"CMA2EYE3QJRCBdKmxHC89GVGmFkqjE8zygNHgIAagafyerfVCkDMw8bHtep9M2cwRdrl+cXp"

"+2/9tTJRisxEEtk7ksYOKMcwPfZkaPr0L5X/Un8Ay9zpBmk1I4QAAAAASUVORK5CYII=")
return t.GetIcon()

def loadMenuSetup(self):
self.popM =
tempDict = {}
tempDict['Test1'] = 'n1'
tempDict['Test2'] = 'n2'
tempDict['Test3'] = 'n3'
self.popM.append(tempDict)
tempDict = {}
tempDict['SubMenu'] = {'subMenu1': 's1',
'subMenu2': 's2',
'subMenu3': 's3'}
self.popM.append(tempDict)

def onTaskBarMenu(self, event):
self.taskbar.PopupMenu(self.taskbarMenu)

def initMenu(self):
taskbarMenu = wx.Menu()
for menuList in self.popM:
self.createMenu(menuList, taskbarMenu)
taskbarMenu.AppendSeparator()
newid = wx.NewId()
menuItem = wx.MenuItem(taskbarMenu, newid, 'Exit', '',
wx.ITEM_NORMAL, None)
taskbarMenu.AppendItem(menuItem)
taskbarMenu.Bind(wx.EVT_MENU, self.onExit, id=newid)
return taskbarMenu

def createMenu(self, menuList, menu):
for key in menuList:
if type(menuList[key]) == types.DictType:
newMenu = wx.Menu()
self.createMenu(menuList[key], newMenu)
menu.AppendSubMenu(newMenu, key)
else:
newid = wx.NewId()
menuItem = wx.MenuItem(menu, newid, key, '', wx.ITEM_NORMAL,
None)
menu.AppendItem(menuItem)
menu.Bind(wx.EVT_MENU, self.onRun, id=newid)

def onRun(self, event):
print '...'
key = event.GetEventObject().GetLabel(event.GetId())
print key

def onExit(self, event):
self.Destroy()

class myApp(wx.App):
def OnInit(self):
self.main = myFrame(None)
self.main.Hide()
return True

def main():
application = myApp(0)
application.MainLoop()

if __name__ == '__main__':
main()
----------------------------------------------------------------------------

Tnx
Holmis

"Robin Dunn" wrote in message news:4D82AD48.4020908@alldunn.com...

On 3/17/11 4:04 PM, Tim Roberts wrote:

I think you meant:
self.Bind(wx.EVT_MENU, self.onRun, menuItem, id=newid)

You want the event bound to the window, but it needs to be for this
specific menuItem.

Using both menuItem and id=newid is redundant in this case. Using just
one or the other is fine.

--
Robin Dunn
Software Craftsman
http://wxPython.org

--
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en

--
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en

--
There is NO FATE, we are the creators.
blog: http://damoc.ro/

Hi Peter...
Thank you for taking your time to look at this, it's working now...
...and thanks for the alternate code in test.1.py

/Holmis

"Peter Damoc" wrote in message news:AANLkTik_Zm05EEDdo1OJTzU1bqjvMt4VKvMnv8T5e-Ck@mail.gmail.com...

Hi Steve,

Looking at your code, the main issue is that you try to bind the event
to the menu.
You need to bind the event to the control receiving the event, in your
case, the taskbar.

Also, I view menus as transient objects, created on the fly and
destroyed after they are used.

I've attached 2 files:
test.2.py is your code with the modification made to make it work
(changed the event binders, the order of creation and added the
taskbar.Destroy() as this is needed for the app to exit)

test.1.py contains an alternate implementation using a different menu
structure that maps commands to menu names. This is done using lists
and tuples as this ensures proper order (the dictionary keys do not)
You can bypass the use of tuples and use just texts doing decoding
only by menu item names but this will less flexible (you cannot have 2
menu items with the same name)

Sorry for the mixed indentations (my code is set to 4).

I hope this helps. :wink:

···

On Mon, Mar 21, 2011 at 12:41 AM, Holmis <stefan_holmgren@msn.com> wrote:

hmm, the taskbarmenu is not working at all with self.Bind....

I made an example (see myCode), the event is working but not in the
subMenu...
tempDict['SubMenu'] = {'subMenu1': 's1', 'subMenu2': 's2', 'subMenu3': 's3'}

myCode... (copy, paste and run)
----------------------------------------------------------------------------
import wx
import types

from wx.lib.embeddedimage import PyEmbeddedImage

class myFrame(wx.Frame):
  def __init__(self, parent):
      wx.Frame.__init__(self, parent, -1, '')

      self.loadMenuSetup()
      self.taskbarMenu = self.initMenu()

      icon = self.getImage()
      self.taskbar = wx.TaskBarIcon()
      self.taskbar.SetIcon(icon, '')
      self.taskbar.Bind(wx.EVT_TASKBAR_RIGHT_UP, self.onTaskBarMenu)

  def getImage(self):
      t = PyEmbeddedImage(

"iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABHNCSVQICAgIfAhkiAAAAWNJ"

"REFUOI3Fk7FOlkEQRc/sLgVBbbU18Q2gpPAVbLSwtTE2dj6BiVYSEh5CGh+HEixoqRAM+829"

"Frsf+YnVHwsn2ewUu+feO8nAP1a8+vj9w+HL/a9Xt+zZgIQlUollZOFMZONMUuLJTv91dnb5"

"6cfRm5N2cLh/vLP7qDzd3Up47/p2OQZO2k2n3Fxdb239d6cANKf5/PbF1oB3X34yABYAd0ti"

"wDIpEEYyKZGLx7G4S/P82WNSOQDYAEQJwsYlcJhQUEoQUYhIopqqQlvGey0TsJIK4AgIqAYF"

"SKYaSlRaQIbJMhwrNSNoEIkA+wEoApxGAQ6gBFV1ANYIazMYMZoNkFrQJOThUvOJNB0svd87"

"CMA2EYE3QJRCBdKmxHC89GVGmFkqjE8zygNHgIAagafyerfVCkDMw8bHtep9M2cwRdrl+cXp"

"+2/9tTJRisxEEtk7ksYOKMcwPfZkaPr0L5X/Un8Ay9zpBmk1I4QAAAAASUVORK5CYII=")
      return t.GetIcon()

  def loadMenuSetup(self):
      self.popM =
      tempDict = {}
      tempDict['Test1'] = 'n1'
      tempDict['Test2'] = 'n2'
      tempDict['Test3'] = 'n3'
      self.popM.append(tempDict)
      tempDict = {}
      tempDict['SubMenu'] = {'subMenu1': 's1',
                           'subMenu2': 's2',
                           'subMenu3': 's3'}
      self.popM.append(tempDict)

  def onTaskBarMenu(self, event):
      self.taskbar.PopupMenu(self.taskbarMenu)

  def initMenu(self):
      taskbarMenu = wx.Menu()
      for menuList in self.popM:
          self.createMenu(menuList, taskbarMenu)
          taskbarMenu.AppendSeparator()
      newid = wx.NewId()
      menuItem = wx.MenuItem(taskbarMenu, newid, 'Exit', '',
wx.ITEM_NORMAL, None)
      taskbarMenu.AppendItem(menuItem)
      taskbarMenu.Bind(wx.EVT_MENU, self.onExit, id=newid)
      return taskbarMenu

  def createMenu(self, menuList, menu):
      for key in menuList:
          if type(menuList[key]) == types.DictType:
              newMenu = wx.Menu()
              self.createMenu(menuList[key], newMenu)
              menu.AppendSubMenu(newMenu, key)
          else:
              newid = wx.NewId()
              menuItem = wx.MenuItem(menu, newid, key, '', wx.ITEM_NORMAL,
None)
              menu.AppendItem(menuItem)
              menu.Bind(wx.EVT_MENU, self.onRun, id=newid)

  def onRun(self, event):
      print '...'
      key = event.GetEventObject().GetLabel(event.GetId())
      print key

  def onExit(self, event):
      self.Destroy()

class myApp(wx.App):
  def OnInit(self):
      self.main = myFrame(None)
      self.main.Hide()
      return True

def main():
  application = myApp(0)
  application.MainLoop()

if __name__ == '__main__':
  main()
----------------------------------------------------------------------------

Tnx
Holmis

"Robin Dunn" wrote in message news:4D82AD48.4020908@alldunn.com...

On 3/17/11 4:04 PM, Tim Roberts wrote:

I think you meant:
            self.Bind(wx.EVT_MENU, self.onRun, menuItem, id=newid)

You want the event bound to the window, but it needs to be for this
specific menuItem.

Using both menuItem and id=newid is redundant in this case. Using just
one or the other is fine.

--
Robin Dunn
Software Craftsman
http://wxPython.org

--
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en

--
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en

--
There is NO FATE, we are the creators.
blog: http://damoc.ro/

--
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en