2016-11-23 14 views
3

アダプタアプローチを使用していくつかの単純なモデルオブジェクトを表示するように、コード例を変更しました(Adapted_tree_editor_demo.py)。ただし、右クリックメニューオプションはすべてが無効になっていますです。どうすればそれらを有効にできますか? can_delete_me()confirm_delete()を実装することは役に立たないようです。TraitsUI TreeViewで右クリックメニューを有効にするには?

Disabled menu items.

from os \ 
    import getcwd 

from traits.api \ 
    import HasTraits, Property, Directory, Instance, Str, List, adapts, property_depends_on 

from traitsui.api \ 
    import View, ModelView, VGroup, Item, TreeEditor, ITreeNode, ITreeNodeAdapter 

from pkg_resources import resource_filename 
traits_icons_path = resource_filename("traitsui", "wx/images") 

# Model classes to adapt 

class Person(HasTraits): 

    name = Str('Bob') 

class Department(HasTraits): 

    name = Str('Sales') 

    people = List(Person) 


# FileAdapter Class 

class PersonAdapter (ITreeNodeAdapter): 

    adapts(Person, ITreeNode) 

    def allows_children (self): 
     """ Returns whether this object can have children. 
     """ 
     return False 

    def has_children (self): 
     """ Returns whether the object has children. 
     """ 
     return False 

    def get_children (self): 
     """ Gets the object's children. 
     """ 
     return [] 

    def get_label (self): 
     """ Gets the label to display for a specified object. 
     """ 
     return self.adaptee.name 

    def get_tooltip (self): 
     """ Gets the tooltip to display for a specified object. 
     """ 
     if self.adaptee.name == 'Bob': 
      return 'This is Bob, he is a man.' 
     elif self.adaptee.name == 'Alice': 
      return 'This is Alice, she is a woman.' 
     return 'An employee of a department.' 

    def get_icon_path(self): 
     """ Return the path of the icons 
     """ 
     return traits_icons_path 

    def get_icon (self, is_expanded): 
     """ Returns the icon for a specified object. 
     """ 
     if self.adaptee.name == 'Bob': 
      return 'file' 
     elif self.adaptee.name == 'Alice': 
      return 'object' 
     else: 
      return 'item' 

    def can_auto_close (self): 
     """ Returns whether the object's children should be automatically 
      closed. 
     """ 
     return True 

    def can_delete_me(self): 
     return True 

    def confirm_delete(self): 
     return True 


class DepartmentAdapter (ITreeNodeAdapter): 

    adapts(Department, ITreeNode) 

    def allows_children (self): 
     """ Returns whether this object can have children. 
     """ 
     return True 

    def has_children (self): 
     """ Returns whether the object has children. 
     """ 
     return len(self.adaptee.people) > 0 

    def get_children (self): 
     """ Gets the object's children. 
     """ 
     return self.adaptee.people 

    def get_label (self): 
     """ Gets the label to display for a specified object. 
     """ 
     return self.adaptee.name 

    def get_tooltip (self): 
     """ Gets the tooltip to display for a specified object. 
     """ 
     return 'The {} department'.format(self.adaptee.name) 

    def get_icon (self, is_expanded): 
     """ Returns the icon for a specified object. 
     """ 
     if is_expanded: 
      return '<open>' 
     return '<close>' 

    def can_auto_close (self): 
     """ Returns whether the object's children should be automatically 
      closed. 
     """ 
     return True 

# Create main view 

class DepartmentView(ModelView): 

    model = Instance(Department) 

    # The traits view to display: 
    view = View(
     VGroup(
      Item('object.model.name', style='readonly'), 
      Item('model', 
        editor = TreeEditor(editable = False, auto_open = 1) 
      ), 
      show_labels = False 
     ), 
     width  = 0.33, 
     height = 0.50, 
     resizable = True 
    ) 


# Create model object graph 

bob = Person(name='Bob') 
alice = Person(name='Alice') 
dept = Department(name='Sales', people=[bob, alice]) 

view = DepartmentView(model=dept) 

# Run the demo (if invoked form the command line): 
if __name__ == '__main__': 
    view.configure_traits() 

答えて

1

私はいくつかのcanXXX方法だけでなく、部門のメニューエントリの貼り付けエントリを追加して表示されるように、すべての人のメニューエントリを作成するために管理。メソッドが呼び出されたときといつ呼び出されるかを調べるために、いくつかのprint文を記述します。

残念ながら、Departmentメニューの他のすべての項目は、自分の努力(たとえば「Rename」、取得するのが非常に複雑であるとは思わない)にもかかわらず抵抗しました。また、カット&ペーストは存在しますが、有効ではありません。append_childのような他のメソッドが実装されているはずです。

from os \ 
    import getcwd 

from traits.api \ 
    import HasTraits, Property, Directory, Instance, Str, List, adapts, property_depends_on 

from traitsui.api \ 
    import View, ModelView, VGroup, Item, TreeEditor, ITreeNode, ITreeNodeAdapter, TreeNode 

from pkg_resources import resource_filename 
traits_icons_path = resource_filename("traitsui", "wx/images") 

# Model classes to adapt 

class Person(HasTraits): 

    name = Str('Bob') 

