0

PyScripter で見られるものと同じように、ある種の Project Explorer を作成しています。問題は、右クリックしてポップアップ メニューを表示すると、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

通常、メニューはフレームまたは親にアタッチする必要があるため、bind ステートメントを次のように変更する必要があると思います。

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

それがうまくいかない場合は、実行可能な小さなサンプル アプリを作成してください。詳細については、 http://wiki.wxpython.org/MakingSampleAppsを参照してください。

于 2012-05-02T15:01:31.247 に答える