cannot draw matplotlib chart in tkinter GUI withou

2019-09-12 06:56发布

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.

2条回答
我欲成王,谁敢阻挡
2楼-- · 2019-09-12 07:03

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

查看更多
可以哭但决不认输i
3楼-- · 2019-09-12 07:22

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.

查看更多
登录 后发表回答