Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should be able to share toggle Actions with images between menu items and toolbar buttons #239

Closed
ptomato opened this issue Aug 31, 2015 · 2 comments

Comments

@ptomato
Copy link

ptomato commented Aug 31, 2015

If you have an Action with the toggle style and an associated image, then you should theoretically be able to share that action between a menu item and a toolbar item. In fact it used to work, until wxWidgets disallowed it, and indeed it still works with the Qt4 backend.

Here's a sample program that reproduces the failure:

from traits.api import HasTraits
from traitsui.api import Action, Menu, MenuBar, ToolBar, View

class Window(HasTraits):
    action = Action(name='Action', style='toggle',
        image='/usr/share/icons/hicolor/16x16/actions/package-new.png')  # replace with some icon on your system
    view = View(menubar=MenuBar(Menu(action, name='File')),
        toolbar=ToolBar(action),
        resizable=True)

w = Window()
w.configure_traits()

And here's the stack trace it produces:

  File "actions.py", line 12, in <module>
    w.configure_traits()
  File "/usr/lib/python2.7/dist-packages/traits/has_traits.py", line 2386, in configure_traits
    kind, handler, id, scrollable, args )
  File "/usr/lib/python2.7/dist-packages/traitsui/wx/toolkit.py", line 219, in view_application
    id, scrollable, args )
  File "/usr/lib/python2.7/dist-packages/traitsui/wx/view_application.py", line 93, in view_application
    scrollable, args ).ui.result
  File "/usr/lib/python2.7/dist-packages/traitsui/wx/view_application.py", line 141, in __init__
    super( ViewApplication, self ).__init__(0)
  File "/usr/lib/python2.7/dist-packages/wx-3.0-gtk2/wx/_core.py", line 8628, in __init__
    self._BootstrapApp()
  File "/usr/lib/python2.7/dist-packages/wx-3.0-gtk2/wx/_core.py", line 8196, in _BootstrapApp
    return _core_.PyApp__BootstrapApp(*args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/traitsui/wx/view_application.py", line 158, in OnInit
    args       = self.args )
  File "/usr/lib/python2.7/dist-packages/traitsui/view.py", line 433, in ui
    ui.ui( parent, kind )
  File "/usr/lib/python2.7/dist-packages/traitsui/ui.py", line 221, in ui
    self.rebuild( self, parent )
  File "/usr/lib/python2.7/dist-packages/traitsui/wx/toolkit.py", line 140, in ui_live
    ui_live.ui_live( ui, parent )
  File "/usr/lib/python2.7/dist-packages/traitsui/wx/ui_live.py", line 68, in ui_live
    ui_dialog( ui, parent, NONMODAL )
  File "/usr/lib/python2.7/dist-packages/traitsui/wx/ui_live.py", line 99, in ui_dialog
    ui.owner.init( ui, parent, style )
  File "/usr/lib/python2.7/dist-packages/traitsui/wx/ui_live.py", line 299, in init
    self.add_menubar()
  File "/usr/lib/python2.7/dist-packages/traitsui/wx/ui_base.py", line 199, in add_menubar
    menubar.create_menu_bar( self.control, self ) )
  File "/usr/lib/python2.7/dist-packages/pyface/ui/wx/action/menu_bar_manager.py", line 49, in create_menu_bar
    menu = item.create_menu(parent, controller)
  File "/usr/lib/python2.7/dist-packages/pyface/ui/wx/action/menu_manager.py", line 58, in create_menu
    return _Menu(self, parent, controller)
  File "/usr/lib/python2.7/dist-packages/pyface/ui/wx/action/menu_manager.py", line 110, in __init__
    self.refresh()
  File "/usr/lib/python2.7/dist-packages/pyface/ui/wx/action/menu_manager.py", line 153, in refresh
    parent, group, previous_non_empty_group
  File "/usr/lib/python2.7/dist-packages/pyface/ui/wx/action/menu_manager.py", line 208, in _add_group
    item.add_to_menu(parent, self, self._controller)
  File "/usr/lib/python2.7/dist-packages/pyface/action/action_item.py", line 103, in add_to_menu
    wrapper = _MenuItem(parent, menu, self, controller)
  File "/usr/lib/python2.7/dist-packages/pyface/ui/wx/action/action_item.py", line 93, in __init__
    self.control.SetBitmap(action.image.create_bitmap())
  File "/usr/lib/python2.7/dist-packages/wx-3.0-gtk2/wx/_core.py", line 12543, in SetBitmap
    return _core_.MenuItem_SetBitmap(*args, **kwargs)
wx._core.PyAssertionError: C++ assertion "Assert failure" failed at ../src/gtk/menu.cpp(724) in SetBitmap(): only normal menu items can have bitmaps

I've opened a wxWidgets bug, since I think an assertion is quite rude to application developers in this case, but they have said this is by design and are not going to change it. Therefore, probably TraitsUI should avoid setting the bitmap on the menu item so that the above sample code can continue to work with the wxWidgets backend.

@corranwebster
Copy link
Contributor

Thanks for noting this. This probably needs to be fixed in pyface (where the base Action objects are defined). I'll open a ticket there.

@corranwebster
Copy link
Contributor

Closing in favour of the Pyface issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants