Beispiel #1
0
def test_sub_window_controls():
    """Test a sub-control."""
    class Example(Window):

        layout = mark("""
          <window title="Test on window controls">
          </window>
        """)

        def on_press(self):
            self.pressed = 1

        def on_press_a(self):
            self.pressed = 2

    window = start(Example)
    window.pressed = 0

    # Check that the control method is registered into the window object
    assert "press" in window.controls
    assert is_registered(window, "press", window.on_press_a)

    # Check that the on_press_a method is called when firing the control
    window._process_control("press", {"key": "a", "raw_key": "a"})
    assert window.pressed == 2, "The control method wasn't called."

    # Check that the control method is registered into the window object
    assert is_registered(window, "press", window.on_press)

    # Check that the on_press method is called when firing the control
    window._process_control("press", {"key": "b", "raw_key": "b"})
    assert window.pressed == 1, "The control method wasn't called."
Beispiel #2
0
def test_alias_widget_control():
    """Test aliases on a widget control."""
    class Example(Window):

        layout = mark("""
          <window title="Test on window controls">
            <button x=2 y=0 id=quit>Quit or report</button>
          </window>
        """)

        def on_click_quit(self):
            self.pressed = True

        on_press_b_in_quit = on_click_quit

    window = start(Example)
    window.pressed = False
    widget = window["quit"]

    # Check that the control method is registered into the window object
    assert "press" in widget.controls
    assert is_registered(widget, "press", window.on_click_quit)

    # Check that the on_click_quit method is called when firing the control
    widget._process_control("click")
    assert window.pressed, "The control method wasn't called."
    window.pressed = False

    # Check that the control method is registered into the window object
    assert is_registered(widget, "press", window.on_press_b_in_quit)

    # Check that the on_press_b method is called when firing the control
    widget._process_control("press", {"key": "b", "raw_key": "b"})
    assert window.pressed, "The control method wasn't called."
Beispiel #3
0
def test_implicit_widget_control():
    """Test the link between a control method and implicit widget control."""
    class Example(Window):
        """Window showing implicit controls."""

        layout = mark("""
          <window title="An example for implicit controls">
            <menubar>
              <menu name="File">
                <item id="quit">Quit this app right away</item>
              </menu>
            </menubar>
          </window>
        """)

        def on_quit(self):
            """The user pressed on the 'quit' menu item."""
            self.should_quit = True

    window = start(Example)
    window.should_quit = False

    # Check that the control method is registered into the widget object
    widget = window.parsed_layout.get("item").widget
    assert "click" in widget.controls
    assert is_registered(widget, "click", window.on_quit)

    # Check that the on_quit method is called when firing the control
    widget._process_control("click")
    assert window.should_quit, "The control method wasn't called."
Beispiel #4
0
def test_alias_window_control():
    """Test aliases on a window control."""
    class Example(Window):

        layout = mark("""
          <window title="Test on window controls">
          </window>
        """)

        def on_press_a(self):
            self.pressed = True

        on_press_b = on_press_a

    window = start(Example)
    window.pressed = False

    # Check that the control method is registered into the window object
    assert "press" in window.controls
    assert is_registered(window, "press", window.on_press_a)

    # Check that the on_press_a method is called when firing the control
    window._process_control("press", {"key": "a", "raw_key": "a"})
    assert window.pressed, "The control method wasn't called."
    window.pressed = False

    # Check that the control method is registered into the window object
    assert is_registered(window, "press", window.on_press_b)

    # Check that the on_press_b method is called when firing the control
    window._process_control("press", {"key": "b", "raw_key": "b"})
    assert window.pressed, "The control method wasn't called."
