1

Following this example I tried to use a lambda function to pass my controller of my Tkinter widget through mpl_connect, located at a different frame (as the controller contains some general variables). Although I did not change the code (except adjusting for the passed through controller in the appropriate instances within the frame), my on_key_event function now behaves quite differently.

The old version, where I call mpl_connect without the lambda function behaves normally. Each key stroke calls the on_key_event instance only once.

self.canvas.mpl_connect('key_press_event', self.on_key_event)

The new version, where I attempt to pass through the controller to on_key_event with the lambda function behaves quite differently. Each key stroke seems to call the on_key_event instance multiple times (seemingly a random number smaller than 10).

self.canvas.mpl_connect('key_press_event', lambda event: self.on_key_event(event, controller))

Is this related to the lambda function behaving slightly different than I am expecting? A wrong syntax? Something differently because of the matplotlib figure embedded within Tkinter?

Community
  • 1
  • 1
  • It is unrelated to using lambda. There should be no difference at all. There must be something else going on that is causing the behavior you see. The best think you can do is to create a [Minimal, Complete and Verifiable example](http://stackoverflow.com/help/mcve) to narrow down the problem. – Bryan Oakley Jan 11 '17 at 23:10
  • Thanks! At least I now know that the lambda function is not to blame. I will try to narrow down the issue by looking within my on_key_event. – B. Buysschaert Jan 11 '17 at 23:14
  • 1
    It seemed to be related to where I exactly call the mpl_connect. I had placed it with the redrawing instance, creating a new mpl_connect every time I had updated the figure. While this did not provide any issues without the lambda function, it did when including the lambda function. The issue now seems to be resolved by calling the mpl_connect somewhere else (outside of the updating instance or the on_key_event). – B. Buysschaert Jan 12 '17 at 09:37

0 Answers0