9 Feb 16:51
More on: How to dispose of a GUI
Dear all,
I posted a request yesterday and was able to partially solve the problem.
Let me explain what the problem is.
A main GUI (GUI #1) is displayed with a single button. When the user
pushes the button, another GUI (GUI #2) appears with a Save button on it. When the user
presses the Save button, a dialog box appears and prompts the user to select a file to be saved.
When the user presses Save on the dialog box, the dialog box disappears of the screen as it should. Is
there a way that GUI #2 disappears as well?
I include below an example. Notice that I partially solve the problem by adding "info.ui.dispose()"
in the handler. Unfortunately, this is not satisfactory for me. The problem is that once GUI #2 is gone,
there is no trace of its attributes. To see that, I include in the example two print statements:
(1) print 'a', filename #(inside GUI #2)
(2) print 'b', filename #(inside GUI #1, after disposing of GUI #2)
Have a look at the example:
from enthought.traits.api import HasTraits, Button, Str
from enthought.pyface.api import FileDialog, OK
from enthought.traits.ui.api import View, Item, Handler
from enthought.traits.ui.menu import Action
class UI_Handler(Handler):
def save(self, info):
""" Save location info to location file and load the location info """
info.object.save()
info.ui.dispose()
class MainClass(HasTraits):
myButton = Button(label = 'My Button',
height_padding = 30,
orientation = 'horizontal')
traits_view = View(Item('myButton', show_label=False, style='custom'))
def __init__(self, **traits):
super(MainClass, self).__init__(**traits)
def _myButton_fired(self):
sc = SubClass()
print 'b', sc.filename
class SubClass(HasTraits):
saveButton = Action(name='Save',action='save')
filename = Str
traits_view = View(
handler = UI_Handler(),
buttons=[saveButton],
resizable = True,
kind = 'modal')
def __init__(self, **traits):
super(SubClass, self).__init__(**traits)
self.configure_traits()
def save(self):
dialog = FileDialog(title='Save File',
action='save as')
if dialog.open() == OK:
self.filename = dialog.path
print 'a', self.filename
if __name__ == "__main__":
main_object = MainClass()
main_object.configure_traits()
Regards,
Sylvain
RSS Feed