【发布时间】:2016-12-18 13:18:47
【问题描述】:
我正在尝试实现与这个问题(cannot draw matplotlib chart in tkinter GUI without crashing)非常相似的东西。链接中显示的代码与我的代码之间的唯一区别如下:
-我没有使用canvas.get_tk_widget().pack(...),而是使用canvas.get_tk_widget().grid(...)。
-对于我的每个单独的框架,我会使用columnconfigure 和rowconfigure,因为这就是我在每个框架中组织 GUI 元素的方式。
-我的 Spyder、Matplotlib 和 Tkinter 版本都是最新版本(我使用的是 Anaconda)。
-我没有收到任何警告或错误,我在 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()
【问题讨论】:
标签: python user-interface canvas matplotlib tkinter