2013-10-09 67 views
1

我想在運行時修改guidata.dataset.dataitems.ChoiceItem顯示的選項。編輯guidata選項

例如:

from guidata.dataset.datatypes import DataSet 
from guidata.dataset.dataitems import ChoiceItem 

class Example(DataSet): 
    choiceBox = ChoiceItem("Example", [(0,'First'),(1,'Second'),(2,'Third')]) 

if __name__ == "__main__": 
    import guidata 
    _app = guidata.qapplication() 

    win = Example() 
    magic_function_to_change_choices(win, [(0,'A'), (1,'B'), (2,'C')]) 
    win.edit() 

回答

1

EDITED

我想出了一個辦法,我想好了很多。這會創建一個可以在運行時設置的變量類。

from guidata.dataset.datatypes import DataSet 
from guidata.dataset.dataitems import ChoiceItem, TextItem 

class ChoicesVariable(object): 
    def __init__(self): 
     self.choices = [(0,'First',None),(1,'Second',None),(2,'Third',None)] 
    def set(self, choices): 
     normalized = [] 
     for idx, c in enumerate(choices): 
      normalized.append(self._normalize_choice(idx, c)) 
     self.choices = normalized 
    def __call__(self, *args): 
     return self.choices 
    def _normalize_choice(self, idx, choice_tuple): 
     img = None 
     if isinstance(choice_tuple, tuple): 
      if len(choice_tuple) == 2: 
       key, value = choice_tuple 
      else: 
       key, value, img = choice_tuple 
     else: 
      key = idx 
      value = choice_tuple 
     if isinstance(value, str): 
      value = str(value)   
     return (key, value, img) 
choices = ChoicesVariable() 



class Example(DataSet): 
    choiceBox = ChoiceItem("Example", choices) 
    otherChoiceBox = ChoiceItem("Example2", [(0,'DontTouch'),(1,'Second'),(2,'Third')]) 
    text = TextItem("Example3") 

if __name__ == "__main__": 
    import guidata 
    _app = guidata.qapplication() 

    newChoices = [(0,'Aaa'),(1,'Bbb'),(2,'Ccc')] 
    choices.set(newChoices) 
    #Example._items[0].set_prop('data', choices=newChoices) 
    win = Example() 
    win.edit() 
    newChoices = [(0,'Alpha',None),(1,'Beta',None),(2,'Gamma',None)] 
    choices.set(newChoices) 
    win.edit()