0

我正在创建某种项目资源管理器,就像您在 PyScripter 中看到的一样。问题是当我右键单击以显示弹出菜单时出现 AssertionError 。任何的想法?谢谢是提前。

class ProgramManager(wx.Panel):
  def __init__(self, parent):
    super(ProgramManager, self).__init__(parent, size = (300,500), style = wx.BORDER_NONE|wx.TB_TOP)
    self.SetMinSize((300,500))
    self.BackgroundColour = wx.WHITE
    self.Sizer = wx.BoxSizer(wx.VERTICAL)
    self.Layout = self.Sizer

    ## Set the ToolBar
    self.ToolBar = IPTToolBar(self)
    self.ToolBar.AddTool2(ID_NEW_PROGRAM, 'Creates a new project')
    self.ToolBar.AddTool2(ID_OPEN_PROGRAM, 'Open an existing project')
    self.ToolBar.AddTool2(ID_SAVE_PROGRAM, 'Save project')
    self.ToolBar.AddTool2(ID_SAVE_PROGRAM_AS, 'Save a copy of project')
    self.ToolBar.AddTool2(ID_CLOSE_PROGRAM, 'Close the program')
    self.Layout.Add(self.ToolBar, flag = wx.EXPAND)
    self.ToolBar.Realize()

    ## Set the Tree
    self.FilePath = ''
    self.Tree = CT.CustomTreeCtrl(self)
    self.Layout.Add(self.Tree, 1, wx.ALL|wx.EXPAND)

    ## Bind Events
    self.Bind(wx.EVT_CLOSE, self.OnClose, self)
    self.Bind(wx.EVT_TREE_ITEM_MENU, self.RClickMenu)

  def RClickMenu(self, Event):
    EvtObj = Event.GetItem()
    EvtObjData = EvtObj.GetData()
    if EvtObj.GetParent() == None:
      # Root Menu
      self.PopMenu = wx.Menu()
      self.PopMenu.Append(ID_NEW_PROGRAM, 'New Program', 'Create a New Program')
      self.PopMenu.Append(ID_OPEN_PROGRAM, 'Open Program', 'Open Existing Program')
      self.PopMenu.Append(ID_SAVE_PROGRAM, 'Save Program', 'Save Current Program')
      self.PopMenu.Append(ID_SAVE_PROGRAM_AS, 'Save Program As', 'Save a Copy of Current Program')
      self.PopMenu.Append(ID_CLOSE_PROGRAM, 'Close','Close Program')
      self.Tree.PopupMenu(self.PopMenu)
    elif EvtObjData == 'Files':
      self.PopMenu = wx.Menu()
      self.PopMenu.Append(ID_ADD_FILE, 'Add File', 'Add an Existing File From Hard Disk')
      self.PopMenu.Append(ID_ADD_CURRENT_FILE, 'Add Current File', 'Add Currently Selected File')
      self.PopMenu.Append(ID_ADD_SUBFOLDER, 'Add a Subfolder', 'Add a Subfolder')

      self.Tree.PopupMenu(self.PopMenu)
    elif EvtObjData == 'Configuration':
      self.PopMenu = wx.Menu()
      self.PopMenu.Append(wx.ID_EDIT, 'Edit', 'Edit the Configuration')
      self.Tree.PopupMenu(self.PopMenu)

    elif EvtObjData == 'Folder':
      self.PopMenu = wx.Menu()
      self.PopMenu.Append(ID_ADD_FILE, 'Add File', 'Add an Existing File From Hard Disk')
      self.PopMenu.Append(ID_ADD_CURRENT_FILE, 'Add Current File', 'Add Currently Selected File')
      self.PopMenu.Append(ID_ADD_SUBFOLDER, 'Add a Subfolder', 'Add a Subfolder')
      self.PopMenu.Append(ID_RENAME, 'Rename', 'Rename This Folder')
      self.PopMenu.Append(ID_REMOVE, 'Remove Folder', 'Remove This Folder and Its Contents')
      self.Tree.PopupMenu(self.PopMenu)
    elif os.path.isfile(EvtObjData):
      self.PopMenu = wx.Menu()
      self.PopMenu.Append(wx.ID_EDIT, 'Edit File', 'Edit This File')
      self.PopMenu.Append(ID_REMOVE, 'Remove File', 'Remove Currently Selected File')
      self.Tree.PopupMenu(self.PopMenu)

    self.PopMenu.Bind(wx.EVT_MENU, self.OnMenu, self.PopMenu)
    self.PopMenu.Destroy()

  def OnMenu(self, Event):
    pass

错误代码是这样的

Traceback (most recent call last):
  File "L:\NUSAIPT\NUSAIPT.py", line 222, in RClickMenu
    self.PopMenu.Bind(wx.EVT_MENU, self.OnMenu, self.PopMenu)
  File "C:\Python27\lib\site-packages\wx-2.8-msw-unicode\wx\_core.py", line 3918, in Bind
    assert source is None or hasattr(source, 'GetId')
AssertionError

在这里玩的小例子:

import wx, os, wx.lib.customtreectrl as CT

