【问题标题】:How to fix button press in ipywidgets?如何修复 ipywidgets 中的按钮按下?
【发布时间】:2020-02-10 08:00:57
【问题描述】:

我在 jupyter 笔记本中有以下代码,ipywidgets 7.5.1 是登录掩码

from ipywidgets import widgets

class Login:
    def __init__(self):
        text21 = widgets.Text(description="username")   
        text22 = widgets.Text(description = "password")
        self.button = widgets.Button(
            description='Login NSG',
            disabled=False,
            button_style='info', 
            tooltip='Login with username and password',            
        )        
        self.vbox_conf = widgets.VBox(children=[text21, text22, self.button])             
        self.button.observe(self.login) 
        # alternative: self.button.observe(self.login, names='value') 
        display(self.vbox_conf)            

    def login(self, args):
        print("test")
        print(args)    

login = Login()

它呈现了一个登录掩码,但是当我按下按钮时没有任何反应!什么都没有打印出来,也没有打印在笔记本上,也没有在日志窗口中!

发生了什么以及如何解决这个问题?

【问题讨论】:

    标签: python jupyter ipywidgets


    【解决方案1】:

    解决办法是:你必须使用方法on_click来处理被点击的按钮

      self.button.on_click(self.login) 
    

    这是一个未记录的功能(或者至少在不完整且具有误导性的documentation 中很难找到)。

    【讨论】:

    • 这个确切功能的文档是小部件事件页面下的第一项。 ipywidgets.readthedocs.io/en/latest/examples/…
    • 出于好奇,您发现与此相关的文档的哪一部分具有误导性?如果有可以改进的地方,很高兴听到它们。
    • 但不适用于“按钮”的文档:ipywidgets.readthedocs.io/en/latest/examples/…
    • 很公平,我想文档将小部件的基本创建和事件分开的原因是并非所有小部件都支持所有事件。不过,我不会考虑 undocumented 的功能。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-31
    • 1970-01-01
    • 2020-03-30
    • 2019-12-25
    • 2019-01-04
    相关资源
    最近更新 更多