浏览代码

#603 open color picker popup with current color

Falko Schindler 2 年之前
父节点
当前提交
e933862839
共有 2 个文件被更改,包括 15 次插入2 次删除
  1. 7 1
      nicegui/elements/color_input.py
  2. 8 1
      nicegui/elements/color_picker.py

+ 7 - 1
nicegui/elements/color_input.py

@@ -26,5 +26,11 @@ class ColorInput(ValueElement):
 
         with self.add_slot('append'):
             self.picker = ColorPicker(on_pick=lambda e: self.set_value(e.color))
-            self.button = ui.button(on_click=self.picker.open) \
+            self.button = ui.button(on_click=self.open_picker) \
                 .props('icon=colorize flat round', remove='color').classes('cursor-pointer')
+
+    def open_picker(self) -> None:
+        """Open the color picker"""
+        if self.value:
+            self.picker.set_color(self.value)
+        self.picker.open()

+ 8 - 1
nicegui/elements/color_picker.py

@@ -18,4 +18,11 @@ class ColorPicker(Menu):
         with self:
             def handle_change(msg: Dict):
                 handle_event(on_pick, ColorPickEventArguments(sender=self, client=self.client, color=msg['args']))
-            Element('q-color').on('change', handle_change)
+            self.q_color = Element('q-color').on('change', handle_change)
+
+    def set_color(self, color: str) -> None:
+        """Set the color of the picker
+
+        :param color: the color to set
+        """
+        self.q_color.props(f'model-value="{color}"')