Django: tkinter/matplotlib - TclError - Null main window
<p>I'm having an error with my Django project. It appears that tkinter is throwing an error due to the main thread executing outside of the main loop. This is happening implicitly in Django or Matplotlib, could anyone advise on what is causing this problem.</p>

<p>This is the error in Django...</p>

<pre><code>Internal Server Error: /stocks/
Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/django/core/handlers/base.py", line 149, in get_response
    response = self.process_exception_by_middleware(e, request)
  File "/usr/local/lib/python3.4/dist-packages/django/core/handlers/base.py", line 147, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/usr/local/lib/python3.4/dist-packages/django/views/generic/base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "/usr/local/lib/python3.4/dist-packages/django/views/generic/base.py", line 88, in dispatch
    return handler(request, *args, **kwargs)
  File "/home/philip/PycharmProjects/stockmarket/cs_coursework/views.py", line 161, in post
    self.graphData(request.POST["ticker"])
  File "/home/philip/PycharmProjects/stockmarket/cs_coursework/views.py", line 130, in graphData
    fig = plt.figure()
  File "/usr/lib/python3/dist-packages/matplotlib/pyplot.py", line 423, in figure
    **kwargs)
  File "/usr/lib/python3/dist-packages/matplotlib/backends/backend_tkagg.py", line 79, in new_figure_manager
    return new_figure_manager_given_figure(num, figure)
  File "/usr/lib/python3/dist-packages/matplotlib/backends/backend_tkagg.py", line 96, in new_figure_manager_given_figure
    icon_img = Tk.PhotoImage(file=icon_fname)
  File "/usr/lib/python3.4/tkinter/__init__.py", line 3419, in __init__
    Image.__init__(self, 'photo', name, cnf, master, **kw)
  File "/usr/lib/python3.4/tkinter/__init__.py", line 3375, in __init__
    self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: NULL main window
[19/Mar/2016 14:58:44] "POST /stocks/ HTTP/1.1" 500 128763
Error in atexit._run_exitfuncs:
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/matplotlib/_pylab_helpers.py", line 86, in destroy_all
    manager.destroy()
  File "/usr/lib/python3/dist-packages/matplotlib/backends/backend_tkagg.py", line 584, in destroy
    self.window.destroy()
  File "/usr/lib/python3.4/tkinter/__init__.py", line 1895, in destroy
    for c in list(self.children.values()): c.destroy()
  File "/usr/lib/python3.4/tkinter/__init__.py", line 2131, in destroy
    self.tk.call('destroy', self._w)
RuntimeError: main thread is not in main loop

Process finished with exit code 139
</code></pre>

<p>Please message if you need any of my code I'm not sure what I should post it terms of code.</p>

<p>As well as exit code 139 sometimes I get exit code 134.</p>

<p>Help would be greatly appreciated. </p>

<p>Thanks.</p>