阅读有关使用TraitsUI应用MVC模式的文档,我阅读了示例MVC_demo.py。现在,我想知道如何管理多个“MVC”。 我想写一些“includeallMVCs.py”并有类似的东西:
import MyViewController1, MyViewController2, MyViewController2
class IncludeallMVCs(HasTraits):
view = Include(MyViewController1, MyViewController2, MyViewController3)
其中MyViewController,MyViewController,MyViewController是类似MVC_demo示例的类。
因此,我们的想法是将不同视图与其控制器分开,然后在一个“通用”视图中“加入”所有视图。
答案 0 :(得分:0)
在示例中搜索我找到了这个:Dynamic Forms Using Instances 然后我修改它以分离AdultHandler。在这个例子中,我使用的工资变量由AdultHandler控制。
文件: adult.py
from traitsui.api import Handler
class AdultHandler(Handler):
def object_salary_changed (self, info):
if (info.object.salary >= 20000):
print "Good Salary!"
else:
print "Bad Salary"
文件: adult_model.py
from adult import AdultHandler
class AdultSpec ( HasTraits ):
""" Trait list for adults (assigned to 'misc' for a Person when age >= 18).
"""
marital_status = Enum( 'single', 'married', 'divorced', 'widowed' )
registered_voter = Bool
military_service = Bool
salary = Int
traits_view = View(
'marital_status',
'registered_voter',
'military_service',
'salary',
handler = AdultHandler()
)
if __name__ == '__main__':
a = AdultSpec()
a.configure_traits()
文件: main.py
from adult_model import AdultSpec
class Person ( HasTraits ):
""" Demo class for demonstrating dynamic interface restructuring.
"""
datainput = Instance( AdultSpec )
# Interface for attributes that depend on the value of 'age':
spec_group = Group(
Group(
Item( name = 'datainput', style = 'custom' ),
show_labels = False
),
label = 'Additional Info',
show_border = True
)
# A simple View is enough as long as the right handler is specified:
view = View(
Group(
spec_group
),
title = 'Using MVC',
buttons = [ 'OK' ],
resizable = True,
width = 300
)
# Create the demo:
demo = Person( datainput = AdultSpec() )
# Run the demo (if invoked from the command line):
if __name__ == '__main__':
demo.configure_traits()