带参数的 Python Matplotlib 回调函数

标签 python button matplotlib callback

在按钮按下的回调函数中,除了'event'之外还有没有传递更多参数?例如,在回调函数中,我想知道按钮的文本(在本例中为“Next”)。我该怎么做?

import matplotlib.pyplot as plt
from matplotlib.widgets import Button

fig = plt.figure()
def next(event):
    # I want to print the text label of the button here, which is 'Next'
    pass


axnext = plt.axes([0.81, 0.05, 0.1, 0.075])
bnext = Button(axnext, 'Next')
bnext.on_clicked(next)
plt.show()

最佳答案

另一个可能更快的解决方案是使用 lambda 函数:

import matplotlib.pyplot as plt
from matplotlib.widgets import Button

fig = plt.figure()
def next(event, text):
    print(text)
    pass


axnext = plt.axes([0.81, 0.05, 0.1, 0.075])
bnext = Button(axnext, 'Next')
bnext.on_clicked(lambda x: next(x, bnext.label.get_text()))
plt.show()

关于带参数的 Python Matplotlib 回调函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28758592/

相关文章:

python - 如何告诉 setuptools 从 src/mypackage 获取我的包

python - 使用 Python pysftp 使用 key 文件连接到 SFTP

jquery - 重置按钮状态

c# - 根据屏幕分辨率将 Windows 窗体上的按钮控件调整为固定大小

python - 如何让地 block 变大?

python - 绘制表示边权重的连续宽度图例

python - 我应该在 pip 的需求文件中输入什么来选择正确的 git 分支?

python - 许多数据帧上的高效 Python Pandas Stock Beta 计算

html - 汉堡包按钮过渡到 X 按钮,但刷新时不会作为汉堡包按钮返回

python - 如何将 matplotlib 编辑选项卡添加到 Canvas 中显示的 seaborn 图中?