如何修复ipywidgets中的按钮按下?

2024-10-05 11:45:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我在jupyter notebook中有以下代码,其中ipywidgets7.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()

它呈现一个登录掩码,但当我按下按钮时,什么也没有发生!没有打印出来,也没有在笔记本或日志窗口中

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


Tags: textselfconfdefusernameloginbuttonpassword
1条回答
网友
1楼 · 发布于 2024-10-05 11:45:52

解决方案是:您必须使用方法on_click来处理被单击的按钮

  self.button.on_click(self.login) 

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

相关问题 更多 >

    热门问题