对于每个绘图,新框架

时间:2015-06-04 08:11:29

标签: python python-3.x matplotlib tkinter frames

我正在练习一些tkinter和matplotlib,我遇到了一些我想学习怎么做的东西。这个想法很简单:每个情节都有一个新页面(一个新的框架)。

它是我正在处理的真实场景的模拟。在这个例子中,你应该输入应该发生的图的数量然后你点击'文件' > '打开情节'然后绘图开始了。但问题是......我知道如何将一个图形嵌入到一个框架中,但我想学习如何在不同的框架中嵌入多个图形,所以稍后我可以创建一个' next&#39 ;按钮进入下一个包含下一个图的帧。

以下代码不是太开发,可能包含错误,但我希望它能告知这个想法:

from tkinter import *
import matplotlib
matplotlib.use('TkAgg')
from matplotlib import pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg

x = [1,2,3]
y = [1,2,3]

numberofplots = None


numberofplots = eval(input('Number of plots: '))

class app():
    global numberofplots
    def __init__(self):
        root = Tk()
        root.geometry('640x400')
        menu_bar = Menu(root)
        root.configure(menu = menu_bar)
        menu_file = Menu(menu_bar)
        menu_bar.add_cascade(label='File',menu = menu_file)
        menu_file.add_command(label='Open plots',command = openPlots)
        root.mainloop()

def openPlots():
    global numberofplots
    fig = plt.figure()
    for i in range(numberofplots):
        plt.plot(x,y)

        # numberofplots = number of frames to be created

        frame = Frame(root)
        frame.pack()
        canvas = FigureCanvasTkAgg(fig, frame)
        canvas.show()
        canvas.get_tk_widget().pack(fill='both', expand=True)
        toolbar = NavigationToolbar2TkAgg(canvas, frame)
        toolbar.update()
        canvas._tkcanvas.pack(fill='both', expand=True)

run = app()

0 个答案:

没有答案