简体   繁体   English

traitsui文件对话框更改活动窗口

[英]traitsui file dialog changes active window

I have a large application that has many small windows. 我有一个带有许多小窗口的大型应用程序。 I wanted to use traitsui's file dialog to open some files from these windows. 我想使用traitsui的文件对话框从这些窗口中打开一些文件。 However, when I do, the file dialog correctly spawns and picks a file, but it also consistently switches the active window to an undesirable window after it is finished. 但是,当我这样做时,文件对话框会正确生成并选择一个文件,但是在完成后,它还会将活动窗口始终切换到不希望的窗口。 I am really confused why. 我真的很困惑为什么。

Here is a simplified test which displays the same problem: 这是显示相同问题的简化测试:

from traitsui.api import *
from traits.api import *
from traitsui.file_dialog import *

class BigApplication(HasTraits):
  subwindow=Instance(HasTraits)
  open_subwindow=Button('clickme')

  traits_view=View(Item(name='open_subwindow'),height=500,width=500)

  def _subwindow_default(self):
    return Subwindow()

  def _open_subwindow_fired(self):
    self.subwindow.edit_traits()

class Subwindow(HasTraits):
  f=File
  some_option = Bool
  openf=Button('Browse for file')

  traits_view=View(Item(name='f',style='text'),
                Item(name='some_option'),
                Item(name='openf'),buttons=OKCancelButtons)

  def _openf_fired(self):
    self.f=open_file()

BigApplication().configure_traits()

When open_file returns and selects the desired file, the active window is switched to the BigApplication window and not back to the Subwindow window (so that the user can select some additional options before clicking OK). 当open_file返回并选择所需的文件时,活动窗口将切换到BigApplication窗口,而不是子窗口窗口(以便用户可以在单击OK之前选择一些其他选项)。

I found a hacky workaround, as per usual. 和往常一样,我发现了一个骇人的解决方法。 But this behavior is still a bug. 但是这种行为仍然是一个错误。

The workaround is to dispose() of the old window and then to call edit_traits() on it. 解决方法是先dispose()旧窗口的dispose() ,然后在其上调用edit_traits() This edits the File trait and also happens to make it the active window. 这将编辑“ File特征,也恰巧使其成为活动窗口。 Disposing of the window manually has to be done inside the handler and is a little trickier than might be expected. 手动处理窗口必须在处理程序内部完成,这比预期的要复杂一些。

from traits.api import *
from traitsui.api import *
from traitsui.file_dialog import *

class BigApplication(Handler):
  subwindow=Instance(Handler)
  open_subwindow=Button('clickme')

  traits_view=View(Item(name='open_subwindow'),height=200,width=200)

  def _subwindow_default(self):
    return Subwindow()

  def _open_subwindow_fired(self):
    self.subwindow.edit_traits()

class Subwindow(Handler):
  f=File
  some_additional_option=Bool
  openf=Button('Browse')

  traits_view=View(Item(name='f',style='text'),
    Item(name='some_additional_option'),
    Item(name='openf'),
    buttons=OKCancelButtons)

  def _openf_fired(self):
    self.f=open_file()
    self.do_dispose(self.info)
    self.edit_traits()

  #handler methods
  def init_info(self,info):
    self.info=info
  def do_dispose(self,info):
    info.ui.dispose()

BigApplication().configure_traits()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM