每一块地都有一个新的框架

2024-09-29 19:33:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在练习一些tkinter和matplotlib,我遇到了一些我想学习如何做的事情。想法很简单:对于每一个情节,都有一个新的页面(一个新的框架)。在

这是一个真实场景的模拟,我正在处理。在本例中,您应该输入应该发生的绘图数量,然后单击“文件”>;“打开绘图”,然后开始绘图。但问题是。。。我知道如何在一个帧中嵌入一个图形,但是我想学习如何在不同的帧中嵌入多个图,这样以后我可以创建一个“下一个”按钮,它将转到包含下一个绘图的下一个帧。在

下面的代码不是很完善,可能包含错误,但我希望它能告诉您这个想法:

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()

Tags: fromimport绘图matplotlibtkinterbarpltroot

热门问题