1

假设我有一个带有在 ipyvuetify 中编码的按钮的代码

v_btn_load  = vue.Btn(class_='mx-2 light-red darken-1', 
                           children=[vue.Icon(left=True, children=['get_app']),'Load data'])

def on_click_load(widget, event, data):
    #pseudo code: load file
    print("button run")
                
v_btn_load.on_event('click', on_click_load)

如何以编程方式运行(单击) v_btn_load 按钮?

v_btn_load.click() does not work

谢谢

4

2 回答 2

4

“on_click_load”仍然是一个本地 python 函数,因此您可以在脚本中简单地访问它。只需用假人(可能是小部件和事件)填写您不需要的变量,并根据您的需要填写数据变量。

如果您需要来自客户端的一些输入,那就更难了。我不知道如何远程控制客户端。到目前为止,我唯一要做的就是使用私有类扩展 VuetifyTemplate 并指定一些 JS 代码在“挂载”时运行。这将运行显示的代码,但与触发点击行为不同:

这是一个简单的示例,它直接将变量的内容复制到本地剪贴板,而不使用任何显示元素:

import ipyvuetify as v
from traitlets import Unicode, observe


class toClipboard(v.VuetifyTemplate):
    """Copies a given string directly to the users clipboard.

       Parameters:
           clipboardValue - The value to offer for copying to the clipboard

       Example:
           tunnel = toClipboard(clipboardValue='VALUE_TO_COPY')       

           Upon change of the variable 'clipboardValue', it's content will be automatically pushed to the clipboard
       """

    clipboardValue = Unicode('').tag(sync=True)

    template = Unicode('''
        <script>
          export default {
            mounted () {
              var tmpElement = $('<textarea>').val(this.clipboardValue).appendTo('body').select();
              document.execCommand('copy');
              $(tmpElement).remove();
            },
          }
        </script>''').tag(sync=True)

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        
        self.observe(self.pushToClipboard, names=('clipboardValue'))
        
        display(self)


    def pushToClipboard(self, change):
        display(self)

作为一个额外的好处,这个例子使用了 traitlets 的观察函数来在变量的值改变时重新显示 JS。这是一种廉价的解决方法,可以创建一些类似的行为。

我不是在真正的 GUI 中使用上面的示例,而是在 Jupyther Notebook 中作为一种懒惰的方式来自动将计算结果复制到我的本地剪贴板。

于 2020-11-30T10:53:25.383 回答
3

查看课程的描述v.Btn我发现了这个:

 |  ----------------------------------------------------------------------
 |  Methods inherited from ipyvue.VueWidget.Events:
 |  
 |  fire_event(self, event, data)
 |  
 |  on_event(self, event_and_modifiers, callback, remove=False)

然后我假设

v_btn_load.fire_event('click', None)

应该做的伎俩

于 2020-12-08T16:20:47.693 回答