python - 如何在 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 窗口现在没有弹出。
解决方案
推荐阅读
- python - 如何在numpy中返回一个数组列?
- reactjs - 滚动上的 Mui-datatables 固定列 - (左或右)
- python - Python 的 Robinhood 问题
- python - numpy 多维数组的交集
- c++ - 按位运算期间整数提升的行为
- kotlin - java.lang.ClassCastException:java.lang.Integer 无法转换为 android.widget.LinearLayout 当我尝试充气时
- typescript - 当值的类型取决于键但并非所有键都预先知道时,如何定义接口?
- python - Python:根据文本长度更新字典中的值的快速方法?
- visual-studio - Blazor WebAssembly Visual Studio 调试器问题
- r - 通过小标题列表运行 t.test