TraitsUi handler return True or False

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

TraitsUi handler return True or False

Adam Hughes
Hi all,

I'm restoring some old programs in TraitsUI and am trying to make a simple dialog that alerts the user of the pre-existence of a file.  I'd like the handler to return True or False when clicks Ok or Cancel, respectively.  I can't figure out how to return this, so have resorted to setting an intermediate trait called "overwrite" to store the boolean status after the user has pressed the button.  I also tried to overwrite the FileExistsHandler, but think I am probably better off just using my own Handler.  

Can you guys help me trim the fat off of this; it feels too intricate for what I need.  Thanks.

from enthought.traits.ui.file_dialog import FileExistsHandler

class FileOverwrite(FileExistsHandler):
    ''' See api
    Modified to delete files, not directories.'''
    
    file_name=Str()

    ok=Button('Yes')
    overwrite=Bool(False)
   
    view = View(VGroup(
                   HGroup(Item('handler.message', show_label = False, style = 'readonly', springy = True), 
                                   show_border = True),                   
                   HGroup( 
                           Item('handler.ok', show_label = False), 
                           Item('handler.cancel', show_label = False)
                         ), 
                   ), kind = 'popup', title='File already exists')

    def _message_default(self):
        return 'Warning: File %s will be overwritten, are you sure you want to continue?'%self.file_name


    def handler_ok_changed ( self, info ):
        """ Handles the user clicking the OK button.
        """
        info.ui.dispose( True )
        self.overwrite=True

    def handler_cancel_changed ( self, info ):
        """ Handles the user clicking the Cancel button.
        """
        info.ui.dispose( False )
        self.overwrite=False

    
a=FileOverwrite(file_name='testfile')
a.edit_traits(kind='modal')
print a.overwrite

--
Adam Hughes
Physics Ph.D Candidate
George Washington University


_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: TraitsUi handler return True or False

Corran Webster
Hi Adam,

I tend to use pyface for these simple/standard dialogs, in this case a pyface ConfirmationDialog would seem to be close to what you need, and a lot simpler.

If you need this level of complexity, then I think you can use the ui.result instead of using the handler_ok and handler_cancel, something like:

class FileOverwriteDialog(...):
     filename = File

     message = Str

     def _message_default(self):
         ...

     traits_view = View(
          Item('message', style='readonly'),
          buttons=OKCancelButtons,
     )


a = FileOverwriteDialog(...)
ui = a.edit_traits(kind='modal')
print ui.result

The final twist in all of this is that I recently fixed a bug where Handler's configure_traits() methods were not returning the result in the same way that a standard HasTraits class' configure_traits() method did - I don't think it affected edit_traits() methods, but I might have missed something.  So in other words, you might check out the github master traitsui as well.

-- Corran


On Mon, Dec 10, 2012 at 2:32 PM, Adam Hughes <[hidden email]> wrote:
Hi all,

I'm restoring some old programs in TraitsUI and am trying to make a simple dialog that alerts the user of the pre-existence of a file.  I'd like the handler to return True or False when clicks Ok or Cancel, respectively.  I can't figure out how to return this, so have resorted to setting an intermediate trait called "overwrite" to store the boolean status after the user has pressed the button.  I also tried to overwrite the FileExistsHandler, but think I am probably better off just using my own Handler.  

Can you guys help me trim the fat off of this; it feels too intricate for what I need.  Thanks.

from enthought.traits.ui.file_dialog import FileExistsHandler

class FileOverwrite(FileExistsHandler):
    ''' See api
    Modified to delete files, not directories.'''
    
    file_name=Str()

    ok=Button('Yes')
    overwrite=Bool(False)
   
    view = View(VGroup(
                   HGroup(Item('handler.message', show_label = False, style = 'readonly', springy = True), 
                                   show_border = True),                   
                   HGroup( 
                           Item('handler.ok', show_label = False), 
                           Item('handler.cancel', show_label = False)
                         ), 
                   ), kind = 'popup', title='File already exists')

    def _message_default(self):
        return 'Warning: File %s will be overwritten, are you sure you want to continue?'%self.file_name


    def handler_ok_changed ( self, info ):
        """ Handles the user clicking the OK button.
        """
        info.ui.dispose( True )
        self.overwrite=True

    def handler_cancel_changed ( self, info ):
        """ Handles the user clicking the Cancel button.
        """
        info.ui.dispose( False )
        self.overwrite=False

    
