4

我正在使用 TraitsUI 开发应用程序。

我需要弹出一个窗口来请求一些值。

会有类似 ProcessValues 按钮和 SaveValues 按钮的东西。

  • 如果单击 ProcessValues,则应进行某些操作,然后应关闭窗口
  • 如果单击 SaveValues,则应执行其他操作,窗口也应关闭。

应该没有确定按钮

在下面的示例代码中,单击按钮会打印处理程序中的消息,但除了单击框架中的 [x] 之外,我不知道如何使窗口自行关闭。

重载 close() 似乎不起作用,因为它在单击 OK 后被调用。也许有一种方法可以生成 close_window 事件,或者它可能是别的东西。

有人可以帮忙吗?

from enthought.traits.api import HasTraits, Instance, Str, Int, List, Any, DelegatesTo
from enthought.traits.ui.api import Handler, View, Item, Action

class MyPanelHandler(Handler):
    def _process_values(self, info):
        #>>>reach process_values() through info and call
        print 'values processed OK'
        #>>> what goes here so that the window is closed?
    def _save_values(self, info):
        #>>>reach save_values() through info and call
        print 'values saved OK'
        #>>> what goes here so that the window is closed?

class MyPanel(HasTraits):
    model = Any
    name = Str
    age = Int
    process_values_button = Action(name = 'Process Values', action = '_process_values')
    save_values_button = Action(name = 'Save Params', action = '_save_values')

    view = View( 'name', 'age', handler = MyPanelHandler(),
            buttons = [process_values_button, save_values_button],)

class MyApp(HasTraits):
    panel = Instance(MyPanel)

    def __init__(self):
        self.panel = MyPanel(model = self)
    def get_values(self):
        self.panel.configure_traits()
    def save_values(self, name, age):
        print '... doing whatever to save values'
    def process_values(self):
        print '... doing whatever to process values'

if __name__ == '__main__':
    a = MyApp()
    a.get_values()
4

1 回答 1

7

我在其他地方找到了解决方案。要关闭窗口,单击调用 Handler 中方法的 Action,请从 Handler 调用“info.ui.dispose()”(其中 info 是传递给处理程序方法的唯一参数,而不是 self)。

这是修正后的虚拟程序;当心“if not info.initialized”部分——没有它,如果由于某种原因 uiinfo 对象需要很长时间才能填充,则可能会引发异常。

from enthought.traits.api import HasTraits, Instance, Str, Int, List, Any, DelegatesTo
from enthought.traits.ui.api import Handler, View, Item, Action

class MyPanelHandler(Handler):
    def _process_values(self, info):
        if not info.initialized: 
            return # do this in case info takes long to initialize
        # invoke methods
        info.object.model.process_values(info.object.name, info.object.age)
        print 'values have been processed'
        info.ui.dispose() # THIS IS WHAT I WAS ACTUALLY ASKING FOR
    def _save_values(self, info):
        if not info.initialized: return
        info.object.model.save_values(info.object.name, info.object.age)
        print 'values have been saved'
        info.ui.dispose()

class MyPanel(HasTraits):
    model = Any
    name = Str
    age = Int
    process_values_button = Action(name = 'Process Values', action = '_process_values')
    save_values_button = Action(name = 'Save Params', action = '_save_values')   
    view = View( 'name', 'age', handler = MyPanelHandler(),
            buttons = [process_values_button, save_values_button],)

class MyApp(HasTraits):
    panel = Instance(MyPanel)
    def __init__(self):
        self.panel = MyPanel(model = self)
    def get_values(self):
        self.panel.configure_traits()
    def save_values(self, name, age):
        print '... saving (%s, %s)' % (name, age)
    def process_values(self, name, age):
        print '... processing (%s, %s)' % (name, age)

if __name__ == '__main__':
    a = MyApp()
    a.get_values()
于 2012-06-15T10:55:46.190 回答