我正在使用TraitsUI开发应用程序.
我需要弹出一个窗口来请求一些值.
会有类似ProcessValues按钮和SaveValues按钮的东西.
>如果单击ProcessValues,则应该执行某些操作,然后应关闭窗口
>如果单击SaveValues,则应该执行其他操作,窗口也应该关闭.
应该没有OK按钮
在下面的示例代码中,单击按钮会打印处理程序中的消息,但我不知道如何使窗口自动关闭,而不是单击框架中的[x].
重载close()似乎不起作用,因为它被称为AFTER 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()
解决方法:
我在别处找到了解决方案.要关闭窗口,单击调用处理程序中的方法的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()