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()