【问题标题】:How does one get widget values with a button in ipython如何使用 ipython 中的按钮获取小部件值
【发布时间】:2014-10-14 04:15:27
【问题描述】:

我有一个函数 createWidgets,其目的是获取一个字符串列表并为每个字符串创建一个容器列表 -> 1 个容器 = 一个文本框和复选框。然后将每个容器放入一个大容器中。

我想要做的是在容器上附加一个按钮,on_click 会获取所有“True”并将所有修改后的字符串放入数据框中

    widgelist = e.options
    txtBox_type = 'text_widget' # Define if Area box o regular txtbox
    bigContainer = createWidgets(widgelist, txtBox_type)

    Function
    def createWidgets(widgelist, txtBox_type):

        #containerList = []
        i = 0

        for k in widgelist:
        ## Build Container widgets

           chBox_Widget = widgets.CheckboxWidget(description = str(i),value = False,)
           if txtBox_type == 'textA_widget': # Check wether txtBox should be an area txt box or not.
               txt_Widget = widgets.TextareaWidget( description = str(i), value = k)
           else:
               txt_Widget = widgets.TextWidget( description = str(i), value = k)

        container = widgets.ContainerWidget()
        container.children = [chBox_Widget, txt_Widget]
        containerList.append(container)

        i+= 1

        button = widgets.ButtonWidget(description = 'Add')
        bigContainer = widgets.ContainerWidget()
        bigContainer.children = containerList

        return  bigContainer

我访问了许多网站,并在此帮助上花费了很多天,非常感谢

【问题讨论】:

    标签: widget ipython ipython-notebook


    【解决方案1】:

    尽我所能解释这个问题,下面的代码应该可以提供答案:

    import IPython.html.widgets as widgets
    from IPython.display import display, clear_output
    import pandas as pd
    
    df = pd.DataFrame(columns=['Thing'])
    
    def createWidgets(widgelist):
    
        ## Each CheckboxWidget and TextWidget are enclosed in a subwidget.  We use a 
        ## list comprehension to construct a list of these subwidgets.
        containerList = [
            widgets.ContainerWidget(children=(widgets.CheckboxWidget(description=k)
                                              widgets.TextWidget(value=k)))
            for k in widgelist]
        bigContainer = widgets.ContainerWidget(children=containerList)
    
        ## To arrange the CheckboxWidget in a row with the TextWidget, we have to
        ## first display them, then remove_class('vbox') and add_class('hbox'). This 
        ## bit of awkwardness in the IPython version 2.x notebook will hopefully 
        ## be fixed in version 3.x.  Displaying bigContainer also displays it's children.
        display(bigContainer)
        for c in containerList:
            c.remove_class('vbox')
            c.add_class('hbox')
    
        return  bigContainer
    
    widgelist = ['ThingA', 'ThingB', 'ThingC', 'ThingD']
    bigContainer = createWidgets(widgelist, txtBox_type)
    
    ## Callback for button.on_click.
    def add_to_dataframe(a):
        # The children of bigContainer are also containers,
        # each with first child a CheckboxWidget and second
        # child a TextWidget.  We iterate through them and
        # if checked, add the text to the dataframe df as
        # an additional row.
        for c in bigContainer.children:
            if c.children[0].value:
                df.loc[len(df)+1] = (c.children[1].value,)
                display(df)
    
        clear_output()
        display(df)
    
    button = widgets.ButtonWidget(description = 'Add')
    button.on_click(add_to_dataframe)
    
    display(button)
    

    这是小部件区域的屏幕剪辑,在向数据框添加几行后输出。


    我本来会设计代码以稍微不同的方式执行此操作,但我试图留下来 靠近您的代码组织。

    【讨论】:

    • 是的,还有更多!添加复选框是一个真正的好处。我宁愿少得多。我想我不明白的部分是Callback for button.on_click.如何继承bigContainer
    • 是的,这可能会造成混淆。在回调函数add_to_dataframe(...) 内部,变量bigContainer 实际上是一个全局变量——你可以看到它在上面定义了三行。但是在函数createWidgets(...)内部还有一个局部变量定义bigContainer。搜索“python 变量范围”将我带到this explanation,但您会发现更多关于此的说明。
    【解决方案2】:

    这是 jupyternotebooks 4 上 Ipython3 的更新版本

    只需重命名:

    • widgets.ContainerWidget ->widgets.Box
    • widgets.CheckboxWidget -> widgets.Checkbox
    • widgets.TextWidget -> widgets.Text

    参考:[https://ipython.org/ipython-doc/3/whatsnew/version3_widget_migration.html]

    【讨论】:

      猜你喜欢
      • 2015-12-14
      • 1970-01-01
      • 2022-01-19
      • 1970-01-01
      • 1970-01-01
      • 2015-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多