a=FileOverwrite(file_name='testfile')
a.edit_traits(kind='modal')
print a.overwrite

--
Adam Hughes
Physics Ph.D Candidate
George Washington University


_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev



_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: TraitsUi handler return True or False

Adam Hughes
Thanks a lot Corran, this is exactly what I was looking for.

On Mon, Dec 10, 2012 at 4:05 PM, Corran Webster <[hidden email]> wrote:
Hi Adam,

I tend to use pyface for these simple/standard dialogs, in this case a pyface ConfirmationDialog would seem to be close to what you need, and a lot simpler.

If you need this level of complexity, then I think you can use the ui.result instead of using the handler_ok and handler_cancel, something like:

class FileOverwriteDialog(...):
     filename = File

     message = Str

     def _message_default(self):
         ...

     traits_view = View(
          Item('message', style='readonly'),
          buttons=OKCancelButtons,
     )


a = FileOverwriteDialog(...)
ui = a.edit_traits(kind='modal')
print ui.result

The final twist in all of this is that I recently fixed a bug where Handler's configure_traits() methods were not returning the result in the same way that a standard HasTraits class' configure_traits() method did - I don't think it affected edit_traits() methods, but I might have missed something.  So in other words, you might check out the github master traitsui as well.

Thanks for letting me know.  Your class FileOverwriteDialog is a subclass of HasTraits, no?  EG:
 class FileOverwriteDialog(HasTraits) instead of FileOverwriteDIalog(Handler)
 

-- Corran


On Mon, Dec 10, 2012 at 2:32 PM, Adam Hughes <[hidden email]> wrote:
Hi all,

I'm restoring some old programs in TraitsUI and am trying to make a simple dialog that alerts the user of the pre-existence of a file.  I'd like the handler to return True or False when clicks Ok or Cancel, respectively.  I can't figure out how to return this, so have resorted to setting an intermediate trait called "overwrite" to store the boolean status after the user has pressed the button.  I also tried to overwrite the FileExistsHandler, but think I am probably better off just using my own Handler.  

Can you guys help me trim the fat off of this; it feels too intricate for what I need.  Thanks.

from enthought.traits.ui.file_dialog import FileExistsHandler

class FileOverwrite(FileExistsHandler):
    ''' See api
    Modified to delete files, not directories.'''
    
    file_name=Str()

    ok=Button('Yes')
    overwrite=Bool(False)
   
    view = View(VGroup(
                   HGroup(Item('handler.message', show_label = False, style = 'readonly', springy = True), 
                                   show_border = True),                   
                   HGroup( 
                           Item('handler.ok', show_label = False), 
                           Item('handler.cancel', show_label = False)
                         ), 
                   ), kind = 'popup', title='File already exists')

    def _message_default(self):
        return 'Warning: File %s will be overwritten, are you sure you want to continue?'%self.file_name


    def handler_ok_changed ( self, info ):
        """ Handles the user clicking the OK button.
        """
        info.ui.dispose( True )
        self.overwrite=True

    def handler_cancel_changed ( self, info ):
        """ Handles the user clicking the Cancel button.
        """
        info.ui.dispose( False )
        self.overwrite=False

    
a=FileOverwrite(file_name='testfile')
a.edit_traits(kind='modal')
print a.overwrite

--
Adam Hughes
Physics Ph.D Candidate
George Washington University


_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev



_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev




--
Adam Hughes
Physics Ph.D Candidate
George Washington University


_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: TraitsUi handler return True or False

Adam Hughes
One last thing Corran, sorry.  With the implementation you showed, is it possible to relable the buttons?  EG make OK go to "Yes" and CANCEL to "no"?

On Mon, Dec 10, 2012 at 7:32 PM, Adam Hughes <[hidden email]> wrote:
Thanks a lot Corran, this is exactly what I was looking for.

On Mon, Dec 10, 2012 at 4:05 PM, Corran Webster <[hidden email]> wrote:
Hi Adam,

I tend to use pyface for these simple/standard dialogs, in this case a pyface ConfirmationDialog would seem to be close to what you need, and a lot simpler.

If you need this level of complexity, then I think you can use the ui.result instead of using the handler_ok and handler_cancel, something like:

class FileOverwriteDialog(...):
     filename = File

     message = Str

     def _message_default(self):
         ...

     traits_view = View(
          Item('message', style='readonly'),
          buttons=OKCancelButtons,
     )


a = FileOverwriteDialog(...)
ui = a.edit_traits(kind='modal')
print ui.result

The final twist in all of this is that I recently fixed a bug where Handler's configure_traits() methods were not returning the result in the same way that a standard HasTraits class' configure_traits() method did - I don't think it affected edit_traits() methods, but I might have missed something.  So in other words, you might check out the github master traitsui as well.

Thanks for letting me know.  Your class FileOverwriteDialog is a subclass of HasTraits, no?  EG:
 class FileOverwriteDialog(HasTraits) instead of FileOverwriteDIalog(Handler)
 

-- Corran


On Mon, Dec 10, 2012 at 2:32 PM, Adam Hughes <[hidden email]> wrote:
Hi all,

I'm restoring some old programs in TraitsUI and am trying to make a simple dialog that alerts the user of the pre-existence of a file.  I'd like the handler to return True or False when clicks Ok or Cancel, respectively.  I can't figure out how to return this, so have resorted to setting an intermediate trait called "overwrite" to store the boolean status after the user has pressed the button.  I also tried to overwrite the FileExistsHandler, but think I am probably better off just using my own Handler.  

Can you guys help me trim the fat off of this; it feels too intricate for what I need.  Thanks.

from enthought.traits.ui.file_dialog import FileExistsHandler

class FileOverwrite(FileExistsHandler):
    ''' See api
    Modified to delete files, not directories.'''
    
    file_name=Str()

    ok=Button('Yes')
    overwrite=Bool(False)
   
    view = View(VGroup(
                   HGroup(Item('handler.message', show_label = False, style = 'readonly', springy = True), 
                                   show_border = True),                   
                   HGroup( 
                           Item('handler.ok', show_label = False), 
                           Item('handler.cancel', show_label = False)
                         ), 
                   ), kind = 'popup', title='File already exists')

    def _message_default(self):
        return 'Warning: File %s will be overwritten, are you sure you want to continue?'%self.file_name


    def handler_ok_changed ( self, info ):
        """ Handles the user clicking the OK button.
        """
        info.ui.dispose( True )
        self.overwrite=True

    def handler_cancel_changed ( self, info ):
        """ Handles the user clicking the Cancel button.
        """
        info.ui.dispose( False )
        self.overwrite=False

    
a=FileOverwrite(file_name='testfile')
a.edit_traits(kind='modal')
print a.overwrite

--
Adam Hughes
Physics Ph.D Candidate
George Washington University


_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev



_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev




--
Adam Hughes
Physics Ph.D Candidate
George Washington University




--
Adam Hughes
Physics Ph.D Candidate
George Washington University


_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: TraitsUi handler return True or False

Brennan Williams
On 11/12/2012 3:09 p.m., Adam Hughes wrote:
One last thing Corran, sorry.  With the implementation you showed, is it possible to relable the buttons?  EG make OK go to "Yes" and CANCEL to "no"?

In a Traits dialog I use buttons defined using Action

from traitsui.menu import Action
...
myButton=Action(name='Click Me',action='_clickme_clicked')
....
and in the view definition,

