0

我有几个滑块、一个带有两个设置为 False 的选项的复选框和一个重置按钮,如下所示:

#Slider
slider_M = plt.axes(rectM, facecolor=axcolor)   
svalueM = Slider(slider_M, 'M', s_Mmin, s_Mmax, valinit=s_Minit, valfmt="%1.2f")
svalueM.on_changed(updateChart) 

#CheckBox
ShkBox = plt.axes(rectC,facecolor=axcolor)
CheckPar = CheckButtons(ShkBox, ('Strong', 'Weak'), (False, False)) 
CheckPar.on_clicked(updateChart)

#Reset Button
resetax = plt.axes(rectR, facecolor=axcolor)
button = Button(resetax, 'Reset', color=axcolor, hovercolor='0.975')
button.on_clicked(resetplt)

在我的重置例程中,我使用以下内容来重置我的滑块和 CheckButtons:

def resetplt(event)
    svalueM.reset()   #Reset Slider
    CheckPar.set_active(0)  # Reset Checkbox

问题出在上面,CheckPar.set_active(0) 切换了 FIRST 复选框的值。我想要的是将两个复选框重置为“False”的原始值。

这在 Tkinter 和 Java 中似乎可行,但我无法用 matplotlib 实现。matplotlib 文档说要使用

set_active(self, index)

通过索引直接(取消)激活检查按钮,其中索引是原始标签列表的索引。

不幸的是,我不知道如何实现这一点,也找不到任何人做过的例子。我尝试过的事情包括:

CheckPar.set_active(0)
CheckPar.set_active(("Strong","Weak"),(0, 0))
CheckPar.set_active([0],[1])

最后两个选项导致类型错误

TypeError: set_active() takes 2 positional arguments but 3 were given

我能够使用来确定盒子的状态

status = CheckPar.get_status()

但我无法将它们重置为原始值。

任何帮助,将不胜感激。

谢谢

4

1 回答 1

0

您必须知道盒子在其原始状态下的状态,然后遍历这些盒子,并仅在当前状态与原始状态不匹配时才切换它们的状态。

看这个测试:

def resetplt(event):
    for i, (state, orig) in enumerate(zip(CheckPar.get_status(), origState)):
        if not state == orig:
            CheckPar.set_active(i)  # switch state


fig, (ax1, ax2) = plt.subplots(1, 2)

# CheckBox
origState = (False, True)
ShkBox = plt.axes(ax1)
CheckPar = CheckButtons(ShkBox, ('Strong', 'Weak'), origState)

# Reset Button
resetax = plt.axes(ax2)
button = Button(resetax, 'Reset', color=axcolor, hovercolor='0.975')
button.on_clicked(resetplt)

plt.show()
于 2019-12-11T14:03:00.967 回答