Pyplot图形无法在tkinter窗口中绘制
import tkinter as tk
from tkinter import ttk
import matplotlib
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
import matplotlib.animation as animation
from matplotlib import pyplot as plt
class graphPage(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="Graph Page")
label.pack(padx=10, pady=10)
canvas = FigureCanvasTkAgg(f, self)
canvas.draw()
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=True)
self.a = plt.subplot2grid((6,4), (0,0), rowspan = 5, colspan = 4)
self.a.plot([1,2,3,4,5], [10,20,30,40,50], "#00A3E0", label = "high")
toolbar = NavigationToolbar2Tk(canvas, self)
toolbar.update()
canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
f = plt.figure()
class stock(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame()
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(index=0, weight=1)
container.grid_columnconfigure(index=0, weight=1)
frame = graphPage(parent = container)
frame.grid(row=0, column=0, sticky="nsew")
app = stock()
app.mainloop()
tkinter窗口就是空白的,连标签都不显示。当不使用pyplot.figure时,标签是可以显示的。结果发现,jupyter notebook的默认交互模式是关闭的(False)。当改成开启(true)时,会打开一个叫做Figure的新窗口,并在里面绘制图表。不过,tkinter窗口还是空白的。
补充:使用stock = tk.Tk()来定义tkinter窗口可以解决这个问题。但是,这样做不太可行,因为我最终会有多个页面,这些页面会作为不同的框架加载到tkinter类中。
0 个回答
暂无回答