首页 > 解决方案 > 如何在 tkinter 的顶级小部件中导入我的图表

问题描述

我正在尝试使用 topLevel 小部件将我的 matplotlib 图导入我的 tkinter 窗口,但我收到“pyimage10 不存在错误”。我希望我的情节在 topLevel 小部件中,但目前有另一个创建情节的整个窗口。

我已经压缩了 graphs.py 代码,但可以确认它确实成功地创建了一个图

#graphs.py
def plot1(self):
    key = ['Sales Volume','Sales Revenue']
    index = np.arange(len(self.drinks))
    f = plt.figure(figsize=(7,5))
    plt.subplots_adjust(left=0.3,bottom=0.3)
    plt.bar(index+0,self.salesVolume1,color= 
    ('m','m'),align='center',width=.4)
    plt.bar(index+.40,self.salesRevenue1,color= 
    ('r','r'),align='center',width=.4)
    plt.legend(key, fontsize=12)
    plt.xlabel('Drinks', fontsize=10)
    plt.ylabel('Sales Volume / Revenue', fontsize=10)
    plt.xticks(index, self.drinks, fontsize=10, rotation=30)
    plt.title('Aggregate Sales Volume and Revenue')
    f.show()


#file.py
import graphs
import matplotlib
import tkinter as tk

matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, 
NavigationToolbar2Tk
from matplotlib.figure import Figure


class PageThree(tk.Frame,graphs.Analytics2):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)


        canvas = FigureCanvasTkAgg(graphs.Analytics2, self)
        canvas.draw()
        canvas.get_tk_widget().pack()

运行时,它会在一个单独的窗口中打开我的绘图,这不是我想要做的——我试图将它嵌入到另一个窗口的顶层中,而我的初始 tk 窗口现在没有弹出。

标签: pythonmatplotlibtkintertoplevel

解决方案


推荐阅读