我正在嘗試實現與此問題非常相似的內容(cannot draw matplotlib chart in tkinter GUI without crashing)。鏈接中顯示的代碼與我的代碼之間的唯一區別如下:Python 3.5/Tkinter:在多幀GUI中使用Matplotlib(不斷崩潰!)
- 我正在使用canvas.get_tk_widget().grid(...)
而不是使用canvas.get_tk_widget().pack(...)
。
- 對於我的每個幀,我做了一個columnconfigure
和rowconfigure
,因爲這就是我在每個幀中組織我的GUI元素的方式。
-Spyder,Matplotlib和Tkinter的版本都是最新版本(我使用的是Anaconda ontop)。
- 我不接受任何警告或錯誤,我在Spyder中的控制檯窗口停下來,我必須重新啓動我的內核。
我已經看過關於matplotlib和tkinter的所有教程。在這一點上,我準備放棄。
編輯#2:當我複製並粘貼此代碼(https://pythonprogramming.net/how-to-embed-matplotlib-graph-tkinter-gui/)時,我得到完全相同的錯誤。這導致我認爲,這可能與我的安裝有關。
編輯:代碼/故障代碼,
主App類的骨架版本:
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()