gpt4 book ai didi

带参数的 Python Matplotlib 回调函数

转载 作者:太空狗 更新时间:2023-10-30 02:10:37 24 4
gpt4 key购买 nike

在按钮按下的回调函数中,除了'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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com