0

in my Tkinter GUI widget, the submit button commands to draw a plot on a designated frame (plotframe). Initially, the canvas code, and the navigation toolbar work. However, every time I press the submit button, the frame expands and a new graph is created rather than plotting in the same assigned area.

How can I modify the code so that the plot is updated on itself? In addition, the save option of the navigation toolbar only saves the plot not the whole content of the frame. Would it possible to arrange it to save the plot and its associated text?

I appreciate it if anyone helps me to resolve the two issues.

All the best, Payman

from tkinter import *
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec
from matplotlib.backends._backend_tk import NavigationToolbar2Tk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg


def main ():
    root = Tk()
    w , h = root.winfo_screenwidth() , root.winfo_screenheight()
    root.geometry("%dx%d+0+0" % (w , h))
    root.resizable(width=True , height=True)
    root.title("Form")
    root.configure(bg='Dodgerblue4')
    root.state('zoomed')

    top_frame = Frame(root , bg='Dodgerblue4' ,borderwidth=2, bd=1, relief= SUNKEN, pady=10)
    frameX = Frame(root , bg='Dodgerblue4' ,borderwidth=2, bd=1 , relief=SUNKEN, padx = 15, pady = 10)
    frameY = Frame(root , bg='Dodgerblue4' ,borderwidth=2, bd=1 , relief=SUNKEN, padx = 15, pady = 10)
    submitframe = Frame(root , bg='Dodgerblue4' ,borderwidth=2, bd=1 , relief=SUNKEN)
    plotframe = Frame(root , bg='Dodgerblue4' ,borderwidth=2, bd=1 , relief=SUNKEN)
    # plotframe = Canvas(root , bg='Dodgerblue4' ,borderwidth=2, bd=1 , relief=SUNKEN)


    top_frame.grid(column=0, row=0, sticky="nsew", columnspan= 20)
    top_frame.grid_columnconfigure(0 , weight=1)
    frameX.grid(column=0, row=1, sticky="nsew", columnspan = 2, rowspan = 6)
    frameY.grid(column=0, row=7, sticky="nsew", columnspan = 2, rowspan = 16)
    submitframe.grid(column=2, row=1, sticky="nsew", rowspan = 22)
    submitframe.grid_rowconfigure(0 , weight=1)

    plotframe.grid(column=3, row=1, sticky="nsew", rowspan = 22, columnspan = 17 )
    plotframe.grid_rowconfigure(0 , weight=1)
    plotframe.grid_columnconfigure(0 , weight=1)

    label_top = Label(top_frame , text="Design Page" , font=('' , '15' , ''), bg='Dodgerblue4' ,  fg='white', justify=CENTER).grid(row=0, column=0,columnspan= 20)

   # ------------------------------------------Xs-------------------------------------------------------------------------
    X1 = Label(frameX, text='X1', fg='white', bg='Dodgerblue4', font=('' , '12' , ''), justify=CENTER).grid(row=1, column=0, sticky = W)
    X2 = Label(frameX, text='X2', fg='white', bg='Dodgerblue4', font=('' , '12' , ''), justify=CENTER).grid(row=2, column=0, sticky = W)
    X3 = Label(frameX, text='X3', fg='white', bg='Dodgerblue4', font=('' , '12' , ''), justify=CENTER).grid(row=3, column=0, sticky = W)
    X4 = Label(frameX, text='X4', fg='white', bg='Dodgerblue4', font=('' , '12' , ''), justify=CENTER).grid(row=4, column=0, sticky = W)
    X5 = Label(frameX, text='X5', fg='white', bg='Dodgerblue4', font=('' , '12' , ''), justify=CENTER).grid(row=5, column=0, sticky = W)
    X6 = Label(frameX, text='X6', fg='white', bg='Dodgerblue4', font=('' , '12' , ''), justify=CENTER).grid(row=6, column=0, sticky = W)
    # -------------------------------------------Ys--------------------------------------------------------------------
    label_spec = Label(frameY, text='the parameters', fg='white', bg='Dodgerblue4', font=('' , '12' , ''), justify=CENTER).grid(row=0, column=0, columnspan = 2)
    Y1 = Label(frameY, text='Y1', fg='white', bg='Dodgerblue4', font=('' , '12' , ''), justify=CENTER).grid(row= 1, column= 0, sticky = W)
    Y2 = Label(frameY, text='Y2', fg='white', bg='Dodgerblue4', font=('' , '12' , ''), justify=CENTER).grid(row= 2, column= 0, sticky = W)
    Y3 = Label(frameY, text='Y3', fg='white', bg='Dodgerblue4', font=('' , '12' , ''), justify=CENTER).grid(row= 3, column= 0, sticky = W)
    Y4 = Label(frameY, text='Y4', fg='white', bg='Dodgerblue4', font=('' , '12' , ''), justify=CENTER).grid(row= 4, column= 0, sticky = W)
    Y5 = Label(frameY, text='Y5', fg='white', bg='Dodgerblue4', font=('' , '12' , ''), justify=CENTER).grid(row= 5, column= 0, sticky = W)
    Y6 = Label(frameY, text='Y6', fg='white', bg='Dodgerblue4', font=('' , '12' , ''), justify=CENTER).grid(row= 6, column= 0, sticky = W)
    Y7 = Label(frameY, text='Y7', fg='white', bg='Dodgerblue4', font=('' , '12' , ''), justify=CENTER).grid(row= 7, column= 0, sticky = W)
    Y8 = Label(frameY, text='Y8', fg='white', bg='Dodgerblue4', font=('' , '12' , ''), justify=CENTER).grid(row= 8, column= 0, sticky = W)
    Y9 = Label(frameY, text='Y9', fg='white', bg='Dodgerblue4', font=('' , '12' , ''), justify=CENTER).grid(row= 9, column= 0, sticky = W)
    Y10 = Label(frameY, text='Y10', fg='white', bg='Dodgerblue4', font=('' , '12' , ''), justify=CENTER).grid(row= 10, column= 0, sticky = W)
    Y11 = Label(frameY, text='Y11', fg='white', bg='Dodgerblue4', font=('' , '12' , ''), justify=CENTER).grid(row= 11, column= 0, sticky = W)
    Y12 = Label(frameY, text='Y12', fg='white', bg='Dodgerblue4', font=('' , '12' , ''), justify=CENTER).grid(row= 12, column= 0, sticky = W)
    Y13 = Label(frameY, text='Y13', fg='white', bg='Dodgerblue4', font=('' , '12' , ''), justify=CENTER).grid(row= 13, column= 0, sticky = W)

    def todo():
        x = [-100 , 100]
        y = [0 , 4]
        fig = plt.figure( facecolor='#104e8b' , edgecolor='k', figsize=(9,6))
        gs = fig.add_gridspec(13 , 7 , top=1)

        ax1 = fig.add_subplot(gs[1:8 , 0:5])
        ax1.plot(x , y , 'b-' , label=' Profile 1')
        plt.xlabel(' x1 (mm) ' , color='w')
        plt.ylabel(' y1 (mm)' , color='w')
        plt.legend(loc='lower left')

        ax2 = fig.add_subplot(gs[1:12 , 5:6])
        ax2.text(0.05 , 0.95 ,
                 "The  Specifications: \n Y1 \n\n Y2, \n\n Y3, \n\n Y4, \n\n Y5,\n\n Y6, \n\n Y7, \n\n Y8, "
                 "\n\n Y9,\n\n Y10, \n\n Y11, \n\n Y12, \n\n Y13",
                 style='normal' , color='w' , fontsize=10 ,transform=ax2.transAxes, va='top',
                 bbox={'facecolor': 'blue' , 'alpha': 0. , 'pad': 10}
                 )
        ax2.axis('off')

        ax3 = fig.add_subplot(gs[8:12 , 0:5])
        ax3.plot(x , y , 'b-' , label=' Profile 2')
        plt.xlabel(' x2 (mm) ' , fontsize=10 , color='w')
        plt.ylabel(' y2 (mm)' , fontsize=10 , color='w')

        canvas = FigureCanvasTkAgg(fig , master=plotframe)
        canvas.get_tk_widget().pack(fill=BOTH , expand=1)
        toolbar = NavigationToolbar2Tk(canvas , plotframe)
        toolbar.config(background='Dodgerblue4')
        toolbar._message_label.config(background='Dodgerblue4')
        toolbar.update()
        canvas.draw()


    submit_button = Button(submitframe , text='Submit' , command=todo , font=('' , '12' , '') , padx=10 , relief=RIDGE ,
                           activebackground='green' , bg='orange' , bd=5)
    submit_button.grid(row=0 , column=0 , rowspan=10 , ipady=10)
    submit_button.flash()
    submit_button.invoke()

    root.mainloop()

if __name__ == '__main__':
    main()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.