The idea is that I want the graphs only to appear on the scrollable canvas next to the previous graph. At this moment when I create another graph it will just be plotted outside the canvas. Note that I just set the scrollregion to a random high number. It actually needs to grow with each extra graph added. However, I did not figure out how.
Anyone knows what I am doing wrong here?
Anyone knows what I am doing wrong here?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
import tkinter as tk import numpy as np from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg from matplotlib.figure import Figure class Example: def __init__( self , master): self .clicks = 0 self .master = master self .frame = tk.Frame(master,width = 600 ,height = 100 ,background = "green" ) self .canv = tk.Canvas(master, width = 600 , height = 400 ,background = "blue" ,scrollregion = ( 0 , 0 , 2000 , 2000 )) button = tk.Button(master,text = 'click' ,command = lambda : self .select(master)) self .scrollY = tk.Scrollbar(master, orient = tk.VERTICAL, command = self .canv.yview) self .scrollX = tk.Scrollbar(master, orient = tk.HORIZONTAL, command = self .canv.xview) self .canv[ 'xscrollcommand' ] = self .scrollX. set self .canv[ 'yscrollcommand' ] = self .scrollY. set self .frame.grid(row = 0 ,column = 0 ,rowspan = 3 ) self .scrollY.grid(row = 3 , column = 1 , sticky = tk.N + tk.S) button.grid(row = 2 ,column = 0 ,padx = 10 ,sticky = tk.NW) self .scrollX.grid(row = 4 , column = 0 , sticky = tk.E + tk.W) self .canv.grid(row = 3 , column = 0 ) def select( self ,master): self .clicks + = 1 shape = np.random.randint( 0 , 2 ,[ 5 , 5 ]) lon = np.arange( 5 ) lat = np.arange( 5 ) fig = Figure(figsize = ( 4 , 4 )) ax = fig.add_subplot( 111 ) c = ax.pcolor(lon,lat,shape) fig.colorbar(c,ax = ax,fraction = 0.046 ,pad = 0.04 ) canvas = FigureCanvasTkAgg(fig, self .canv) canvas.get_tk_widget().grid(row = 0 ,column = self .clicks) if __name__ = = "__main__" : root = tk.Tk() Example(root) root.mainloop() |