'''
ARGOMENTI: 
---------------------------------------------------------------------

	- Canvas
			Area rettangolare destinata al disegno di immagini o altri layout complessi. 
			Al suo interno si possono disporre grafici, testo, widget o cornici.

				Sintassi   c = Canvas (PARENT, OPTION = valore, ...) 

			dove PARENT rappresenta la finestra "genitrice" e 
			OPTION puo' essere:

				bd 				- larghezza del bordo in pixel. L'impostazione predefinita è 2
				bg      	  	- colore di sfondo normale
				confine 	  	- se e' TRUE (impostazione predefinita), non e' possibile scorrere 
								  la tela all'esterno della regione di scorrimento.
				cursor 	  		- cursore utilizzato nella tela: arrow, circle, dot etc.
				widht/height  	- dimensioni X Y della tela (larghezza, altezza)
				highlightcolor	- colore del bordo quando il canvas e' "messo a fuoco"
				relief		  	- specifica il tipo di bordo. Alcuni dei valori sono 
										SUNKEN, RAISED, GROOVE e RIDGE
				scrollregion  	- tupla (w, n, e, s) che definisce su quanto grande e' l'area in 
									cui e' possibile scorrere la tela dove: 
										"w" e' il lato sinistro
										"n" la parte superiore 
										"e" la parte destra
										"s" la parte inferiore
				xscrollincrement- le "tele" possono essere fatte scorrere ORIZZONTALMENTE in qualsiasi posizione. 
								  Se si imposta questa opzione ad un valore positivo la tela puo' essere
								  posizionata solo su multipli di questo valore.
								  Ad esempio, il valore sara' usato da una barra di scorrimento associata
				xscrollcommand	- se la tela e' scorrevole, questo attributo dovra' essere il metodo 
								  .set () della barra di scorrimento ORIZZONTALE.
				yscrollincrement- IDEM per scorrimento VERTICALE
				yscrollcommand	- IDEM per scorrimento VERTICALE


			I metodi che il "canvas" supporta sono:

				create_arc()		- disegna un arco
				create_bitmap()		- posiziona una bitmap
				create_image()		- posiziona un'immagine, che puo essere un'istanza delle classi BitmapImage o PhotoImage
				create_line() 		- disegna una linea
				create_oval() 		- crea un cerchio o un'ellisse con coordinate date. 
				create_polygon()	- crea un oggetto "poligono" che deve avere almeno tre vertici
				create_rectangle()	- crea un rettangolo
				create_text()		- posiziona un testo
				create_window()		- posiziona un widget



'''
from tkinter import *
import random

cnvBK="white"  	# colore di sfondo del canvas

# =================================================================
# Funzioni associate ai comandi
# =================================================================
def f_Stato_Iniziale():
	CNV.create_rectangle(1,1,xCAN,yCAN, fill=cnvBK)   			# OK linux, non perfetto WINDOWS  			
	CNV.create_line(0,0,yCAN,yCAN, fill="red", dash=(4, 4))
	CNV.create_line(0,yCAN,xCAN,0, fill="red", dash=(4, 4))

def f_Triangolo():
	x1=random.randint(1, xCAN+1)
	y1=random.randint(1, yCAN+1)
	x2=random.randint(1, xCAN+1)
	y2=random.randint(1, yCAN+1)
	x3=random.randint(1, xCAN+1)
	y3=random.randint(1, yCAN+1)
	COORDINATE = x1,y1,x2,y2,x3,y3
	FILL=""
	if chk_FIL_Var.get(): FILL="yellow"
	CNV.create_polygon(COORDINATE, fill=FILL, outline="black")

def f_Rettangolo():
	x1=random.randint(1, xCAN+1)
	y1=random.randint(1, yCAN+1)
	x2=random.randint(1, xCAN+1)
	y2=random.randint(1, yCAN+1)
	COORDINATE= x1,y1,x2,y2
	FILL=""
	if chk_FIL_Var.get(): FILL="orange"
	CNV.create_rectangle(COORDINATE, fill=FILL, outline="black")

def f_Arco():
	XCEN=random.randint(1, xCAN+1)
	YCEN=random.randint(1, yCAN+1)
	ALT=random.randint(1, xCAN+1)
	LAR=random.randint(1, yCAN+1)
	STA=random.randint(1, 361)
	EXT=random.randint(1, 361)
	COORDINATE = XCEN, YCEN, ALT, LAR
	FILL=""
	if chk_FIL_Var.get(): FILL="blue"
	CNV.create_arc(COORDINATE, start=STA, extent=EXT, fill=FILL, outline="black")

def f_Ovale():
	x1=random.randint(1, xCAN+1)
	y1=random.randint(1, yCAN+1)
	x2=random.randint(1, xCAN+1)
	y2=random.randint(1, yCAN+1)
	COORDINATE = x1,y1,x2,y2
	FILL=""
	if chk_FIL_Var.get(): FILL="green"
	CNV.create_oval(COORDINATE, fill=FILL, outline="black")