class Department(HasTraits): 

    name = Str('Sales') 

    people = List(Person) 


# FileAdapter Class 

class PersonAdapter (ITreeNodeAdapter): 

    adapts(Person, ITreeNode) 

    def allows_children (self): 
     """ Returns whether this object can have children. 
     """ 
     return False 

    def has_children (self): 
     """ Returns whether the object has children. 
     """ 
     return False 

    def get_children (self): 
     """ Gets the object's children. 
     """ 
     return [] 

    def get_label (self): 
     """ Gets the label to display for a specified object. 
     """ 
     return self.adaptee.name 

    def get_tooltip (self): 
     """ Gets the tooltip to display for a specified object. 
     """ 
     if self.adaptee.name == 'Bob': 
      return 'This is Bob, he is a man.' 
     elif self.adaptee.name == 'Alice': 
      return 'This is Alice, she is a woman.' 
     return 'An employee of a department.' 

    def get_icon_path(self): 
     """ Return the path of the icons 
     """ 
     return traits_icons_path 

    def get_icon (self, is_expanded): 
     """ Returns the icon for a specified object. 
     """ 
     if self.adaptee.name == 'Bob': 
      return 'file' 
     elif self.adaptee.name == 'Alice': 
      return 'object' 
     else: 
      return 'item' 

    def can_auto_close (self): 
     """ Returns whether the object's children should be automatically 
      closed. 
     """ 
     return True 

    def can_delete(self): 
     print('can_delete from PersonAdapter') 
     return True 

    def can_delete_me(self): 
     print('can_delete_me from PersonAdapter') 
     return True 

    def confirm_delete(self): 
     return True 

    def can_copy (self): 
     """ Returns whether the object's children can be copied. 
     """ 
     print('can_copy from PersonAdapter') 
     return True 

    def can_rename_me (self): 
     """ Returns whether the object can be renamed. 
     """ 
     print('can_rename_me from PersonAdapter') 
     return True 

    def can_rename (self): 
     """ Returns whether the object can be renamed. 
     """ 
     print('can_rename_me from PersonAdapter') 
     return True 

    def can_add (self, add_object): 
     """ Returns whether a given object is droppable on the node. 
     """ 
     print('can_add from PersonAdapter') 
     return True 


class DepartmentAdapter (ITreeNodeAdapter): 

    adapts(Department, ITreeNode) 

    def allows_children (self): 
     """ Returns whether this object can have children. 
     """ 
     return True 

    def has_children (self): 
     """ Returns whether the object has children. 
     """ 
     return len(self.adaptee.people) > 0 

    def get_children (self): 
     """ Gets the object's children. 
     """ 
     return self.adaptee.people 

    def get_label (self): 
     """ Gets the label to display for a specified object. 
     """ 
     return self.adaptee.name 

    def get_tooltip (self): 
     """ Gets the tooltip to display for a specified object. 
     """ 
     return 'The {} department'.format(self.adaptee.name) 

    def get_icon (self, is_expanded): 
     """ Returns the icon for a specified object. 
     """ 
     if is_expanded: 
      return '<open>' 
     return '<close>' 

    def can_auto_close (self): 
     """ Returns whether the object's children should be automatically 
      closed. 
     """ 
     return True 

    def can_copy (self): 
     """ Returns whether the object's children can be copied. 
     """ 
     print('can_copy from DepartmentAdapter') 
     return True 

    def can_add (self, add_object): 
     """ Returns whether a given object is droppable on the node. 
     """ 
     print('can_add from DepartmentAdapter') 
     return True 

    def can_delete (self): 
     """ Returns whether a given object is droppable on the node. 
     """ 
     print('can_delete from DepartmentAdapter') 
     return True 

    def can_delete_me (self): 
     """ Returns whether a given object is droppable on the node. 
     """ 
     print('can_delete_me from DepartmentAdapter') 
     return True 

    def can_rename (self): 
     """ Returns whether the object can be renamed. 
     """ 
     print('can_rename from DepartmentAdapter') 
     return True 

    def can_rename_me (self): 
     """ Returns whether the object can be renamed. 
     """ 
     print('can_rename_me from DepartmentAdapter') 
     return True 

    def can_insert (self): 
     """ Returns whether the object's children can be inserted (vs. 
      appended). 
     """ 
     print('can_insert from DepartmentAdapter') 
     return True 

    def get_add (self): 
     """ Returns the list of classes that can be added to the object. 
     """ 
     print('get_add from DepartmentAdapter') 
     return [ Person ] 

# Create main view 

class DepartmentView(ModelView): 

    model = Instance(Department) 

    # The traits view to display: 
    view = View(
     VGroup(
      Item('object.model.name', style='readonly'), 
      Item('model', 
        editor = TreeEditor(editable = False, auto_open = 1) 
      ), 
      show_labels = False 
     ), 
     width  = 0.33, 
     height = 0.50, 
     resizable = True 
    ) 


# Create model object graph 

bob = Person(name='Bob') 
alice = Person(name='Alice') 
dept = Department(name='Sales', people=[bob, alice]) 

view = DepartmentView(model=dept) 

# Run the demo (if invoked form the command line): 
if __name__ == '__main__': 
    view.configure_traits() 
関連する問題