Beispiel #5
0
def test_sub_widget_controls():
    """Test a sub-control."""
    class Example(Window):

        layout = mark("""
          <window title="Test on window controls">
            <button x=2 y=3 id=count>Count me</button>
          </window>
        """)

        def on_press_count(self):
            self.pressed = 1

        def on_press_a_in_count(self):
            self.pressed = 2

    window = start(Example)
    window.pressed = 0
    widget = window["count"]

    # Check that the control method is registered into the widget object
    assert "press" in widget.controls
    assert is_registered(widget, "press", window.on_press_a_in_count)

    # Check that the on_press_a_count method is called when firing the control
    widget._process_control("press", {"key": "a", "raw_key": "a"})
    assert window.pressed == 2, "The control method wasn't called."

    # Check that the control method is registered into the widget object
    assert is_registered(widget, "press", window.on_press_count)

    # Check that the on_press_count method is called when firing the control
    widget._process_control("press", {"key": "b", "raw_key": "b"})
    assert window.pressed == 1, "The control method wasn't called."
Beispiel #6
0
def radio():
    from bui import Window, start

    class TestWindow(Window):

        layout = mark(DEFAULT_LAYOUT)

    window = start(TestWindow)
    radio = window["actions"]
    return radio
Beispiel #7
0
def test_widget_control():
    """Test a widget control with its associated method."""
    class Example(Window):

        layout = mark("""
          <window title="Test on widget controls">
            <button x=2 y=2 id="quit">Quit the window</button>
          </window>
        """)

        def on_click_quit(self):
            self.should_quit = True

    window = start(Example)
    window.should_quit = False

    # Check that the control method is registered into the widget object
    widget = window["quit"]
    assert "click" in widget.controls
    assert is_registered(widget, "click", window.on_click_quit)

    # Check that the on_click_quit method is called when firing the control
    widget._process_control("click")
    assert window.should_quit, "The control method wasn't called."
Beispiel #8
0
        <menubar>
          <menu name="File">
            <item>What is it?</item>
            <item>Quit</item>
          </menu>
        </menubar>

        <button x=2 y=2>Click me!</button>
        <text x=3 y=3 id=report>Report</text>
      </window>
    """)

    def on_click_me(self):
        """When the 'Click me!' button is pressed."""
        self["report"].value = "The button was clicked!"

    def on_quit(self):
        """When the user press the quit menu item in the File menu."""
        self.close()

    # Keyboard shortcuts
    def on_press_alt_f4(self):
        """When the user presses Alt + F4."""
        self.close()

    # Linking an alias to a method is so simple
    on_press_ctrl_q = on_press_alt_f4


start(HelloBUI)
Beispiel #9
0
                    self.file_size = length

            # Download the file
            total = 0
            async with aiofiles.open(self.file, "wb") as file:
                while response:
                    if self.widget.downloading:
                        bytes = await response.content.read(1024)
                        if not bytes:
                            self.complete(total)
                            return

                        total += len(bytes)
                        await file.write(bytes)
                        self.progress = total
                        await self.widget.sleep(0.1)
                    else:
                        await self.widget.sleep(0.2)


def human_size(num):
    for unit in ('', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi'):
        if abs(num) < 1024.0:
            return f"{num:3.1f}{unit}B"
        num /= 1024.0

    return f"{num:3.1f}YiB"


start(DownloadExample)
Beispiel #10
0
"""

from bui import Window, start

class CheckboxExample(Window):

    """Class to show several checkboxes on the window."""

    layout = mark("""
      <window title="Blind User Interface - checkboxes">
        <checkbox x=3 y=0 id="gif">GIF</checkbox>
        <checkbox x=1 y=1 id="png">PNG</checkbox>
        <checkbox x=4 y=2 id="jpg">JPEG</checkbox>
      </window>
    """)

    def on_check_gif(self, checked):
        print(f"Check GIF: {checked}")

    def on_checked_jpg(self):
        print("JPEG was checked.")

    def on_unchecked_jpg(self):
        print("JPEG was unchecked.")

    def on_check_jpg(self):
        print("JPEG state was changed.")


start(CheckboxExample)