[Python] Gui to view matplotlib figure with forward and backward button

Discussão em 'Python' iniciado por Stack, Maio 25, 2025.

  1. Stack

    Stack Membro Participativo

    I want to build a GUI to view matplotlib figures with forward and backward buttons. GUI is just similar to view images. To an extent, I could get the results I need. The problem is: that when I use the backward button(<<) the size of the figure increases and I could not find out why. Is there anyone out there who can help understand what is missing and fix the size so it does not increase? Following are the codes for reference:

    from tkinter import *
    import tkinter as tk
    from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg,NavigationToolbar2Tk)
    import matplotlib.pyplot as plt

    # Variables for line plots
    x= [1,2,3,4]
    y=[4,5,6,7]

    x1=[5,6,7,3,1]
    y1=[8,9,10,2,4]

    x2=[1,4,2,5,9,3]
    y2=[2,5,1,6,1,4]


    # Creating figures
    fig1,ax1 = plt.subplots()
    ax1.plot(x, y, marker='o', label="Data Points",color='red')
    ax1.set_title("Basic Components of Matplotlib Figure")
    ax1.set_xlabel("X-Axis")
    ax1.set_ylabel("Y-Axis")
    fig1.tight_layout()
    # plt.show()

    fig2,ax2 = plt.subplots()
    ax2.plot(x1, y1, marker='o', label="Data Points",color='green')
    ax2.set_title("Basic Components of Matplotlib Figure")
    ax2.set_xlabel("X-Axis")
    ax2.set_ylabel("Y-Axis")
    fig2.tight_layout()

    fig3,ax3 = plt.subplots()
    ax3.plot(x2, y2, marker='o', label="Data Points",color='orange')
    ax3.set_title("Basic Components of Matplotlib Figure")
    ax3.set_xlabel("X-Axis")
    ax3.set_ylabel("Y-Axis")
    fig3.tight_layout()

    fig_list = [fig1,fig2,fig3]



    def frwrd(number):
    global Fw_Btn
    global Bck_Btn
    global exit_btn
    global frame1
    global Fig_Canvas

    frame1.destroy()

    # Creating a frame in root
    frame1= Frame(root,bg= "green")
    frame1.grid(row=1,column=0,columnspan=3,padx=10,pady=10,sticky='nsew')

    # Create a figure canvas again
    Fig_Canvas = FigureCanvasTkAgg(fig_list[number-1],master = frame1)
    Fig_Canvas.draw()
    Fig_Canvas.get_tk_widget().grid(row=0,column=0)

    # Creating buttons again
    Fw_Btn = Button(root, text = '>>',command=lambda:frwrd(number + 1))
    Bck_Btn = Button(root, text = '<<',command= lambda: back(number - 1))
    if number ==3:
    Fw_Btn = Button(root, text = '>>',state=DISABLED)

    Fw_Btn.grid(row=0,column=2,sticky='nswe')
    Bck_Btn.grid(row=0,column=0,sticky='nswe')
    exit_btn.grid(row=0,column=1,sticky='nswe')


    def back(number):
    global Fw_Btn
    global Bck_Btn
    global exit_btn
    global frame1
    global Fig_Canvas


    frame1.destroy()

    # Creating a frame inplace of root
    frame1= Frame(root,bg= "green")
    frame1.grid(row=1,column=0,columnspan=3,padx=10,pady=10,sticky='nsew')

    # Create a figure canvas again
    Fig_Canvas = FigureCanvasTkAgg(fig_list[number-1],master = frame1)
    Fig_Canvas.draw()
    Fig_Canvas.get_tk_widget().grid(row=0,column=0)
    # Creating buttons again
    Fw_Btn = Button(root, text = '>>',command=lambda:frwrd(number + 1))
    Bck_Btn = Button(root, text = '<<',command= lambda: back(number - 1))
    if number ==1:
    Bck_Btn = Button(root, text = '<<',state=DISABLED)

    Fw_Btn.grid(row=0,column=2,sticky='nswe')
    Bck_Btn.grid(row=0,column=0,sticky='nswe')
    exit_btn.grid(row=0,column=1,sticky='nswe')


    # root of tkinter
    root = Tk()
    root.geometry('700x600')

    # Buttons
    Fw_Btn = Button(root, text = '>>',command=lambda: frwrd(2))
    Bck_Btn = Button(root, text = '<<',command= back,state=DISABLED)
    exit_btn = Button(root,text = 'Exit',command= root.quit)

    # Placing buttons
    Bck_Btn.grid(row=0,column=0,sticky='nswe')
    exit_btn.grid(row=0,column=1,sticky='nswe')
    Fw_Btn.grid(row=0,column=2,sticky='nswe')

    # Creating a frame inplace of root
    frame1= Frame(root,bg= "green")
    frame1.grid(row=1,column=0,columnspan=3,padx=10,pady=10,sticky='nsew')

    # Creating a figure canvas
    Fig_Canvas = FigureCanvasTkAgg(fig1,master = frame1)
    Fig_Canvas.draw()
    Fig_Canvas.get_tk_widget().grid(row=0,column=0)

    # Configureing the root
    root.grid_columnconfigure([0,1,2],weight=1)
    root.grid_rowconfigure(1,weight=1)
    root.mainloop()

    Continue reading...

Compartilhe esta Página