buttons=[myButton,'Cancel'],

but I don't know if this will work in a pyface dialog.

Brennan



On Mon, Dec 10, 2012 at 7:32 PM, Adam Hughes <[hidden email]> wrote:
Thanks a lot Corran, this is exactly what I was looking for.

On Mon, Dec 10, 2012 at 4:05 PM, Corran Webster <[hidden email]> wrote:
Hi Adam,

I tend to use pyface for these simple/standard dialogs, in this case a pyface ConfirmationDialog would seem to be close to what you need, and a lot simpler.

If you need this level of complexity, then I think you can use the ui.result instead of using the handler_ok and handler_cancel, something like:

class FileOverwriteDialog(...):
     filename = File

     message = Str

     def _message_default(self):
         ...

     traits_view = View(
          Item('message', style='readonly'),
          buttons=OKCancelButtons,
     )


a = FileOverwriteDialog(...)
ui = a.edit_traits(kind='modal')
print ui.result

The final twist in all of this is that I recently fixed a bug where Handler's configure_traits() methods were not returning the result in the same way that a standard HasTraits class' configure_traits() method did - I don't think it affected edit_traits() methods, but I might have missed something.  So in other words, you might check out the github master traitsui as well.

Thanks for letting me know.  Your class FileOverwriteDialog is a subclass of HasTraits, no?  EG:
 class FileOverwriteDialog(HasTraits) instead of FileOverwriteDIalog(Handler)
 

-- Corran


On Mon, Dec 10, 2012 at 2:32 PM, Adam Hughes <[hidden email]> wrote:
Hi all,

I'm restoring some old programs in TraitsUI and am trying to make a simple dialog that alerts the user of the pre-existence of a file.  I'd like the handler to return True or False when clicks Ok or Cancel, respectively.  I can't figure out how to return this, so have resorted to setting an intermediate trait called "overwrite" to store the boolean status after the user has pressed the button.  I also tried to overwrite the FileExistsHandler, but think I am probably better off just using my own Handler.  

Can you guys help me trim the fat off of this; it feels too intricate for what I need.  Thanks.

from enthought.traits.ui.file_dialog import FileExistsHandler

class FileOverwrite(FileExistsHandler):
    ''' See api
    Modified to delete files, not directories.'''
    
    file_name=Str()

    ok=Button('Yes')
    overwrite=Bool(False)
   
    view = View(VGroup(
                   HGroup(Item('handler.message', show_label = False, style = 'readonly', springy = True), 
                                   show_border = True),                   
                   HGroup( 
                           Item('handler.ok', show_label = False), 
                           Item('handler.cancel', show_label = False)
                         ), 
                   ), kind = 'popup', title='File already exists')

    def _message_default(self):
        return 'Warning: File %s will be overwritten, are you sure you want to continue?'%self.file_name


    def handler_ok_changed ( self, info ):
        """ Handles the user clicking the OK button.
        """
        info.ui.dispose( True )
        self.overwrite=True

    def handler_cancel_changed ( self, info ):
        """ Handles the user clicking the Cancel button.
        """
        info.ui.dispose( False )
        self.overwrite=False

    
a=FileOverwrite(file_name='testfile')
a.edit_traits(kind='modal')
print a.overwrite

--
Adam Hughes
Physics Ph.D Candidate
George Washington University


_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev



_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev




--
Adam Hughes
Physics Ph.D Candidate
George Washington University




--
Adam Hughes
Physics Ph.D Candidate
George Washington University



_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev


_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: TraitsUi handler return True or False

Corran Webster
In reply to this post by Adam Hughes
On Mon, Dec 10, 2012 at 6:32 PM, Adam Hughes <[hidden email]> wrote:
Thanks a lot Corran, this is exactly what I was looking for.

On Mon, Dec 10, 2012 at 4:05 PM, Corran Webster <[hidden email]> wrote:
Hi Adam,

