栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

matplotlib弹出窗口中发生错误(AttributeError:“ NoneType”对象没有属性“ set_canvas”)

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

matplotlib弹出窗口中发生错误(AttributeError:“ NoneType”对象没有属性“ set_canvas”)

我做了一个最小的工作示例,说明了如何做到这一点。

它需要对您的代码进行更改,但我不知道您在代码中所拥有的内容,并且您也没有创建最少的工作示例。

它使用(without
)在

generate_all_figures
(在您的代码中将
plot_sheets
使用
s
)中创建三个数字,并保持在列表中。
plot_sheet``s

window
显示此列表中的第一个图形。

Buttons
删除带有图形的画布,并从列表中使用下一个/上一个图形创建新的画布。

我使用

grid()
而不是
pack()
因为这样可以轻松地将新画布放在同一位置。

import tkinter as tkimport matplotlib.pyplot as pltfrom matplotlib.backends.backend_tkagg import FigureCanvasTkAggclass MyClass():    def __init__(self):        self.sheets = [[1,2,3], [3,1,2], [1,5,1]]        self.W = 2        self.L = 5        self.all_figures = []    def plot_sheet(self, data):        """plot single figure"""        fig, ax = plt.subplots(1)        ax.set_xlim([0, self.W])         ax.set_ylim([0, self.L])        ax.plot(data)        return fig    def generate_all_figures(self):        """create all figures and keep them on list"""        for data in self.sheets: fig = self.plot_sheet(data) self.all_figures.append(fig)def show_figure(number):    global dataPlot    # remove old canvas    if dataPlot is not None: # at start there is no canvas to destroy        dataPlot.get_tk_widget().destroy()    # get figure from list    one_figure = my_class.all_figures[number]    # display canvas with figuere    dataPlot = FigureCanvasTkAgg(one_figure, master=window)    dataPlot.draw()    dataPlot.get_tk_widget().grid(row=0, column=0)def on_prev():    global selected_figure    # get number of previous figure    selected_figure -= 1    if selected_figure < 0:        selected_figure = len(my_class.all_figures)-1    show_figure(selected_figure)def on_next():    global selected_figure    # get number of next figure    selected_figure += 1    if selected_figure > len(my_class.all_figures)-1:        selected_figure = 0    show_figure(selected_figure)# --- main ---my_class = MyClass()my_class.generate_all_figures()window = tk.Tk()window.rowconfigure(0, minsize=500)    # minimal heightwindow.columnconfigure(0, minsize=700) # minimal width# display first figure    selected_figure = 0dataPlot = None # default value for `show_figure`show_figure(selected_figure)# add buttons to change figuresframe = tk.frame(window)frame.grid(row=1, column=0)b1 = tk.Button(frame, text="<<", command=on_prev)b1.grid(row=0, column=0)b2 = tk.Button(frame, text=">>", command=on_next)b2.grid(row=0, column=1)window.mainloop()

可能无需替换画布即可完成,但可以替换剧情中的数据(

fig.data
???,
ax.data
???我不记得了)



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/455988.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号