1 回答

TA貢獻1811條經(jīng)驗 獲得超5個贊
關鍵是pyplot
當你想在內(nèi)部繪制時不要使用,matplotlib.figure.Figure
下面是一個最小示例,它沿著我在您的代碼中看到的小部件繪制了 3 個獨立的圖表Text
:
import pandas as pd
import numpy as np
import tkinter as tk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from matplotlib.figure import Figure
? ??
class Graph(tk.Frame):
? ? def __init__(self, master=None, title="", *args, **kwargs):
? ? ? ? super().__init__(master, *args, **kwargs)
? ? ? ? self.fig = Figure(figsize=(4, 3))
? ? ? ? ax = self.fig.add_subplot(111)
? ? ? ? df = pd.DataFrame({"values": np.random.randint(0, 50, 10)}) #dummy data
? ? ? ? df.plot(ax=ax)
? ? ? ? self.canvas = FigureCanvasTkAgg(self.fig, master=self)
? ? ? ? self.canvas.draw()
? ? ? ? tk.Label(self, text=f"Graph {title}").grid(row=0)
? ? ? ? self.canvas.get_tk_widget().grid(row=1, sticky="nesw")
? ? ? ? toolbar_frame = tk.Frame(self)
? ? ? ? toolbar_frame.grid(row=2, sticky="ew")
? ? ? ? NavigationToolbar2Tk(self.canvas, toolbar_frame)
? ??
root = tk.Tk()
for num, i in enumerate(list("ABC")):
? ? Graph(root, title=i, width=200).grid(row=num//2, column=num%2)
text_box = tk.Text(root, width=50, height=10, wrap=tk.WORD)
text_box.grid(row=1, column=1, sticky="nesw")
text_box.delete(0.0, "end")
text_box.insert(0.0, 'My message will be here.')
root.mainloop()
結(jié)果:
添加回答
舉報