[Matplotlib-users] Key Press and Key Release Events in TkInter

Gregory Key gregk3172 at outlook.com
Thu Feb 2 08:36:03 EST 2017


Here is some code showing my problem. One uses the QT5Agg backend and
one uses the TkAgg backend. The QT5Agg version behaves the way I would
expect. You press a key, it fires the key_press event and then does
nothing until you release the key. In the TkAgg version if you press a
key and hold it there is a continuous stream of key_press, key_release
events.

from __future__ import print_function
import matplotlib
matplotlib.use('q
t5agg')
import matplotlib.pyplot as plt


def on_key_press_event(event):
    print('you pressed %s' % event.key)


def on_key_release_event(event):
    print('you released %s' % event.key)

fig = plt.figure()
fig.canvas.mpl_connect('key_press_event', on_key_press_event)
fig.canvas.mpl_connect('key_release_event', on_key_release_event)

plt.show()


from __future__ import print_function
import matplotlib
matplotlib.use('tkagg')
import matplotlib.pyplot as plt


def on_key_press_event(event):
    print('you pressed %s' % event.key)


def on_key_release_event(event):
    print('you released %s' % event.key)

fig = plt.figure()
fig.canvas.mpl_connect('key_press_event', on_key_press_event)
fig.canvas.mpl_connect('key_release_event', on_key_release_event)

plt.show()

Thanks
Greg Key

On Tue, 2017-01-31 at 21:05 +0000, Gregory Key wrote:
> I am running Python 3.5.2 and MatPlotLib 2.0.0 on Ubuntu 16.10.
> 
> I am developing a program that uses matplotlib embedded in tkinter to
> plot vibration data in a bode plot, phase and amplitude versus rotor
> speed. I adapted the data browser given in the matplotlib
> documentation
>             as example 91.2 data_browser.py to browse the data. The
> browser worked but I wanted to modify it so that holding down the
> next
> or previous key would scroll through the data instead of having to
> continously press and release the keys. My plan was to use the
> key_press_event to start a timer which would call a scroll method and
> then use the key_release event to stop the timer and stop scrolling.
> I
> couldn't make this scheme work so I did some investigating on the
> key_press_event and key_release_event in tkinter. I used example 91.5
> keypress_demo.py given in the matplotlib documentation but I added a
> key_release_event method to it. What I found was that the key events
> don't work the way I would expect them to work. If you press and
> release a key things work as expected. When a key is pressed the
> key_press_event fires and when it is released the key_release_event
> fires. If a key is pressed and held however the key_press_event fires
> followed immediately by a key_release_event even though the key has
> not
> been released. As long as the key is held down it will continue to
> fire
> key_press_event followed by key_release_event. Is this the way it is
> supposed to work?
> 
> Thanks
> Greg Key
> _______________________________________________
> Matplotlib-users mailing list
> Matplotlib-users at python.org
> https://mail.python.org/mailman/listinfo/matplotlib-users


More information about the Matplotlib-users mailing list