class CTTest(wx.Frame):
  def __init__(self, parent, *args, **kwargs):
    wx.Frame.__init__(self, parent, *args, **kwargs)
    self.Maximize(True)
    self.parent = parent
    self.BackgroundColour = wx.WHITE
    self.SetMinSize((800,600))
    self.Tree = CT.CustomTreeCtrl(self)
    ## Bind Events
    self.Bind(wx.EVT_CLOSE, self.OnClose, self)
    self.Bind(CT.EVT_TREE_ITEM_MENU, self.RClickMenu)
    self.NewTree()
    self.Show(True)

  def RClickMenu(self, Event):
    EvtObj = Event.GetItem()
    EvtObjData = EvtObj.GetData()
    if EvtObj.GetParent() == None:
      # Root Menu
      self.PopMenu = wx.Menu()
      self.PopMenu.Append(ID_NEW_TREE, 'New Tree', 'Create a New Tree')
      self.PopMenu.Append(ID_OPEN_TREE, 'Open Tree', 'Open Existing Tree')
      self.PopMenu.Append(ID_SAVE_TREE, 'Save Tree', 'Save Current Tree')
      self.PopMenu.Append(ID_SAVE_TREE_AS, 'Save Tree As', 'Save a Copy of Current Tree')
      self.PopMenu.Append(ID_CLOSE_TREE, 'Close Tree','Close Tree')
      self.Tree.PopupMenu(self.PopMenu)
    # Files Folder Menu
    elif EvtObjData == 'Files':
      self.PopMenu = wx.Menu()
      self.PopMenu.Append(ID_ADD_FILE, 'Add File', 'Add an Existing File From Hard Disk')
      self.PopMenu.Append(ID_ADD_SUBFOLDER, 'Add a Subfolder', 'Add a Subfolder')
      self.Tree.PopupMenu(self.PopMenu)
    # Run Configuration Menu
    elif EvtObjData == 'Configuration':
      self.PopMenu = wx.Menu()
      self.PopMenu.Append(wx.ID_EDIT, 'Edit', 'Edit the Configuration')
      self.Tree.PopupMenu(self.PopMenu)
    # Folder Menu
    elif EvtObjData == 'Folder':
      self.PopMenu = wx.Menu()
      self.PopMenu.Append(ID_ADD_FILE, 'Add File', 'Add an Existing File From Hard Disk')
      self.PopMenu.Append(ID_ADD_SUBFOLDER, 'Add a Subfolder', 'Add a Subfolder')
      self.PopMenu.Append(ID_RENAME, 'Rename', 'Rename This Folder')
      self.PopMenu.Append(ID_REMOVE, 'Remove Folder', 'Remove This Folder and Its Contents')
      self.Tree.PopupMenu(self.PopMenu)
    # File Menu
    elif os.path.isfile(EvtObjData):
      self.PopMenu = wx.Menu()
      self.PopMenu.Append(wx.ID_EDIT, 'Edit File', 'Edit This File')
      self.PopMenu.Append(ID_REMOVE, 'Remove File', 'Remove Currently Selected File')
      self.Tree.PopupMenu(self.PopMenu)

    self.Bind(wx.EVT_MENU, self.OnMenu, self.PopMenu)

  def OnMenu(self, Event):
    print 'OnMenu'
    EvtID = Event.GetEventId()
    print EvtID
    if EvtID == ID_NEW_TREE:
      self.NewProgram()

    if EvtID == ID_OPEN_TREE:
      self.OpenProgram()

    if EvtID == ID_SAVE_TREE:
      self.SaveProgram()

    if EvtID == ID_SAVE_TREE_AS:
      self.SaveProgramAs()

    if EvtID == ID_ADD_FILE:
      print 'Add File'

    if EvtID == ID_ADD_SUBFOLDER:
      print 'Add SubFolder'

    if EvtID == wx.ID_EDIT:
      print 'Edit'

    if EvtID == ID_RENAME:
      print 'Rename'

    self.PopMenu.Destroy()

  def NewTree(self):
    self.Tree.AddRoot('Root')
    Root = self.Tree.GetRootItem()
    self.Tree.InsertItemByItem(Root, None, 'Files', data = 'Files')
    self.Tree.InsertItemByItem(Root, None, 'Configuration', data = 'Configuration')
    self.Tree.ExpandAll()

  def OpenTree(self):
    print 'Open'

  def SaveTree(self):
    print 'Save'

  def SaveTreeAs(self):
    print 'Save As'

  def CloseTree(self):
    print 'Close Tree'

  def OnClose(self, Event):
    self.Destroy()

if __name__ == '__main__':
  App = wx.App()

  # The IDs
  ID_NEW_TREE = wx.NewId()
  ID_OPEN_TREE = wx.NewId()
  ID_SAVE_TREE = wx.NewId()
  ID_SAVE_TREE_AS = wx.NewId()
  ID_CLOSE_TREE = wx.NewId()
  # IDs of Program Manager
  ID_ADD_FILE = wx.NewId()
  ID_ADD_SUBFOLDER = wx.NewId()
  ID_RENAME = wx.NewId()
  ID_REMOVE = wx.NewId()

  Fr = CTTest(None, -1, 'Testing',(0,0))
  App.MainLoop()

顺便说一句,我正在使用 Python2.7 和 wx2.8.12

4

1 回答 1

2

菜单通常必须附加到框架或父级,所以我猜你需要将绑定语句更改为:

self.Bind(wx.EVT_MENU, self.OnMenu, self.PopMenu)

如果这不起作用,请创建一个我们可以玩的小型可运行示例应用程序。有关详细信息,请参阅http://wiki.wxpython.org/MakingSampleApps

于 2012-05-02T15:01:31.247 回答