Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

python - Error in matplotlib popup window (AttributeError: 'NoneType' object has no attribute 'set_canvas')

I tried to plot the graph in pop up window. It pops up. But there is an error.

import tkinter as tk
window = tk.Tk()
window.configure(background='white')


label_1 = tk.Label(window, text="Conpyright 123456789123456798", anchor=tk.S)
label_1.pack()

ws = window.winfo_screenwidth()
hs = window.winfo_screenheight()
w = 980  # width for the Tk root
h = 600  # height for the Tk root
x = (ws / 2) - (w / 2)
y = (hs / 2) - (h / 2)



window.geometry('%dx%d+%d+%d' % (w, h, x, y))
canvas = tk.Canvas(window, bg="white", width=980, height=580, highlightthickness=0)
canvas.pack()
canvas_scroll = tk.Scrollbar(canvas, command=canvas.yview)
canvas_scroll.place(relx=1, rely=0, relheight=1, anchor=tk.NE)
canvas.configure(yscrollcommand=canvas_scroll.set, scrollregion=())



minw_var = tk.DoubleVar()
entry_minw_number = tk.Entry(canvas, textvariable=minw_var)
canvas.create_window(220,215, window=entry_minw_number)

maxw_var = tk.DoubleVar()
entry_maxw_number = tk.Entry(canvas, textvariable=maxw_var)
canvas.create_window(355,215, window=entry_maxw_number)


minl_var = tk.DoubleVar()
entry_minl_number = tk.Entry(canvas, textvariable=minl_var)
canvas.create_window(220,240, window=entry_minl_number)

maxl_var = tk.DoubleVar()
entry_maxl_number = tk.Entry(canvas, textvariable=maxl_var)
canvas.create_window(355,240, window=entry_maxl_number)

rect_var = tk.IntVar()
entry_rect_number = tk.Entry(canvas, textvariable=rect_var)
canvas.create_window(290,270, window=entry_rect_number)

And this is the part for matplotlib

-------------------------------------------------------------------------

def plot_sheet(self):
    fig,ax = plt.subplots(1)
    ax.set_xlim([0, self.W]) 
    ax.set_ylim([0, self.L]) 
    recs = []
    for i in range(len(self.rect_list)):
        if self.rect_rotate[i]:
            ax.add_patch(patches.Rectangle((self.rect_pos[i][0], self.rect_pos[i][1]), self.rect_list[i].l, self.rect_list[i].w,linewidth=3,edgecolor='r'))
        else:
            ax.add_patch(patches.Rectangle((self.rect_pos[i][0], self.rect_pos[i][1]), self.rect_list[i].w, self.rect_list[i].l,linewidth=3,edgecolor='r'))
    #plt.show()
    return fig
def plot_sheets(self):
    for i in range(len(self.sheets)):
        self.sheets[i].plot_sheet()

def cal_culate1():

    fig = packing_options[best_index].plot_sheets()

    dataPlot = FigureCanvasTkAgg(fig, master = window)
    dataPlot.show()
    dataPlot.get_tk_widget().pack(side='top', fill='both', expand=1)

window.mainloop()

I wrote dataPlot = FigureCanvasTkAgg(fig, master = window). There is an error in master = window.

File "", line 687, in cal_culate1 dataPlot = FigureCanvasTkAgg(fig, master = window)

File "C:UsersselAnaconda3libsite-packagesmatplotlibackends_backend_tk.py", line 204, in init super(FigureCanvasTk, self).init(figure)

File "C:UsersselAnaconda3libsite-packagesmatplotlibackend_bases.py", line 1618, in init figure.set_canvas(self)

AttributeError: 'NoneType' object has no attribute 'set_canvas'

What should be written there?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You didn't show how you create your class for plotting, so i can only go by assumption here. First create a empty list:

import tkinter as tk
window = tk.Tk()
window.configure(background='white')
figure_holder = []

Then append to the list when you create your figure:

def plot_sheets(self):
    for i in range(len(self.sheets)):
        a = self.sheets[i].plot_sheet()
        figure_holder.append(a)

Retrieve the figure object from the list when you plot it:

def cal_culate1():

    fig = figure_holder[0]

    dataPlot = FigureCanvasTkAgg(fig, master = window)
    #dataPlot.show()
    dataPlot.get_tk_widget().pack(side='top', fill='both', expand=1)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...