如何在从另一段代码作为实例调用对象特征时显示它们

时间:2013-10-31 07:29:40

标签: python enthought traitsui

我刚刚开始使用TraitsUI,而且我是初学者Python程序员。我希望这个问题不是太低级。

我想在ControlPanel中显示一个设置按钮,从Main调用它。当我执行以下操作时,我只是一个带有“Panel”按钮的窗口。如果我单击该按钮,我会得到另一个窗口,其中包含我想要的“开始”按钮。如何使用“开始”按钮获取窗口?

谢谢, 科斯莫

主:

from enthought.traits.api import *
from enthought.traits.ui.api import *

class ControlPanel(HasTraits):
    """ This object is the core of the traitsUI interface. It hosts the method for
    interaction between the objects and the GUI.
    """

    start = Button("Start Measurements") 
    view = View(Item('start', show_label=False, style='custom' )) 

class MainWindow(HasTraits):
 """ The main window, here go the instructions to create and destroy the application. """

    panel = Instance(ControlPanel)
    def _panel_default(self):
        return ControlPanel()
    view = View(Item('panel'))

if __name__ == '__main__':
    MainWindow().configure_traits() 

1 个答案:

答案 0 :(得分:0)

MainWindow中,更改此内容:

    view = View(Item('panel'))

到此:

    view = View(Item('panel', style='custom'))

有关详细信息,请参阅InstanceEditor()文档。该文档的相关部分是屏幕截图下方的段落。 simple的{​​{1}}样式(这是默认样式)会创建一个按钮,单击该按钮会打开一个包含实例视图的新窗口。 InstanceEditor样式将实例的视图嵌入到包含该项的同一窗口中。图36中的屏幕截图显示了一个例子。屏幕截图的顶部显示custom样式,下面是simple样式。 (以下是customtext样式,但除了调试之外它们不是很有用。)