有没有办法通过多个数据项属性函数来激活Python guidata数据项?

huangapple go评论91阅读模式
英文:

Is there a way to have a python guidata dataitem activated by more than one dataitem attribute function property?

问题

I would like a python guidata dataitem to be activated based on the property of more than one other ChoiceItem attribute function. Is there a way to link together the properties of ChoiceItems to affect the activation of another dataitem in the interface?

For example, in the output from the below code, I would like the interface to activate 'Choice D' if 'Choice B' is toggled True, even if 'Choice A' is set to 'A2'. At the moment, 'Choice D' only activates if 'Choice A' is NOT set to 'A2', because the 'Choice D' ChoiceItem is linked only to the property affected by 'Choice A'.

import sys
import guidata.dataset.datatypes as gdt
import guidata.dataset.dataitems as gdi
from PyQt5.QtWidgets import QApplication


# initialise QApplication
_in_app = QApplication(sys.argv)

class Input(gdt.DataSet):
    
    _prop1 = gdt.GetAttrProp('select_chA')
    choicesA = (('a1', "A1"), ('a2', "A2"))
    select_chA = gdi.ChoiceItem("Choice A:", choicesA, default='a2').set_prop('display', store=_prop1)
        
    _prop2 = gdt.GetAttrProp('select_chB')
    select_chB = gdi.BoolItem("Choice B:", default=False).set_prop('display', store=_prop2)
    choicesC = (('c1', "C1"),('c2', "C2"))
    select_chC = gdi.ChoiceItem("Choice C:",
                                choicesC).set_prop('display',
                                                   active=gdt.FuncProp(_prop2,
                                                                       lambda x: x != False))
    choicesD = (('d1', "D1"), ('d2', "D2"), ('d3', "D3"))
    
    select_chD = gdi.ChoiceItem("Choice D:",
                                choicesD).set_prop('display', active=gdt.FuncProp(_prop1,
                                                                        lambda x: x == 'a1'))
inputs = Input()
input_dialog = inputs.edit()
英文:

I would like a python guidata dataitem to be activated based on the property of more than one other ChoiceItem attribute function. Is there a way to link together the properties of ChoiceItems to affect the activation of another dataitem in the interface?

For example, in the output from the below code, I would like the interface to activate 'Choice D' if 'Choice B' is toggled True, even if 'Choice A' is set to 'A2'. At the moment, 'Choice D' only activates if 'Choice A' is NOT set to 'A2', because the 'Choice D' ChoiceItem is linked only to the property affected by 'Choice A'.

import sys
import guidata.dataset.datatypes as gdt
import guidata.dataset.dataitems as gdi
from PyQt5.QtWidgets import QApplication


# initialise QApplication
_in_app = QApplication(sys.argv)

class Input(gdt.DataSet):
    
    _prop1 = gdt.GetAttrProp('select_chA')
    choicesA = (('a1', "A1"), ('a2', "A2"))
    select_chA = gdi.ChoiceItem("Choice A:", choicesA, default='a2').set_prop('display', store=_prop1)
        
    _prop2 = gdt.GetAttrProp('select_chB')
    select_chB = gdi.BoolItem("Choice B:", default=False).set_prop('display', store=_prop2)
    choicesC = (('c1', "C1"),('c2', "C2"))
    select_chC = gdi.ChoiceItem("Choice C:",
                                choicesC).set_prop('display',
                                                   active=gdt.FuncProp(_prop2,
                                                                       lambda x: x != False))
    choicesD = (('d1', "D1"), ('d2', "D2"), ('d3', "D3"))
    
    select_chD = gdi.ChoiceItem("Choice D:",
                                choicesD).set_prop('display', active=gdt.FuncProp(_prop1,
                                                                        lambda x: x == 'a1'))
inputs = Input()
input_dialog = inputs.edit()

答案1

得分: 0

That Guidata library seems to be an opinionated one. As always for such libraries, you need a hacky solution if you want a feature that's not readymade.

In this case, you can use the undocumented ItemProperty. Its constructor expects a callable argument, which is called when the property is evaluated. (See the source for details.) So, do like the following example.

...
import guidata.dataset.datatypes as gdt
import guidata.dataset.dataitems as gdi
...
class Input(gdt.DataSet):
    
    _prop1 = gdt.GetAttrProp('select_chA')
    ...
    select_chA = gdi.ChoiceItem(...).set_prop('display', store=_prop1)

    _prop2 = gdt.GetAttrProp('select_chB')
    select_chB = gdi.BoolItem(...).set_prop('display', store=_prop2)

    ...
    select_chD = gdi.ChoiceItem(...).set_prop('display', active=
        gdt.ItemProperty(lambda *args:
			Input._prop1(*args) == 'a1' or Input._prop2(*args)))
...
英文:

That Guidata library seems to be an opinionated one. As always for such libraries, you need a hacky solution if you want a feature that's not readymade.

In this case, you can use the undocumented ItemProperty. Its constructor expects a callable argument, which is called when the property is evaluated. (See the source for details.) So, do like the following example.

...
import guidata.dataset.datatypes as gdt
import guidata.dataset.dataitems as gdi
...
class Input(gdt.DataSet):
    
    _prop1 = gdt.GetAttrProp('select_chA')
    ...
    select_chA = gdi.ChoiceItem(...).set_prop('display', store=_prop1)

    _prop2 = gdt.GetAttrProp('select_chB')
    select_chB = gdi.BoolItem(...).set_prop('display', store=_prop2)

    ...
    select_chD = gdi.ChoiceItem(...).set_prop('display', active=
        gdt.ItemProperty(lambda *args:
			Input._prop1(*args) == 'a1' or Input._prop2(*args)))
...

huangapple
  • 本文由 发表于 2023年4月17日 01:53:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76029442.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定