0

I'm currently following this link with a tutorial: Here is the one I am following. The dude gives you the code. Have included it at the bottom anyway.

Would definitely recommend. But now facing issues:

  • Using Spyder 2.3.8
  • Python 3.5
  • Everything up to date
  • Have set the backend for Spyder to read matplotlib as 'TkAgg' (also took ages!).

Have hashtagged out the three lines which are causing the issue. Works fine when these lines aren't active. Activating them and running causes my terminal to crash and get the message:

It seems the kernel died unexpectedly. Use 'Restart kernel' to continue using this console.

I have really been looking everywhere for a solution. The objective is to get a chart into a Tkinter GUI without it crashing. Please help!?

Here is the code, nearly identical to the one provided in the link:

import matplotlib
#matplotlib.use("TkAgg")

from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg,NavigationToolbar2TkAgg
from matplotlib.figure import Figure

import tkinter as tk
import pandas as pd
from tkinter import ttk


LARGE_FONT=("Consolas",12)

class SeaofBTCapp(tk.Tk):

    def __init__(self,*args,**kwargs):
        #
        #tk.Tk.wm_title(self,"")

    print("")
    tk.Tk.__init__(self,*args,**kwargs)
    tk.Tk.wm_title(self,"Hold my Hand")
    tk.Tk.iconbitmap(self,default="1.ico")
    container=tk.Frame(self)
    container.pack(side="top", fill="both",expand=True)
    container.grid_rowconfigure(0,weight=1)
    container.grid_columnconfigure(0,weight=1)
    self.frames={}
    for F in (StartPage,PageThree):
        frame=F(container, self)

        self.frames[F]=frame
        frame.grid(row=0,column=0,sticky="nsew") #north south east west

    self.show_frame(StartPage)

def show_frame(self, cont):
    frame = self.frames[cont]
    frame.tkraise()

def qf(stringtoprint):
    print(stringtoprint)

class StartPage(tk.Frame):
    def __init__(self,parent,controller):
        ttk.Frame.__init__(self,parent)
        label=ttk.Label(self,text="Testing",font=LARGE_FONT)
        label.pack(pady=10,padx=10)

        button3=ttk.Button(self, text="Graph page",
                          command=lambda:controller.show_frame(PageThree))
                          #lambda:controller.show_frame(PageOne))
        button3.pack()

class PageThree(tk.Frame):
    def __init__(self, parent, controller):    
       # app=tk.Tk()
        tk.Frame.__init__(self,parent)
        label=tk.Label(self,text="Graph Page",font=LARGE_FONT)
        label.pack(pady=10,padx=10)

        label1=ttk.Button(self, text="Start Page",
                   command=lambda:controller.show_frame(StartPage))
        label1.pack()

        label1=ttk.Button(self, text="Back to Home",
                   command=lambda:controller.show_frame(StartPage))
        label1.pack()

    #    f=Figure(figsize=(5,5))
    #    a=f.add_subplot(111)
    #    a.plot([1,2,3,4,5,6,7,8],[5,6,7,8,1,2,2,1])
     #   canvas=FigureCanvasTkAgg(f,self)
     #   canvas.show()
     #   canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH,expand=True)

app=SeaofBTCapp()
app.mainloop()

It's the above three-six lines that cause Spyder to crash.

7
  • There is way too much code here. Can you reduce this to a 10-15 line example? Commented Apr 29, 2016 at 2:06
  • @Panik Can you check the matplotlib examples e.g. matplotlib.org/examples/user_interfaces/embedding_in_tk.html to ensure they work for you? Commented Apr 29, 2016 at 10:27
  • @tcaswell will update the code and shrink it down, can't make it as a small 15line example but will make as small as i can! Commented Apr 29, 2016 at 11:24
  • @mfitzp thanks very much also, been there a few times.. however, if i copy the code you linked, line for line... i get EXACTLY the same error.. no GUI just kernal dying! seems the error lies when trying to 'show' the chart via tkinter canvas.. driving me crazy Commented Apr 29, 2016 at 11:25
  • @Panik that's why I asked. I could reproduce the error with your code, but when i tried the demo code it also crashed suggesting there is something wrong with our installations. Commented Apr 29, 2016 at 11:32

2 Answers 2

1

Uninstalling the conda version and installing it via pip fixes it.

There is an issue about that: https://github.com/ContinuumIO/anaconda-issues/issues/979

Sign up to request clarification or add additional context in comments.

Comments

0

I was struggling with the same problem for a couple of days (Python 3.5.2/64-bit, matplotlib 1.5.1, Win 10 Professional). Reinstallation of matplotlib, tkinter and Python didn't help. Tkinter + matplotlib in Anaconda didn't work as well. Since I am not savvy enough to build matplotlib from the source, I just installed Ubuntu + Anaconda and it helped to solve the issue.

There is something wrong with the code you posted (i.e. indention etc.), I didn't try to fix it. But the following code is working on Ubuntu:

   import matplotlib
   from matplotlib.figure import Figure
   from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
   import tkinter as tk

   fig = Figure()
   plt = fig.add_subplot(111)
   plt.plot([1, 2], [1, 2])

   root = tk.Tk()
   canvas = FigureCanvasTkAgg(fig, master=root)
   canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=1)
   root.update()
   root.mainloop()

I hope, it helped.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.