Tkinter:在弹出窗口中绘制(图形)结果 window
Tkinter : Plot (Graph) results in popup window
这是帮助我绘制 python 的线条。
packing_options[best_index].plot_sheets()
这是它在 python 上的样子。这是图表的图片。 https://imgur.com/a/fRczosW
现在,我正在尝试 tkinter。我想让图表弹出。我怎样才能做到这一点 ?
window = tk.Tk()
packing_options[best_index].plot_sheets()
window.mainloop()
我试过了。但是没用。
已编辑:
所以,“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()
def plot_sheets(self):
for i in range(len(self.sheets)):
self.sheets[i].plot_sheet()
这是绘图的代码。 packing_options[best_index]也是这里的一个函数。由于有一个循环,它绘制了大约 10-20 个图。
我如何在这里应用 matplotlib 后端?
我不能运行但它可能是这样的
import matplotlib
matplotlib.use('TkAgg')
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import matplotlib.pyplot as plt
import tkinter as tk
class YourClass():
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
#--- main ---
window = tk.Tk()
packing_options = [YourClass(), YourClass(), YourClass()]
best_index = 0
fig = packing_options[best_index].plot_sheets()
dataPlot = FigureCanvasTkAgg(fig, master=master)
dataPlot.show()
dataPlot.get_tk_widget().pack(side='top', fill='both', expand=1)
window.mainloop()
这是帮助我绘制 python 的线条。
packing_options[best_index].plot_sheets()
这是它在 python 上的样子。这是图表的图片。 https://imgur.com/a/fRczosW
现在,我正在尝试 tkinter。我想让图表弹出。我怎样才能做到这一点 ?
window = tk.Tk()
packing_options[best_index].plot_sheets()
window.mainloop()
我试过了。但是没用。
已编辑: 所以,“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()
def plot_sheets(self):
for i in range(len(self.sheets)):
self.sheets[i].plot_sheet()
这是绘图的代码。 packing_options[best_index]也是这里的一个函数。由于有一个循环,它绘制了大约 10-20 个图。 我如何在这里应用 matplotlib 后端?
我不能运行但它可能是这样的
import matplotlib
matplotlib.use('TkAgg')
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import matplotlib.pyplot as plt
import tkinter as tk
class YourClass():
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
#--- main ---
window = tk.Tk()
packing_options = [YourClass(), YourClass(), YourClass()]
best_index = 0
fig = packing_options[best_index].plot_sheets()
dataPlot = FigureCanvasTkAgg(fig, master=master)
dataPlot.show()
dataPlot.get_tk_widget().pack(side='top', fill='both', expand=1)
window.mainloop()