I tend to use pyface for these simple/standard dialogs, in this case a pyface ConfirmationDialog would seem to be close to what you need, and a lot simpler.

If you need this level of complexity, then I think you can use the ui.result instead of using the handler_ok and handler_cancel, something like:

class FileOverwriteDialog(...):
     filename = File

     message = Str

     def _message_default(self):
         ...

     traits_view = View(
          Item('message', style='readonly'),
          buttons=OKCancelButtons,
     )


a = FileOverwriteDialog(...)
ui = a.edit_traits(kind='modal')
print ui.result

The final twist in all of this is that I recently fixed a bug where Handler's configure_traits() methods were not returning the result in the same way that a standard HasTraits class' configure_traits() method did - I don't think it affected edit_traits() methods, but I might have missed something.  So in other words, you might check out the github master traitsui as well.

Thanks for letting me know.  Your class FileOverwriteDialog is a subclass of HasTraits, no?  EG:
 class FileOverwriteDialog(HasTraits) instead of FileOverwriteDIalog(Handler)

Either would work, I think, but HasTraits is simpler in this case.

For comparison, the pyface version is something like:

from pyface.api import ConfirmationDialog, YES

a = ConfirmationDialog(message='Overwrite File?')
result = a.open()
print result == YES

-- Corran


_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: TraitsUi handler return True or False

Corran Webster
In reply to this post by Brennan Williams
On Mon, Dec 10, 2012 at 9:04 PM, Brennan Williams <[hidden email]> wrote:
On 11/12/2012 3:09 p.m., Adam Hughes wrote:
One last thing Corran, sorry.  With the implementation you showed, is it possible to relable the buttons?  EG make OK go to "Yes" and CANCEL to "no"?

In a Traits dialog I use buttons defined using Action

from traitsui.menu import Action
...
myButton=Action(name='Click Me',action='_clickme_clicked')
....
and in the view definition,

buttons=[myButton,'Cancel'],

but I don't know if this will work in a pyface dialog.

Won't work that way for pyface - it's just a trait-ified wrapper around the toolkit native dialogs, but the ConfirmationDialog constructor has arguments which let you specify button text (default is 'Yes' and 'No' with an optional 'Cancel').

Did a little digging, and it looks like it's harder for the TraitsUI approach as various pieces of code appear to be looking for the actual button name (as opposed to the id...) as being 'OK', so you'd need to do a fair amount of work to get the OK button to have text 'Yes', I fear.

-- Corran

_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: TraitsUi handler return True or False

Adam Hughes
Thanks for looking into that for me Corran.  If I absolutely need it, I can resort to my original implementation, but I can live with OK Cancel and a succinct class.

I appreciate your help.

On Tue, Dec 11, 2012 at 12:17 AM, Corran Webster <[hidden email]> wrote:
On Mon, Dec 10, 2012 at 9:04 PM, Brennan Williams <[hidden email]> wrote:
On 11/12/2012 3:09 p.m., Adam Hughes wrote:
One last thing Corran, sorry.  With the implementation you showed, is it possible to relable the buttons?  EG make OK go to "Yes" and CANCEL to "no"?

In a Traits dialog I use buttons defined using Action

from traitsui.menu import Action
...
myButton=Action(name='Click Me',action='_clickme_clicked')
....
and in the view definition,

buttons=[myButton,'Cancel'],

but I don't know if this will work in a pyface dialog.

Won't work that way for pyface - it's just a trait-ified wrapper around the toolkit native dialogs, but the ConfirmationDialog constructor has arguments which let you specify button text (default is 'Yes' and 'No' with an optional 'Cancel').

Did a little digging, and it looks like it's harder for the TraitsUI approach as various pieces of code appear to be looking for the actual button name (as opposed to the id...) as being 'OK', so you'd need to do a fair amount of work to get the OK button to have text 'Yes', I fear.

-- Corran

_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev




--
Adam Hughes
Physics Ph.D Candidate
George Washington University


_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev