【问题标题】:Make matplotlib button widget actuate when pressed, not when released使 matplotlib 按钮小部件在按下时启动,而不是在释放时启动
【发布时间】:2020-05-27 08:54:38
【问题描述】:

我制作了一个简单的交互式 matplotlib 图,它使用按钮来浏览绘图。一个这样的按钮的最小示例是:

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

def message(_):
    print('Hello, World!')

buttonAxes = plt.axes([0.48, 0.48, 0.08, 0.08])
button = Button(buttonAxes, 'Button', color='#FFFFFF', hovercolor='#808080')
button.on_clicked(message)
plt.show()

此按钮的行为与预期相同,但在释放鼠标按钮时激活,而不是在按下鼠标按钮时激活。如果我单击按钮并且不释放鼠标按钮,则永远不会调用 message 并且不会打印任何内容以输出。这使得按钮在我这样的情况下使用起来非常不和谐。

如何使这些按钮在按下时启动?或者,我如何构建一个替换按钮?

Windows 10 上的 Python 3.8.1。

【问题讨论】:

    标签: python python-3.x matplotlib matplotlib-widget


    【解决方案1】:

    你必须通过connect_event()方法使用“button_press_event”事件:

    button.connect_event("button_press_event", message)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-16
      • 1970-01-01
      相关资源
      最近更新 更多