Skip to content

Instantly share code, notes, and snippets.

@mo-han
Last active February 5, 2021 04:54
Show Gist options
  • Save mo-han/af1d5b5eafd4f069e74814368d5cbb0a to your computer and use it in GitHub Desktop.
Save mo-han/af1d5b5eafd4f069e74814368d5cbb0a to your computer and use it in GitHub Desktop.
traitsui large icon button test
from mylib.traitsui_helper.import_10_qt import * # mylib: https://github.com/mo-han/mo-han-toolbox/tree/master/mylib
i = ImageResource(r'scpi_shell\icon.png')
TestAction = Action(
name="&Test", action="test_clicked", tooltip="Click to test", image=i, height=-1280
)
class DialogWithToolbar(HasTraits):
"""Test dialog with toolbar and menu."""
action_successful = Bool(False)
def test_clicked(self):
self.action_successful = True
menus = MenuBar(
Menu(
ActionGroup(TestAction),
name="Test"
)
)
bt1 = Button(image=i)
bt2 = Button(button_editor=ButtonEditor(image=i))
bt3 = ToolbarButton(image=i)
toolbar = ToolBar(ActionGroup(TestAction))
traits_view = View(
Item(
label="Click the button on the toolbar or the menu item.\n"
"The 'Action successful' element should turn to True."
),
Item("action_successful", style="readonly", label='clicked'),
Item(oa(bt1), style='custom', height=-200, width=-200), # `oa` is a wrapper of `varname.nameof`, oa(x.y.z) == str('x.y.z')
Item(oa(bt2), style='custom', height=-200, width=-200),
Item(oa(bt3), style='custom', height=-200, width=-200),
menubar=menus,
toolbar=toolbar,
buttons=[TestAction, "OK", ],
)
def _bt_fired(self):
self.action_successful = not self.action_successful
if __name__ == "__main__":
# Execute from command line for manual testing
vw = DialogWithToolbar()
vw.configure_traits()
@mo-han
Copy link
Author

mo-han commented Feb 5, 2021

图片

@mo-han
Copy link
Author

mo-han commented Feb 5, 2021

Success with SVGButton in enable

from mylib.traitsui_helper.import_10_qt import *  # mylib: https://github.com/mo-han/mo-han-toolbox/tree/master/mylib

svg = r'scpi_shell\icon.svg'
ir = ImageResource(svg)

TestAction = Action(
    name="&Test", action="test_clicked", tooltip="Click to test", image=ir, height=-1280
)


class DialogWithToolbar(HasTraits):
    """Test dialog with toolbar and menu."""

    action_successful = Bool(False)

    def test_clicked(self):
        self.action_successful = True

    menus = MenuBar(
        Menu(
            ActionGroup(TestAction),
            name="Test"
        )
    )

    bt1 = Button(image=ir)
    bt2 = SVGButton(filename=svg, height=128, width=128)
    bt3 = ToolbarButton(image=ir)

    toolbar = ToolBar(ActionGroup(TestAction))

    traits_view = View(
        Item(
            label="Click the button on the toolbar or the menu item.\n"
                  "The 'Action successful' element should turn to True."
        ),
        Item("action_successful", style="readonly", label='clicked'),
        Item(oa(bt1), style='custom', height=-128, width=-128),  # `oa` is a wrapper of `varname.nameof`, oa(x.y.z) == str('x.y.z')
        Item(oa(bt2)),
        Item(oa(bt3), style='custom', height=-128, width=-128),
        menubar=menus,
        toolbar=toolbar,
        buttons=[TestAction, "OK", ],
    )

    def _bt2_fired(self):
        self.action_successful = not self.action_successful


if __name__ == "__main__":
    # Execute from command line for manual testing
    vw = DialogWithToolbar()
    vw.configure_traits()

图片

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment