Pyscript颜色输入 – 触发太早

huangapple go评论136阅读模式
英文:

Pyscript color input - triggers to soon

问题

我正在使用这个HTML来让用户选择颜色:

<input type="color" id="main-color" name="head" value="#15347d">

然后我使用一个按钮来调用我的Python函数:

<button py-click="generate_color_schemes(False)" id="button-submit">Update!</button>

运行良好。(generate_color_schemes()检查Element("main-color").value以获取十六进制颜色。)

但我想将这两者结合起来,这样单击输入框会打开一个选择器,并在用户在颜色选择器内单击时触发Python函数,以及当他们离开时也会触发。

但是(如预期的那样),添加py-click会在首次单击输入框时触发我的函数,而不是在颜色选择器弹出关闭或用户在颜色选择器内单击时触发。

我认为我希望py-clickoninput事件以及onchange事件上触发。有办法将input type='color'py-click结合以实现这种行为吗?

英文:

I'm using this html to have the user choose a color:

&lt;input type=&quot;color&quot; id=&quot;main-color&quot; name=&quot;head&quot; value=&quot;#15347d&quot;&quot;&gt;

Then I'm using a button to call my python function

button py-click=&quot;generate_color_schemes(False)&quot; id=&quot;button-submit&quot;&gt;Update!&lt;/button&gt;

Works well. (generate_color_schemes() examines the Element(&quot;main-color&quot;).value to get the hex color.)

But I'd like to combine the two, so clicking on the input - opens a picker and fires the python function as the user clicks inside the color picker as well as when they leave.

But (as expected) adding py-click fires my function when the input is first clicked and not when the color chooser popup closes or as the user clicks inside the color picker.

I think I want pyclick to be triggering on the oninput event as well as the onchange event.

Is there a way to combine the input type=&#39;color&#39; with py-click to get this behaviour ?

答案1

得分: 1

正如你所说,你有正确的语法,但错误的事件。与其监听click事件,你可以监听input事件(每当输入值更改时都会触发)或change事件(在此情况下,当颜色选择器关闭时触发)。要在PyScript中执行此操作(或监听任何事件),HTML属性是py-[event],其中[event]是事件的类型。在这种情况下,你将使用py-inputpy-change属性。

以下是在当前版本的PyScript(2023.03.1)中的一个可用示例,我提到这一点是因为未来可能会更改事件API。MDN文档中有一个更长的示例,是用JavaScript编写的。

<!-- 适用于PyScript 2023.03.1 -->
<input type="color" id="main-color" py-input="do_something_with_color()" py-change="do_something_else()">

<py-script>
    import js
    def do_something_with_color():
        elem = js.document.getElementById("main-color")
        value = elem.value
        print(f'选择的颜色: {value}') # 在这里执行一些操作

    def do_something_else():
        elem = js.document.getElementById("main-color")
        value = elem.value
        print(f'最终选择的颜色是: {value}') # 在这里执行一些操作
</py-script>
英文:

As you say, you've got the right syntax but the wrong event(s). Rather than listening for the click event, you can listen to the input event (which is dispatched every time the value of an input changes) or the change event (which is dispatched, in this case, when the color picker closes). To do this in PyScript (or to listen to any event), the HTML attribute is py-[event] where [event] is the type of the event. In this case, you'll use the py-input and py-change attributes.

Here's a working example in the current release of PyScript (2023.03.1), which I mention in case the events API changes in the future. The MDN docs have a longer example in JavaScript.

&lt;!-- Written for PyScript 2023.03.1 --&gt;
&lt;input type=&quot;color&quot; id=&quot;main-color&quot; py-input=&quot;do_something_with_color()&quot; py-change=&quot;do_something_else()&quot;&gt;

&lt;py-script&gt;
    import js
    def do_something_with_color():
        elem = js.document.getElementById(&quot;main-color&quot;)
        value = elem.value
        print(f&#39;Chosen color: {value}&#39;) # do something here

    def do_something_else():
        elem = js.document.getElementById(&quot;main-color&quot;)
        value = elem.value
        print(f&#39;The final color chosen was: {value}&#39;) # do something here
&lt;/py-script&gt;

huangapple
  • 本文由 发表于 2023年6月1日 20:21:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76381802.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定