有没有办法在Tkinter框架中嵌入由任意绘图函数生成的图形?下面提到的函数在一个单独的窗口中绘制东西,我想让它在同一个窗口中绘图,也许,只是嵌套在主窗口中。如果我没有弄错的话,似乎有人可能想要使用tkinter.Canvas
小部件,在我的情况下,图表会出现在单独的窗口中。
import tkinter as tk
def plot():
#stuff for plotting
#in this case it returns an instance of matplotlib.figure.Figure
class Main(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self, master)
self.pack()
self.widgets()
def widgets(self):
self.menubar = tk.Menu()
self.plotmenu = tk.Menu(self.menubar)
self.plotmenu.add_command(label = "Plot", command = lambda: plot())
self.master.config(menu = self.menubar)
self.menubar.add_cascade(label = "Plot", menu = self.plotmenu)
self.canvas = tk.Canvas(self)
self.canvas.pack()
self.canvas.create_window(0, 0, window = plot())
root = tk.Tk()
app = Main(master=root)
app.mainloop()