0

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. 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).

aestrivex
  • 5,170
  • 2
  • 27
  • 44

1 Answers1

0

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. This edits the File trait and also happens to make it the active window. 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()
aestrivex
  • 5,170
  • 2
  • 27
  • 44