是否可以在 Traitsui 类中的视图之间切换?
我正在用 Enthought 的 TraitsUI 用 python 编写一个 GUI 程序。我希望能够在同一对象的不同视图之间切换以响应用户的操作。可以这样做吗?
以下大致演示了我想要做的事情:
from enthought.traits.api import HasTraits, Button
from enthought.traits.ui.api import View, Item
class Test(HasTraits):
button1 = Button('Button 1')
button2 = Button('Button 2')
button3 = Button('Button 3')
traits_view = View(Item('button1'), Item('button2'))
view2 = View(Item('button1'), Item('button3'))
def _button1_fired(self):
#select view2
pass
def _button3_fired(self):
#select traits_view
pass
if __name__ == "__main__":
test = Test()
test.configure_traits()
I'm writing a GUI program in python with TraitsUI from Enthought. I want to be able to switch between different Views of the same object in response to a user's action. Is it possible to do that?
The following approximately demonstrates what I want to do:
from enthought.traits.api import HasTraits, Button
from enthought.traits.ui.api import View, Item
class Test(HasTraits):
button1 = Button('Button 1')
button2 = Button('Button 2')
button3 = Button('Button 3')
traits_view = View(Item('button1'), Item('button2'))
view2 = View(Item('button1'), Item('button3'))
def _button1_fired(self):
#select view2
pass
def _button3_fired(self):
#select traits_view
pass
if __name__ == "__main__":
test = Test()
test.configure_traits()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许您可以不通过切换视图来实现您想要的效果,而是通过在作为视图一部分的组的定义中使用visible_when关键字来实现。例子中有一些启发:
示例/demo/Dynamic_Forms/visible_when.py
Maybe you can achieve the effect you desire not by switching views but by use of the visible_when keyword in the definition of a Group that's part of your view. There is some inspiration in the examples:
examples/demo/Dynamic_Forms/visible_when.py