def f_Linea():
	x1=random.randint(1, xCAN+1)
	y1=random.randint(1, yCAN+1)
	x2=random.randint(1, xCAN+1)
	y2=random.randint(1, yCAN+1)
	COORDINATE = x1,y1,x2,y2
	CNV.create_line(COORDINATE, width=4, fill="red")

def f_Testo():
	x1=random.randint(1, xCAN+1)
	y1=random.randint(1, yCAN+1)
	COORDINATE = x1,y1
	CNV.create_text(COORDINATE, text=txt_ENT_Var.get())

def f_Coordinate():
	x1=random.randint(50, xCAN-50)
	y1=random.randint(50, yCAN-50)
	COOR_PUNTO = x1-2,y1-2,x1+2,y1+2 
	COOR_XY = x1,y1+10
	XY= "(" + str(x1) + "," +  str(y1)+ ")"
	CNV.create_oval(COOR_PUNTO, fill="black", outline="black")
	CNV.create_text(COOR_XY, text=XY, fill="black" )

def f_DrawPunto_InFi(event):
	x1=event.x
	y1=event.y
	COOR_PUNTO = x1-2,y1-2,x1+2,y1+2 
	COOR_XY = x1,y1+10
	XY= "(" + str(x1) + "," +  str(y1)+ ")"
	CNV.create_oval(COOR_PUNTO, fill="black", outline="black")
	CNV.create_text(COOR_XY, text=XY, fill="black" )


def f_DrawPunto(event):
	x1=event.x
	y1=event.y
	COOR_PUNTO = x1-1,y1-1,x1+1,y1+1
	CNV.create_oval(COOR_PUNTO, fill="black", outline="black") 

# =================================================================




# =================================================================
# LAYOUT
# =================================================================
# definizione della finestra
fin = Tk()
fin.title("Applicazione")
fin.geometry("500x500+300+50")
fin.resizable(width=False, height=False)


# -------------------------------------------------------------
# labelframe CANVAS
# -------------------------------------------------------------
lblFRA_1=LabelFrame(fin, text="Canvas", pady=20)
xCAN=yCAN=300
CNV = Canvas(lblFRA_1, bg=cnvBK, cursor="dot", height=yCAN, width=xCAN)
CNV.pack()
lblCAN = Label(text="(0,0)")
CNV.create_window(3, 3, window=lblCAN, anchor=N+W)
lblFRA_1.pack(fill="x")     
    

# -------------------------------------------------------------
# labelframe AZIONI
# -------------------------------------------------------------
lblFRA_2=LabelFrame(fin, text="Azioni", padx=10, pady=10)
lblFRA_2.pack(pady=(20, 0)) # padding verticale 20:nord, 0:sud
# Pulsanti degli oggetti con possibilita' di FILL 
btn_TRI = Button(lblFRA_2, text="Triangolo", command=f_Triangolo).grid(row=0,column=0, sticky="we") 
btn_RET = Button(lblFRA_2, text="Rettangolo", command=f_Rettangolo).grid(row=0,column=1, sticky="we") 
btn_ARC = Button(lblFRA_2, text="Arco", command=f_Arco).grid(row=0,column=2, sticky="we")  
btn_OVA = Button(lblFRA_2, text="Ovale", command=f_Ovale).grid(row=0,column=3, sticky="we")  
# ---------------------
chk_FIL_Var= IntVar()
chk_FIL = Checkbutton(lblFRA_2, text="Fill", variable=chk_FIL_Var).grid(row=0,column=4, sticky="we")
# Altri pulsanti  
btn_LIN = Button(lblFRA_2, text="Linea", command=f_Linea).grid(row=1,column=0, sticky="we")
txt_ENT_Var=StringVar() 
txt_ENT_Var.set("Testo")
txt_ENT = Entry(lblFRA_2, textvariable=txt_ENT_Var).grid(row=1,column=1, sticky="we")
btn_TES = Button(lblFRA_2, text="Testo", command=f_Testo).grid(row=1,column=2, sticky="we") 
btn_COO = Button(lblFRA_2, text="(X,Y)", command=f_Coordinate).grid(row=1,column=3, sticky="we")  
btn_CLE = Button(lblFRA_2, text="Clear", command=f_Stato_Iniziale).grid(row=1,column=4, sticky="we")  
# -------------------------------------------------------------


# =================================================================
# BIND - CANVAS
# =================================================================
CNV.bind("<ButtonPress-1>", f_DrawPunto_InFi)      
CNV.bind("<B1-Motion>", f_DrawPunto) 
CNV.bind("<ButtonRelease-1>", f_DrawPunto_InFi)  



f_Stato_Iniziale()
fin.mainloop()