"installing" a menu into a UI using a handler

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

"installing" a menu into a UI using a handler

Brad Buran
I'm trying to figure out how to install a menu into a UI using a handler.  The code at the bottom appears to work (somewhat) when using a WX backend, except I get a recursion error (http://pastebin.com/iLgPPuk7).  When using the QT4 backend, I get a 'parent' not defined error (http://pastebin.com/11T83fkJ).  The code I am trying to run is below (and also at http://pastebin.com/vwGUTpKU). 

Any pointers?

Thanks,
Brad

from enthought.traits.api import *
from enthought.traits.ui.api import *
from enthought.traits.ui.menu import Action, Menu, MenuBar

class MainHandler(Handler):
   
    def init(self, info):
        actions = [Action(name='Load file', action='load_file'),
                   Action(name='Save file', action='save_file'),
                   Action(name='Save as file', action='saveas_file'), ]
        info.ui.view.menubar = MenuBar(Menu(*actions, name='File'))
        info.ui.updated = True
       
class MainWindow(HasTraits):
   
    a = Float
    b = Float
   
    view = View('a', 'b')
   
if __name__ == '__main__':
   
    MainWindow().configure_traits(handler=MainHandler)

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

Re: "installing" a menu into a UI using a handler

bryce hendrix-2
On 04/20/2010 10:28 AM, Brad Buran wrote:
> I'm trying to figure out how to install a menu into a UI using a
> handler.  The code at the bottom appears to work (somewhat) when using
> a WX backend, except I get a recursion error
> (http://pastebin.com/iLgPPuk7).  When using the QT4 backend, I get a
> 'parent' not defined error (http://pastebin.com/11T83fkJ).  The code I
> am trying to run is below (and also at http://pastebin.com/vwGUTpKU).
>
> Any pointers?
>

I don't know why you get errors, it would take a bit of digging through
the backend code. If you can explain what you're trying to do we might
be able to suggest some options.

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

Re: "installing" a menu into a UI using a handler

Brad Buran
I have a series of files that are used to store information for various Python objects used in our experiments.  I created a generic FileHandler (subclassed from enthought.traits.ui.api.
Handler) to control the logic of loading, saving and closing these files (a lot of this logic was redundant such as ensuring that the dot extension was added properly on "save as", making sure the user does accidentally lose changes when they close the window, and generating confirmation, warning and error dialogs on common errors and problems that arise).  Each Python object can then subclass this FileHandler method and define the load_object and save_object methods to control the actual logic of saving and loading the data without having to worry about the file name.

Since the popup window for editing the Python object will need a "load file", "save file" and "save as file" menu, I thought it would make sense to just dynamically add this to the menubar when the FileHandler is installed into the HasTraits object.

The FileHandler code is at http://pastebin.com/bHanLJFz.  A subclassed FileHandler is at http://pastebin.com/U0B08W0m.  I could try making a stand-alone version if you want, but that will take a bit longer to remove the dependencies I've included.

Brad


On Tue, Apr 20, 2010 at 11:32 AM, bryce hendrix <[hidden email]> wrote:
On 04/20/2010 10:28 AM, Brad Buran wrote:
I'm trying to figure out how to install a menu into a UI using a handler.  The code at the bottom appears to work (somewhat) when using a WX backend, except I get a recursion error (http://pastebin.com/iLgPPuk7).  When using the QT4 backend, I get a 'parent' not defined error (http://pastebin.com/11T83fkJ).  The code I am trying to run is below (and also at http://pastebin.com/vwGUTpKU).

Any pointers?


I don't know why you get errors, it would take a bit of digging through the backend code. If you can explain what you're trying to do we might be able to suggest some options.

Bryce


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

Re: "installing" a menu into a UI using a handler

Brad Buran
In reply to this post by bryce hendrix-2
I took a look at the code in enthought.traits.ui.qt4.toolkit.py for the relevant code.  On line 322:

    def rebuild_ui ( self, ui ):
        """ Rebuilds a UI after a change to the content of the UI.
        """
        if ui.control is not None:
            ui.recycle()
            ui.info.ui = ui
        ui.rebuild( ui, parent )

As you can see, parent is never defined.  Looking at the WX version, it appears that an attempt is made to obtain the parent from the underlying WX control.  If no parent can be obtained, parent is set to None.

Brad

On Tue, Apr 20, 2010 at 11:32 AM, bryce hendrix <[hidden email]> wrote:
On 04/20/2010 10:28 AM, Brad Buran wrote:
I'm trying to figure out how to install a menu into a UI using a handler.  The code at the bottom appears to work (somewhat) when using a WX backend, except I get a recursion error (http://pastebin.com/iLgPPuk7).  When using the QT4 backend, I get a 'parent' not defined error (http://pastebin.com/11T83fkJ).  The code I am trying to run is below (and also at http://pastebin.com/vwGUTpKU).

Any pointers?


I don't know why you get errors, it would take a bit of digging through the backend code. If you can explain what you're trying to do we might be able to suggest some options.

Bryce


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

Re: "installing" a menu into a UI using a handler

bryce hendrix-2
On 05/03/2010 07:46 PM, Brad Buran wrote:
I took a look at the code in enthought.traits.ui.qt4.toolkit.py for the relevant code.  On line 322:

    def rebuild_ui ( self, ui ):
        """ Rebuilds a UI after a change to the content of the UI.
        """
        if ui.control is not None:
            ui.recycle()
            ui.info.ui = ui
        ui.rebuild( ui, parent )

As you can see, parent is never defined.  Looking at the WX version, it appears that an attempt is made to obtain the parent from the underlying WX control.  If no parent can be obtained, parent is set to None.

Brad


Thanks Brad, I have check in a fix (though I am not entirely sure it is the right one, since I can only get it to execute in error).

As to the sample code you pasted a couple of weeks ago (sorry it took me so long to look at it), I think the problem is that in the init method of the handler you are adding a menubar, which causes the rebuild_ui method to be called, which in turn causes the init method of your handler to be called. The simplest solution if to put the menu logic in init_info, which is called before init()

Bryce

from enthought.traits.api import *
from enthought.traits.ui.api import *
from enthought.traits.ui.menu import Action, Menu, MenuBar

class MainHandler(Handler):

    def init_info(self, info):
        actions = [Action(name='Load file', action='load_file'),
                   Action(name='Save file', action='save_file'),
                   Action(name='Save as file', action='saveas_file'), ]
        info.ui.view.menubar = MenuBar(Menu(*actions, name='File'))
        info.ui.updated = True

class MainWindow(HasTraits):

    a = Float
    b = Float

    view = View('a', 'b')

if __name__ == '__main__':

    MainWindow().configure_traits(handler=MainHandler)


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