Python 3.5 / Tkinter:在多帧GUI中使用Matplotlib(不断崩溃!)

时间:2016-08-11 17:17:49

标签: python user-interface canvas matplotlib tkinter

我正在努力实现与这个问题非常相似的东西(cannot draw matplotlib chart in tkinter GUI without crashing)。链接中显示的代码与我的代码之间的唯一区别如下:

- 我使用的是canvas.get_tk_widget().pack(...),而不是canvas.get_tk_widget().grid(...)

- 对于我的每个帧,我执行columnconfigurerowconfigure,因为这是我在每个帧中组织GUI元素的方式。

- 我的Spyder,Matplotlib和Tkinter版本都是最新版本(我正在使用Anaconda ontop)。

- 我没有收到任何警告或错误,我在Spyder中的控制台窗口只是停止,我必须重启我的内核。

我已经看过很多关于matplotlib和tkinter的教程。此时我已经准备好放弃了。

编辑#2:当我复制并粘贴此代码(https://pythonprogramming.net/how-to-embed-matplotlib-graph-tkinter-gui/)时,我得到完全相同的错误。这让我觉得这可能与我的装置有关。

编辑:代码/问题代码的骨架版本

主应用类:

  class iSEEApp(tk.Tk):

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

            tk.Tk.wm_title(self, "GUI V0.5")

            container = tk.Frame(self)
            container.winfo_toplevel().geometry("600x600")
            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 (pixelAnnoPage, patchAnnoPage, heatMapPage):
                page_name = F.__name__
                frame = F(parent=container, controller=self)
                self.frames[page_name] = frame

                frame.grid(row=0, column=0, sticky="nsew")

            self.show_frame("pixelAnnoPage")

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

一般框架类别建设/公约:

 class heatMapPage(tk.Frame):

        def __init__(self, parent, controller):
            tk.Frame.__init__(self, parent)
            self.controller = controller 
            self.buildFrame()

        def buildFrame(self):
            print("test")

            #self.pack(fill="both", expand=True)
            self.columnconfigure(1, weight = 1)
            self.columnconfigure(3, pad = 7)
            self.rowconfigure(3, weight = 1)
            self.rowconfigure(5, pad = 7)        

问题代码(在buildFrame()中):

    self.heatFig = Figure(figsize=(5, 4), dpi=100)
    self.heatPlot = self.heatFig.add_subplot(111)
    self.heatPlot.plot([1,2,3,4,5,6,7,8],[5,6,7,8,1,2,2,1])

    self.heatCanvas = FigureCanvasTkAgg(self.heatFig, master=self)
    self.heatCanvas.get_tk_widget().grid(row=1, column=0, columnspan=2, rowspan=4, padx=5, sticky="nsew") 
    self.heatCanvas.show()

1 个答案:

答案 0 :(得分:-1)

好的。事实证明,这个问题与我的代码无关。正如我上面提到的,我使用的是Anaconda Python软件包,这意味着我的主要安装程序不是Pip,而是Conda。每当我做conda upgrade matplotlib时,我都会收到一条消息,声称一切都是最新的。然而,当我最终做python -m pip install --upgrade matplotlib时,不仅结果库不是最新的,它还是一个完全不同的文件!基本上发生的事情是Anaconda和Anaconda Cloud提供的matplotlib库是破碎的!如果您在自己的项目中看到此问题,请使用PIP检查您的图书馆。

相关问题