首页 > 解决方案 > 在 Jupyter 中使用按钮修改变量的简洁方法

问题描述

我正在尝试使用 ipywidget 按钮来修改变量:

df = pd.DataFrame([['a','b'],['c','d']])
my_button = widgets.Button(description='Delete')

out = widgets.Output()
def on_button_clicked(_):
      # "linking function with output"
      with out:
          # what happens when we press the button
        df.to_clipboard()
        df = pd.DataFrame(['t','x'],['y','z'])
        print('hello' + str(len(df)))
        
my_button.on_click(on_button_clicked)                   
       
HBox([my_button])

我的目标是:

  1. 在剪贴板中获取 df 的值(我设法让它在某个时候工作,但在这种情况下不是,我不明白为什么)
  2. 将 df 修改为新值 ('t,x,y,z')
  3. 在按钮下方打印消息(或显示此结果的任何其他方法)

标签: pythonwidgetjupyter

解决方案


推荐阅读