瀏覽代碼

make active flag accessible via keyboard element

Falko Schindler 3 年之前
父節點
當前提交
b46c3bd537
共有 3 個文件被更改,包括 5 次插入2 次删除
  1. 1 1
      main.py
  2. 0 1
      nicegui/elements/element.py
  3. 4 0
      nicegui/elements/keyboard.py

+ 1 - 1
main.py

@@ -410,6 +410,6 @@ with example(ui.keyboard):
 
 
     keyboard = ui.keyboard(handle_keys)
     keyboard = ui.keyboard(handle_keys)
     ui.label('Key events can be caught globally by using the keyboard element.')
     ui.label('Key events can be caught globally by using the keyboard element.')
-    ui.checkbox('Track key events').bind_value_to(keyboard.view, 'active')
+    ui.checkbox('Track key events').bind_value_to(keyboard, 'active')
 
 
 ui.run(port=8080)
 ui.run(port=8080)

+ 0 - 1
nicegui/elements/element.py

@@ -1,5 +1,4 @@
 import justpy as jp
 import justpy as jp
-from enum import Enum
 from ..binding import bind_from, bind_to, BindableProperty
 from ..binding import bind_from, bind_to, BindableProperty
 from ..globals import view_stack, page_stack
 from ..globals import view_stack, page_stack
 
 

+ 4 - 0
nicegui/elements/keyboard.py

@@ -1,5 +1,7 @@
 from pydantic import BaseModel
 from pydantic import BaseModel
 from typing import Callable, Optional
 from typing import Callable, Optional
+
+from ..binding import bind_to
 from ..utils import handle_exceptions, provide_arguments
 from ..utils import handle_exceptions, provide_arguments
 from .custom_view import CustomView
 from .custom_view import CustomView
 from .element import Element
 from .element import Element
@@ -52,6 +54,8 @@ class Keyboard(Element):
         :param active: boolean flag indicating whether the callback should be executed or not
         :param active: boolean flag indicating whether the callback should be executed or not
         """
         """
         super().__init__(KeyboardView(handle_keys=handle_keys, active=active))
         super().__init__(KeyboardView(handle_keys=handle_keys, active=active))
+        self.active = active
+        bind_to(self, 'active', self.view, 'active', lambda x: x)
 
 
 class KeyboardAction(BaseModel):
 class KeyboardAction(BaseModel):
     keypress: bool
     keypress: bool