2

I am trying to have a key-press event from a Pylab plot update a Tkinter listbox. The callback works fine, but when I insert into the listbox I get the error in the title. I also have a key-press callback from the Tkinter window, which updates the listbox with no error, using the same function. ie. the error only occurs when the event comes from the Matplotlib callback.

Here is the relevant (simplified) code.

import matplotlib.pyplot as plt
import Tkinter as Tk

class Tkinter_GUI(Tk.Frame):
    def __init__(self, parent):
        #...
        self.graph = Graph(data, self.on_key_press_event)
        #...
        self.listbox = Tk.Listbox(parent)
        self.listbox.grid()
        #...
        parent.bind("<Key>", self.on_key_press)  # Key press callback from Tkinter

    def on_key_press_event(self, event):
        try:
           char = event.char  # If the event came from Tkinter
        except AttributeError:
           char = event.key  # If the event came from Pyplot
        listbox_items = f(char)
        self.listbox.insert(Tk, *listbox_items) # This line causes the error, but only when called by the Graph callback.

class Graph():
    def __init__(self, data, key_press_callback):
        self.key_press_callback = key_press_callback
        #...
        plt.ion()
        self.fig = plt.figure()
        self.fig.canvas.mpl_connect('key_press_event', self.key_press_callback)
        # plot data ...

if __name__ == '__main__':
    root = Tk.Tk()
    window = Tkinter_GUI(root)
    root.mainloop()

Commenting out the listbox insertion line prevents the error.

I suspect it could be some sort of bad interaction between Tkinter and Matplotlib. My Googling has turned up some related problems (e.g. this) but no solutions.

I am using Enthought Python on OSX Mavericks. I have attached the Apple system report / Python crash dump, in case any useful details might be inside: http://pastebin.com/SqY9GN3C

Community
  • 1
  • 1
rleelr
  • 1,854
  • 17
  • 26

0 Answers0