Hiding MenuBar temporarily

35 views
Skip to first unread message

cryzed cryzed

unread,
Sep 27, 2018, 3:11:47 PM9/27/18
to Enaml
Hello! First off Enaml is wonderful. I just discovered it recently but programming/designing with it is a blast.

I'm having a small issue. I wanted to create a small image viewer as an exercise. To this end I wanted to create an action that allows me to hide the MenuBar temporarily. However I quickly found out that when I toggle the visibility of the (PyQt5) widget, the actions aren't triggered anymore:

`menubar.proxy.widget.setVisible(not menu.proxy.widget.isVisible())`

I tried adding Actions directly to the MainWindow through Enaml, but these are not triggered at all. I ended up doing this:

```
Menu:
    title = '&View'
    Action:
        text = 'Toogle Menubar\tCtrl+M'
        triggered ::
            widget = menubar.proxy.widget.setVisible(not menu.proxy.widget.isVisible())
            for menu in menubar.menus():
                menu.visible = not menu.visible
```

which however leaves a small gap (the min-width of the menubar I assume) at the top. Is there a way to add actions to a MainWindow directly through Enaml?

cryzed

unread,
Sep 27, 2018, 3:13:06 PM9/27/18
to Enaml
The line should be: `menubar.proxy.widget.setVisible(not menubar.proxy.widget.isVisible())`

Matthieu Dartiailh

unread,
Sep 27, 2018, 3:14:21 PM9/27/18
to cryzed, Enaml
Could you post a complete example I am not sure to follow what you are trying to do ?

On Sep 27, 2018, at 3:13 PM, cryzed <cry...@gmail.com> wrote:

The line should be: `menubar.proxy.widget.setVisible(not menubar.proxy.widget.isVisible())`

--
You received this message because you are subscribed to the Google Groups "Enaml" group.
To unsubscribe from this group and stop receiving emails from it, send an email to enaml+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Message has been deleted

cryzed

unread,
Sep 27, 2018, 3:29:50 PM9/27/18
to Enaml
Yes, sorry, I'm not super used to Google Groups. I thought I could edit my posts.

This is my Enaml code:

```
from enaml.image import Image
from enaml.layout.api import hbox, vbox
from enaml.widgets.api import Action, Container, ImageView, Label, MainWindow, Menu, MenuBar, PushButton, StatusBar, StatusItem, FileDialogEx


enamldef MainWindow(MainWindow): window:
    title = 'Image Viewer'
    attr folder

    MenuBar: menubar:
        Menu:
            title = '&File'
            Action:
                text = 'Open...\tCtrl+O'
                triggered ::
                    folder.path = FileDialogEx.get_existing_directory()
            Action:
                separator = True
            Action:
                text = 'Close\tCtrl+C'
                triggered :: folder.path = ''
            Action:
                text = 'Exit\tCtrl+X'
                triggered :: exit()

        Menu:
            title = '&Help'
            Action:
                text = 'About...'
                triggered :: pass

        Menu:
            title = 'View'
            Action:
                text = 'Toogle Menubar\tCtrl+M'
                triggered :: menubar.proxy.widget.setVisible(not menubar.proxy.widget.isVisible())

        # Hidden menu used to implement key navigation
        Menu:
            visible = False
            Action:
                text = '\tLeft'
                triggered :: folder.previous()
            Action:
                text = '\ta'
                triggered:: folder.previous()
            Action:
                text = '\tRight'
                triggered :: folder.next()
            Action:
                text = '\td'
                triggered :: folder.next()

    Container:
        ImageView:
            # Make ImageView resizable
            resist_width = 'weak'
            resist_height = 'weak'
            scale_to_fit = True
            image << Image(data=folder.image_data, transform_mode='smooth')

    StatusBar:
        StatusItem:
            stretch = 1
            Label: status:
                text << 'Loaded: ' + folder.path

        StatusItem:
            Label:
                text = '(10/10)'
```

I want to hit Ctrl+M to toggle the visibility of the menubar. Once it is hidden, I can't toggle it again because the action is not triggered. The solution is to add the Action directly to the QMainWindow, which is not possible via Enaml. This is the workaround code I'm using right now to bind "free" Action objects of the MainWindow (shortly before starting the enaml application in main.py, after calling .show())

```
def add_qt_child_actions(view):
    for action in (child for child in view.children if isinstance(child, Action)):
        view.proxy.widget.addAction(action.proxy.widget)
```

cryzed

unread,
Sep 27, 2018, 3:51:53 PM9/27/18
to Enaml
A small update, this is what I ended up doing to get it working as intended:

Reply all
Reply to author
Forward
0 new messages