如何清除其他窗口小部件及其外部窗口小部件

时间:2017-08-04 07:49:15

标签: python python-3.x ipython-notebook ipywidgets

我使用checkbox加载要ipywidgets的文件列表:

from ipywidgets import Checkbox, interact, Layout, Button
import ipywidgets as widgets
import glob
from traitlets import traitlets
from IPython.display import display, clear_output

class LoadedButton(widgets.Button):
    def __init__(self, value=None, *args, **kwargs):
        super(LoadedButton, self).__init__(*args, **kwargs)
        # Create the value attribute.
        self.add_traits(value=traitlets.Any(value))

def file_list(**all_kwargs):
    cols = [k for k, v in all_kwargs.items() if v==True]
    return cols

def reload_files(rr):
    for c in all_files:
        c.close()
        clear_output()
    print('Unselect the csv files above *to EXCLUDE*')
    rr.value =interact(file_list, **all_kwargs)
    return rr

extension = 'csv'                # extention of file you want to read, csv, dat, etc.
all_file_list = [i for i in glob.glob('*.{}'.format(extension))]
all_files = [Checkbox(description=a, value=True) for a in all_file_list ]
all_kwargs = {c.description: c.value for c in all_files} 

lb = LoadedButton(description="Load/reload file list", value=None)
lb.on_click(reload_files)
display(lb)

我希望每次单击使用Button小部件创建的按钮时覆盖以前的输出(小部件和小部件输出),但不是覆盖它而是创建另一个输出实例。我尝试了clear_outputwidget.close()选项,但没有任何帮助。我知道clear_output不会清除小部件,但我希望使用close()可以实现这一点。有没有人知道如何清除/覆盖ipywidgets以及按钮重新加载时的ipywidget输出? another instance of output

1 个答案:

答案 0 :(得分:1)

如果我正确理解了这个问题,我认为这可能适合你;

from ipywidgets import Checkbox, interact, Layout, Button
import ipywidgets as widgets
import glob
from traitlets import traitlets
from IPython.display import display, clear_output

class LoadedButton(widgets.Button):
    def __init__(self, value=None, *args, **kwargs):
        super(LoadedButton, self).__init__(*args, **kwargs)
        # Create the value attribute.
        self.add_traits(value=traitlets.Any(value))

def file_list(**all_kwargs):
    cols = [k for k, v in all_kwargs.items() if v==True]
    return cols

def reload_files(rr):
    if rr.value is not None:
        # print("Reloading...")
        # Reset the panel to just the button.
        panel.children = [panel.children[0]]
        rr.value = None

    if rr.value is None:
        extension = 'csv' # extention of file you want to read, csv, dat, etc.
        all_file_list = [i for i in glob.glob('*.{}'.format(extension))]
        all_files = [Checkbox(description=a, value=True) for a in all_file_list ]
        all_kwargs = {c.description: c.value for c in all_files} 

        rr.value=widgets.interactive(file_list, **all_kwargs)
        panel.children += (rr.value,)
        # display(rr.value)


lb = LoadedButton(description="Load/reload file list", value=None)
lb.on_click(reload_files)

# A VBox for the Button and checkboxes.
panel = widgets.VBox([lb])
panel

我认为问题来自于依靠display函数来表示窗口小部件。当你有一个小部件修改了另一个小部件的可见性时,你可能做的最好的事情是在VBoxHBox等布局小部件中添加和删除它们。无论哪种方式让我知道它是如何工作的。

更新

单击按钮时,

glob和所有其他文件名操作也需要更新。所以它现在就这样做了。

此处还有访问interactive小部件';

上的复选框的方法

panel.children[1].children

OR

lb.value.children

两个陈述都指向同一个对象。