weixin_44864723
2021-08-11 14:32
采纳率: 50%
浏览 63
已结题

python matplotlib图型界面 键鼠交互

fig.canvas.mpl_connect()怎么同时开启使用键盘和鼠标

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
text = ax.text(0.5, 0.5, 'event', ha='center', va='center', fontdict={'size': 20})

def call_back(event):
    print(event.button)
    print(event.key)
fig.canvas.mpl_connect('key_press_event', call_back)
fig.canvas.mpl_connect('button_press_event',call_back)
plt.show()

程序运行后会有以下报错:

print(event.button)
AttributeError: 'KeyEvent' object has no attribute 'button'
  • 好问题 提建议
  • 收藏

1条回答 默认 最新

  • weixin_44864723 2021-08-11 16:42
    已采纳

    matplotlib键鼠交互 按下Ctrl键,鼠标起作用,弹起Ctrl鼠标失效

    import matplotlib.pyplot as plt
    
    fig, ax = plt.subplots()
    text = ax.text(0.5, 0.5, 'event', ha='center', va='center', fontdict={'size': 20})
    
    key_b=''
    def button_(event):
        global key_b
        if key_b=='press':
            print(event.button)
            # print(key_b)
    key_b=''
    def key_ctrl_press(event):
        global key_b
        if event.key=='control':
            key_b='press'
    def key_ctrl_Bounce(event):
        global key_b
        if event.key=='control':
            key_b='Bounce'
    fig.canvas.mpl_connect('key_press_event', key_ctrl_press)
    fig.canvas.mpl_connect('key_release_event', key_ctrl_Bounce)
    fig.canvas.mpl_connect('button_press_event',button_)
    
    plt.show()
    
    
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题