python - 关闭 TraitsUI 窗口而不单击确定

标签 python traits

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

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

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

  • 如果点击 ProcessValues,应该做一些事情,然后关闭窗口
  • 如果单击 SaveValues,则应执行其他操作,窗口也应关闭。

不应该有确定按钮

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

重载 close() 似乎无法解决问题,因为它是在单击“确定”后调用的。可能有一种方法可以生成 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()

最佳答案

我在其他地方找到了解决方案。要关闭窗口单击调用处理程序中的方法的操作,请从处理程序调用“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()

关于python - 关闭 TraitsUI 窗口而不单击确定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11036985/

相关文章:

c++ - 当只有少数成员存在差异时,是否有必要对整个类(class)进行特化?

module - 错误[E0277] : the trait bound `my_struct::MyStruct: my_trait::MyTrait` is not satisfied

python访问字典中字典中的元素

scala - 如何在 subtrait 中初始化 trait 的 vals?

python - 如何在 Python 中将毫秒时间戳转换为正常日期?

javascript - python x[1 :2:3] in Javascript?

generics - 提供基于特征类型参数的默认特征实现

php - 如果存在,我如何有条件地使类使用特征?

python - 有人可以解释一下 np.log 是做什么的吗?

python - Django - 评级系统 View 和模板