python - 按下时更改 matplotlib 按钮颜色

标签 python user-interface animation button matplotlib

我正在使用 matplotlib 的 FuncAnimation 运行动画来显示来自微处理器的数据(实时)。我正在使用按钮向处理器发送命令,并希望按钮的颜色在单击后发生变化,但我在 matplotlib.widgets.button 文档中找不到任何内容(目前)实现了这一点。

class Command:

    def motor(self, event):
    SERIAL['Serial'].write(' ')
    plt.draw()

write = Command()
bmotor = Button(axmotor, 'Motor', color = '0.85', hovercolor = 'g')
bmotor.on_clicked(write.motor)            #Change Button Color Here

最佳答案

只需设置button.color

例如

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


fig, ax = plt.subplots()
button = Button(ax, 'Click me!')

colors = itertools.cycle(['red', 'green', 'blue'])

def change_color(event):
    button.color = next(colors)
    # If you want the button's color to change as soon as it's clicked, you'll
    # need to set the hovercolor, as well, as the mouse is still over it
    button.hovercolor = button.color
    fig.canvas.draw()

button.on_clicked(change_color)

plt.show()

关于python - 按下时更改 matplotlib 按钮颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17707169/

相关文章:

python - 需要 Python 中 _xmlplus 的源代码

java - Eclipse RCP 用户友好的 GUI 组件

java - Android 应用程序一开始就崩溃

c# - 使用摇动效果左右动画 WPF 窗口?

jquery - 如何为动态创建的不同组件重用相同的代码?

javascript - 如何保持动画 CSS 的最终状态并删除动画属性?

python - Pygame Mixer.music无法读取mp3流

python - 仅沿一个轴平滑二维阵列

python - StatsModels:返回没有截距的线性回归的预测区间

android - 在多行中并排显示博客样式标签 (Android)