#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from tkinter import *

# on crée une fonction de dispatch
def dispatch_yview (*args):
    """
        cette fonction dispatche les commandes de la scrollbar
        VERTICALE entre les deux canevas;
    """
    canvas1.yview(*args)
    canvas2.yview(*args)
# end def

# on crée la fenêtre principale
fenetre = Tk()

# on ajoute des widgets

# options de texte
txt_options = dict(
    text="Hello, world!",
    font="sans 16 bold",
    fill="indian red",
)

# canvas 1
canvas1 = Canvas(fenetre, bg="pale goldenrod", width=200, height=200)
canvas1.create_text(100, 100, **txt_options)
canvas1.grid(row=0, column=0, sticky=NW+SE)

# canvas 2
canvas2 = Canvas(fenetre, bg="sky blue", width=200, height=200)
canvas2.create_text(100, 100, **txt_options)
canvas2.grid(row=0, column=1, sticky=NW+SE)

# horizontal scrollbar canvas 1
hbar1 = Scrollbar(fenetre, orient=HORIZONTAL)
hbar1.grid(row=1, column=0, sticky=EW)

# horizontal scrollbar canvas 2
hbar2 = Scrollbar(fenetre, orient=HORIZONTAL)
hbar2.grid(row=1, column=1, sticky=EW)

# VERTICAL scrollbar
vbar = Scrollbar(fenetre, orient=VERTICAL)
vbar.grid(row=0, column=2, sticky=NS)

# on rend les canevas redimensionnables
fenetre.rowconfigure(0, weight=1)
fenetre.columnconfigure(0, weight=1)
fenetre.columnconfigure(1, weight=1)

# on connecte les scrollbars aux canevas
canvas1.configure(
    xscrollcommand=hbar1.set,
    yscrollcommand=vbar.set,
    scrollregion=(0, 0, 800, 600),
)
canvas2.configure(
    xscrollcommand=hbar2.set,
    yscrollcommand=vbar.set,
    scrollregion=(0, 0, 800, 600),
)
hbar1.configure(command=canvas1.xview)
hbar2.configure(command=canvas2.xview)

# on relie la fonction de dispatch ici
vbar.configure(command=dispatch_yview)

# on lance la boucle principale
fenetre.mainloop()
