Copy Link
Add to Bookmark
Report

Tcl/tk (parte II)

eZine's profile picture
Published in 
bakunin
 · 21 Aug 2022

BAKUNIN
bakunin@meganmail.com


Questa è la parte avanzata del primo corso.
Parto dal presupposto che voi già sappiate fare molto (cioè tutto quello che vi ho scritto nel primo corso!).

Quindi bando alle ciance, e incominciamo!
Il mio progetto è riprendere tutto da capo con spiegazioni e opzioni più approfondite:

INDICE

  1. label
  2. checkbutton
  3. image
  4. Listbox e scrollbar
  5. Entry
  6. Text
  7. Disegni
  8. Messaggi

1. Label

Come voi sapete servono per creare delle etichette, delle scritte. Il vostro modo di farlo era questo:

label .ciao -text "Tanto va la gatto al largo che... affoga!"

Bene. Vi insegno che potete farle anche in modo tridimensionale. Questo può essere rientrante o uscente dalla finestra.

label .ciao -text "Tanto va la gatto al largo che... affoga!" -relief raised

Uscente (classico pulsante!)

label .ciao -text "Tanto va la gatto al largo che... affoga!" -relief sunken

Rientrante.

Ma lo sapete che potete mettere anche immagini. Queste però vengono schifide. In versione testo. Devono essere però rigorosamente in formato bmp. Il modo per fare queste immagini è:

label .ciao -borderwidth 2 -relief sunken \ 
-bitmap @[file join lettera.bmp]

"Cosa vuol dire?" Innanzi tutto che apre il file lettera.bmp che è allegato in questa guida. Apre in formato bitmap il file lettera.bmp.

lettera.bmp
Pin it
lettera.bmp

Vi ho anche insegnato un'altra cosa: il comando borderwidth. Questo regola il bordo a 2. Posso quindi impostare il bordo!

2. Checkbutton

Forse di questo manco vi avevo parlato! Errore grave, perché sono molto utili. Questi sono i classici pulsantini da schiacciare. Se non sono premuti hanno la variabile con valore 0. Altrimenti 1.

Se quindi faccio:

checkbutton .ciao1 -text "Casa" -variable casa -relief flat 
checkbutton .ciao2 -text "Edificio" -variable edificio -relief flat
checkbutton .ciao3 -text "Catapecchia" -variable catapecchia -relief flat

Posso poi vedere le loro variabili così:

label .mamma -text "Il valore di casa è: $casa, \n quello di edificio, \

invece è: $edificio,\n e per ultimo, quello di catapecchia è: $catapecchia"

Posso quindi selezionare più di una cosa.

Posso anche qui inserire un immagine, nello stesso ed identico modo delle label:

-bitmap @[file join lettera.bmp]

Questa è l'opzione da aggiungere al posto del testo.

3. Image

Questo è un comando che permette di inserire un'immagine in modalità grafica perfetta (o meglio migliore! Non esiste ancora la grafica perfetta, in nessun programma!).
Questo comando funziona così:

image create photo immagine -file [file join earth.gif] 
label .ciao -image immagine -bd 1 -relief sunken

Come potete vedere deve essere abbinato ad un comando label.

earth.gif
Pin it
earth.gif

Questo quindi funziona così:
crea un'immagine in modalità foto (poteva anche essere bitmap al posto che photo e avrei avuto la grafica orrenda di prima!) dal file ....

Poi per richiamarla dal label basta inserire -immage.

Avrei potuto richiamarla pure da button, il risultato sarebbe stato identico.

4. Listbox e scrollbar

Vediamo l'esempio:

scrollbar .scroll -command ".lista yview" 
listbox .lista -yscroll ".scroll set" -setgrid 1 -height 12
pack .scroll -side right -fill y
pack .lista -side left -expand 1 -fill both

.lista insert 0 Alabama Alaska Arizona Arkansas California \
Colorado Connecticut Delaware Florida Georgia Hawaii Idaho Illinois \
Indiana Iowa Kansas Kentucky Louisiana Maine Maryland \
Massachusetts Michigan Minnesota Mississippi Missouri \
Montana Nebraska Nevada "New Hampshire" "New Jersey" "New Mexico" \
"New York" "North Carolina" "North Dakota" \
Ohio Oklahoma Oregon Pennsylvania "Rhode Island" \
"South Carolina" "South Dakota" \
Tennessee Texas Utah Vermont Virginia Washington \
"West Virginia" Wisconsin Wyoming

Ecco a voi!

Spiegazione: Creo una scrollbar di nome .scroll e ci gli metto come comando yview (cioè fammi vedere in ordinata [asse y] tutto ciò che sta nella lista) Poi creo una lista che si collega all'.scroll. Dopo di che, inserisco nella lista i valori che mi va di vedere.
Semplice!

Cosa molto importante è che i dati della lista devono incominciare con 0.

Così però è un po' inutile.
Guardiamo questo esempio:

scrollbar .scroll -command ".lista yview" 
listbox .lista -yscroll ".scroll set" -width 20 -height 16 -setgrid 1
pack .lista .scroll -side left -fill y -expand 1


bind .lista <Double-1> {
tk_setPalette [selection get]
}


frame.list insert 0 gray60 gray70 gray80 gray85 gray90 gray95 \
snow1 snow2 snow3 snow4 seashell1 seashell2 \
seashell3 seashell4 AntiqueWhite1 AntiqueWhite2 AntiqueWhite3 \
AntiqueWhite4 bisque1 bisque2 bisque3 bisque4 PeachPuff1 \
PeachPuff2 PeachPuff3 PeachPuff4 NavajoWhite1 NavajoWhite2 \
NavajoWhite3 NavajoWhite4 LemonChiffon1 LemonChiffon2 \
LemonChiffon3 LemonChiffon4 cornsilk1 cornsilk2 cornsilk3 \
cornsilk4 ivory1 ivory2 ivory3 ivory4 honeydew1 honeydew2 \
honeydew3 honeydew4 LavenderBlush1 LavenderBlush2 \
LavenderBlush3 LavenderBlush4 MistyRose1 MistyRose2 \
MistyRose3 MistyRose4 azure1 azure2 azure3 azure4 \
SlateBlue1 SlateBlue2 SlateBlue3 SlateBlue4 RoyalBlue1 \
RoyalBlue2 RoyalBlue3 RoyalBlue4 blue1 blue2 blue3 blue4 \
DodgerBlue1 DodgerBlue2 DodgerBlue3 DodgerBlue4 SteelBlue1 \
SteelBlue2 SteelBlue3 SteelBlue4 DeepSkyBlue1 DeepSkyBlue2 \
DeepSkyBlue3 DeepSkyBlue4 SkyBlue1 SkyBlue2 SkyBlue3 \
SkyBlue4 LightSkyBlue1 LightSkyBlue2 LightSkyBlue3 \
LightSkyBlue4 SlateGray1 SlateGray2 SlateGray3 SlateGray4 \
LightSteelBlue1 LightSteelBlue2 LightSteelBlue3 \
LightSteelBlue4 LightBlue1 LightBlue2 LightBlue3 \
LightBlue4 LightCyan1 LightCyan2 LightCyan3 LightCyan4 \
PaleTurquoise1 PaleTurquoise2 PaleTurquoise3 PaleTurquoise4 \
CadetBlue1 CadetBlue2 CadetBlue3 CadetBlue4 turquoise1 \
turquoise2 turquoise3 turquoise4 cyan1 cyan2 cyan3 cyan4 \
DarkSlateGray1 DarkSlateGray2 DarkSlateGray3 \
DarkSlateGray4 aquamarine1 aquamarine2 aquamarine3 \
aquamarine4 DarkSeaGreen1 DarkSeaGreen2 DarkSeaGreen3 \
DarkSeaGreen4 SeaGreen1 SeaGreen2 SeaGreen3 SeaGreen4 \
PaleGreen1 PaleGreen2 PaleGreen3 PaleGreen4 SpringGreen1 \
SpringGreen2 SpringGreen3 SpringGreen4 green1 green2 \
green3 green4 chartreuse1 chartreuse2 chartreuse3 \
chartreuse4 OliveDrab1 OliveDrab2 OliveDrab3 OliveDrab4 \
DarkOliveGreen1 DarkOliveGreen2 DarkOliveGreen3 \
DarkOliveGreen4 khaki1 khaki2 khaki3 khaki4 \
LightGoldenrod1 LightGoldenrod2 LightGoldenrod3 \
LightGoldenrod4 LightYellow1 LightYellow2 LightYellow3 \
LightYellow4 yellow1 yellow2 yellow3 yellow4 gold1 gold2 \
gold3 gold4 goldenrod1 goldenrod2 goldenrod3 goldenrod4 \
DarkGoldenrod1 DarkGoldenrod2 DarkGoldenrod3 DarkGoldenrod4 \
RosyBrown1 RosyBrown2 RosyBrown3 RosyBrown4 IndianRed1 \
IndianRed2 IndianRed3 IndianRed4 sienna1 sienna2 sienna3 \
sienna4 burlywood1 burlywood2 burlywood3 burlywood4 wheat1 \
wheat2 wheat3 wheat4 tan1 tan2 tan3 tan4 chocolate1 \
chocolate2 chocolate3 chocolate4 firebrick1 firebrick2 \
firebrick3 firebrick4 brown1 brown2 brown3 brown4 salmon1 \
salmon2 salmon3 salmon4 LightSalmon1 LightSalmon2 \
LightSalmon3 LightSalmon4 orange1 orange2 orange3 orange4 \
DarkOrange1 DarkOrange2 DarkOrange3 DarkOrange4 coral1 \
coral2 coral3 coral4 tomato1 tomato2 tomato3 tomato4 \
OrangeRed1 OrangeRed2 OrangeRed3 OrangeRed4 red1 red2 red3 \
red4 DeepPink1 DeepPink2 DeepPink3 DeepPink4 HotPink1 \
HotPink2 HotPink3 HotPink4 pink1 pink2 pink3 pink4 \
LightPink1 LightPink2 LightPink3 LightPink4 PaleVioletRed1 \
PaleVioletRed2 PaleVioletRed3 PaleVioletRed4 maroon1 \
maroon2 maroon3 maroon4 VioletRed1 VioletRed2 VioletRed3 \
VioletRed4 magenta1 magenta2 magenta3 magenta4 orchid1 \
orchid2 orchid3 orchid4 plum1 plum2 plum3 plum4 \
MediumOrchid1 MediumOrchid2 MediumOrchid3 MediumOrchid4 \
DarkOrchid1 DarkOrchid2 DarkOrchid3 DarkOrchid4 purple1 \
purple2 purple3 purple4 MediumPurple1 MediumPurple2 \
MediumPurple3 MediumPurple4 thistle1 thistle2 thistle3 \
thistle4

Vediamo subito che le prime tre righe sono come le altre. Passiamo ora poi al comando bind. Questo collega al click del mouse su uno dei colori contenuti nella lista il rispettivo cambiamento dello sfondo.

E' un comando che collega il bind.

Ora avete anche un immenso elenco di colori.

Come è facile immaginare potete inserire anche 2 barre di scorrimento:

scrollbar .yscroll -command "$.lista yview" 
scrollbar .xscroll -orient horizontal -command "$.lista xview"
listbox $.lista -width 20 -height 10 -setgrid 1 -yscroll ".yscroll \
set" -xscroll ".yscroll set"

EX CLARO?

5. Entry

Partendo dal presupposto che sappiate cosa sono, eccovi un bel esempio:

entry .e1 
entry .e2
entry .e3
pack .e1 .e2 .e3 -side top -pady 5 -padx 10 -fill x

.e1 insert 0 "Scritta"
.e2 insert end "Qui la scritta è un pochettino più lunga, ma nulla "
.e2 insert end "di che, per leggerla vi tocca semplicemente sposta"
.e2 insert end "rvi, chiaro?"

perché sono andato a capo, non lo so!
So solo dirvi che ora potete vedere le differenze.

Per avere le barre di scorrimento, basta fare così:

entry .e1 -xscrollcommand ".s1 set" 
scrollbar .s1 -relief sunken -orient horiz -command \
".e1 xview"
frame .spacer1 -width 20 -height 10
entry .e2 -xscrollcommand ".s2 set"
scrollbar .s2 -relief sunken -orient horiz -command \
".e2 xview"
frame .spacer2 -width 20 -height 10
entry .e3 -xscrollcommand ".s3 set"
scrollbar .s3 -relief sunken -orient horiz -command \
".e3 xview"
pack .e1 .s1 .spacer1 .e2 .s2 .spacer2 .e3 .s3 -side top -fill x

.e1 insert 0 "Scritta"
.e2 insert end "Qui la scritta è un pochettino più lunga, ma nulla "
.e2 insert end "di che, per leggerla vi tocca semplicemente sposta"
.e2 insert end "rvi, chiaro?"

Semplice no?

Ho creato delle barre di scorrimento che mi permettono di vedere ... Tutti i comandi li conoscente, quindi capiteveli da soli!

Ovviamente poi io non metto le variabili, ma se le mettete poi potete giostrarvele come vi pare. Ma questo l'abbiamo già visto.

6. Text

Questo è un altro argomento interessante.
I testi visualizzati.

Esempio:

text .text -relief sunken -bd 2 -yscrollcommand ".scroll set" -setgrid 1 \ 
-height 30
scrollbar .scroll -command ".text yview"
pack .scroll -side right -fill y
pack .text -expand yes -fill both
.text insert 0.0 \
{Qui si inserisce tutto il testo che voi volete!

Andando tranquillamente a capo! Senza problemi alcuni...

... 
..
COSI'!}

.text mark set insert 0.0

Spiegazione:
Il comando text visualizza un testo. Ovviamente se il testo è lungo conviene mettere una barra di scorrimento. Qui lo messa perché così potete vedere come si fa dato che il commando per metterla è un po' diverso dagli altri.
Notate che per mettere il testo c'è la procedura adatta. Questo è importante.

Guardiamo l'esempio:

text .text -relief sunken -bd 2 -yscrollcommand ".scroll set" -setgrid 1 \ 
-height 30
scrollbar .scroll -command ".text yview"
pack .scroll -side right -fill y
pack .text -expand yes -fill both

.text tag configure bold -font {Courier 12 bold italic}
.text tag configure big -font {Courier 14 bold}
.text tag configure verybig -font {Helvetica 24 bold}
if {[winfo depth $w] > 1} {
.text tag configure color1 -background #a0b7ce
.text tag configure color2 -foreground red
.text tag configure raised -relief raised -borderwidth 1
.text tag configure sunken -relief sunken -borderwidth 1
} else {
.text tag configure color1 -background black -foreground white
.text tag configure color2 -background black -foreground white
.text tag configure raised -background white -relief raised \
-borderwidth 1
.text tag configure sunken -background white -relief sunken \
-borderwidth 1
}
.text tag configure bgstipple -background black -borderwidth 0 \
-bgstipple gray12
.text tag configure fgstipple -fgstipple gray50
.text tag configure underline -underline on
.text tag configure overstrike -overstrike on
.text tag configure right -justify right
.text tag configure center -justify center
.text tag configure super -offset 4p -font {Courier 10}
.text tag configure sub -offset -2p -font {Courier 10}
.text tag configure margins -lmargin1 12m -lmargin2 6m -rmargin 10m
.text tag configure spacing -spacing1 10p -spacing2 2p \
-lmargin1 12m -lmargin2 6m -rmargin 10m

.text insert end {Vediamo un po' le diverse tipologie di stile! Per modificare il
\ tipo di stile si usano dei comandi detti }
.text insert end tags bold
.text insert end {. Questi permettono di fare varie tipo }
.text insert end "\n1. Font." grandi
.text insert end " Puoi scegliere ogni tipo di Font, "
.text insert end grande verybig
.text insert end " o "
.text insert end "piccolo.\n"
.text insert end "\n2. Colore." big
.text insert end " Puoi cambiare lo "
.text insert end sfondo color1
.text insert end " o il "
.text insert end testo color2
.text insert end "\n o "
.text insert end entrambi {color1 color2}
.text insert end ".\n"
.text insert end "\n3. Effetti." big
.text insert end " Tu poi creare uno "
.text insert end "sfondo puntinato" bgstipple
.text insert end " o "
.text insert end cancellare fgstipple
.text insert end {
Potrei aggiungervi MICA MALE!.
}
.text insert end "\n4. Sottolineato." big
.text insert end " Puoi "
.text insert end sottolineare underline
.text insert end " iltesto.\n"
.text insert end "\n5. Barrare." big
.text insert end " E puoi "
.text insert end barrare overstrike
.text insert end " tutto ciò che vuoi!\n"
.text insert end "\n6. Effetti 3d." big
.text insert end "effetto pulsante" raised
.text insert end " o "
.text insert end abbassato sunken
.text insert end ".\n"
.text insert end "\n7. Giustificazioni." big
.text insert end " Puoi mettere il testo:\n"
.text insert end "a sinistra\n"
.text insert end "a destra, o\n" right
.text insert end "centrato.\n" center
.text insert end "\n8. Apice o pedice." big
.text insert end " Ovviamente\n"
.text insert end "puoi fare 10"
.text insert end "n" super
.text insert end " or\n o 10"
.text insert end "n" sub
.text insert end ".\n"
.text insert end "\n9. Margini." big
.text insert end " Puoi inserire margini"
.text insert end "Come in questo esempio! Non penso sia facile da " margins
.text insert end "capire! Anzi mi sembra di usa semplicità assurda " margins
.text insert end "Cosa volete che vi dica. Non vi sembra fantastico?\n" margins
.text insert end "\n10. Spaziato." big
.text insert end " Puoi controllare lo spazio tra una linea ed un'altra\n"
.text insert end "attraverso i tag. \"Spacing1\" Cosa ve ne pare?\n "
.text insert end "Puoi anche lasciare righe vuote! " spacing
.text insert end "spaziando il testo. Aumento la distanza " spacing
.text insert end "tra una l'altra! " spacing

Semplice! E' solo questione di pratica.

Ora per parlare seriamente diciamo che si possono fare collegamenti anche attraverso i tags dei text.

"Un esempio?" Eccolo:

if {[winfo depth .] > 1} { 
set bold "-background #43ce80 -relief raised -borderwidth 1"
set normal "-background {} -relief flat"
} else {
set bold "-foreground white -background black"
set normal "-foreground {} -background {}"
}

.text insert 0.0 {\
Tanto per darvi un altro esempio della potenza di questo linguaggio, vi dico che\
ora vi parlo di link. I collegamenti all'interno dei text.\
}
.text insert end \
{1. Se clicchi qui ti collegi ad una finestra di nome pagina.tcl. Questa deve \
eseistere! Nel nostro caso non esiste quindi non ti colleghi!.} d1
.text insert end \n\n
.text insert end \
{2. Anche questo link manca. Quindi è inutile che clicchi!.} d2
.text insert end \n\n
.text insert end \
{3. Ma dove pensi di andare!?.} d3
.text insert end \n\n
.text insert end \
{4. Non prendiamoci in giro!.} d4
.text insert end \n\n
.text insert end \
{5. Sembri un bambino che vuole cliccare senza sapere dove andare!} d5
.text insert end \n\n
.text insert end \
{6.Basta mi sono stufato!} d6



foreach tag {d1 d2 d3 d4 d5 d6} {
.text tag bind $tag <Any-Enter> ".text tag configure $tag $bold"
.text tag bind $tag <Any-Leave> ".text tag configure $tag $normal"
}
.text tag bind d1 <1> {source [file join pagina.tcl]}
.text tag bind d2 <1> {source [file join pagina.tcl]}
.text tag bind d3 <1> {source [file join pagina.tcl]}
.text tag bind d4 <1> {source [file join pagina.tcl]}
.text tag bind d5 <1> {source [file join pagina.tcl]}
.text tag bind d6 <1> {source [file join pagina.tcl]}
.text mark set insert 0.0
.text configure -state disabled

Spiegazione: Nulla qui ho usato tutto e di più! Ho usato? No! non è vero questo esempio lo copiato in pieno! Diciamoci la verità.

Comunque è semplice da ricreare.

7. Disegni

Prima di tutto si deve creare una zona adibita ad disegno:

canvas .c -scrollregion {0c 0c 30c 24c} -width 15c -height 10c \ 
-relief sunken -borderwidth 2 \
-xscrollcommand ".hscroll set" \
-yscrollcommand ".vscroll set"
scrollbar .vscroll -command ".c yview"
scrollbar .hscroll -orient horiz -command ".c xview"

Dopo di che si disegna:

.c create rect 0c 0c 30c 24c -width 2 
.c create line 0c 8c 30c 8c -width 2

Linee e rettangoli. 0c 0c ... sono le coordinate rispetto all'area di disegno.

Più complicato:

.c create line 12c 6c 13.5c 4.5c 16.5c 7.5c 18c 6c \ 
16.5c 4.5c 13.5c 7.5c 12c 6c -smooth on -width 3m -cap round \
-stipple @[file join lettera.bmp] \
-fill red -tags item

Abbiamo quindi scoperto che grazie al comando create si può disegnare in una zona adibita al disegno.

Esistono poi altre dozzine di comandi. Quindi vi allego i file di spiegazione. Li ho scaricati da internet. E' inutile che lo copi come un cretino. Poi non è nel mio spirito.
Quindi leggetevelo.

Importante da dire è che si hanno dei disegni già impostati che si possono richiamare con il comando bitmapRow.

bitmapRow .0 error gray12 gray25 gray50 gray75 
bitmapRow .1 hourglass info question questhead warning
pack .0 .1

Le immagini che qui vi ho già messo (perché se ne possono mettere più di una per riga e comando) sono:

 --------------------------------------- 
| error | simbolo errore |
| gray12 | grigio |
| gray25 | grigio più scuro |
| gray50 | grigio ancora più scuro |
| gray75 | grigio scurissimo |
| hourglass | clessidra |
| info | I |
| question | ? |
| questhead | Una faccina |
| warning | ! |
---------------------------------------


8. Messaggi vari

Sapete già come aprire una finestrella. Ora vi spiego che esistono altri tipi di finestra
ECCO A VOI:

 --------- 
| ICONE |
---------
| error |
| info |
| question|
| warning |
---------

E poi i pulsanti:

 ------------------ 
| PULSANTI |
------------------
| abortretryignore |
| ok |
| okcancel |
| retrycancel |
| yesno |
| yesnocancel |
------------------

Il modo per utilizzarli sono:

tk_messageBox -icon info -type ok -message "MESSAGGIO!" 
return 1

Qui potete vedere che ho messo info come icona e ok come pulsante (type).

Direi che anche questa guida è finita!

Dato che l'importante è imparare, nella sezione Programmare abbiamo inserito la guida totale che ho trovato or ora! L'unico inconveniente è che è in inglese.. Per questo se sapete l'inglese e volete migliorare il vostro livello leggetevela bene, cmq per iniziare leggete anche questa guida in italiano, male non fa. Purtroppo è per Linux, ma se avete un buon visualizzatore di file PS siete a posto! (visualizzatore di PS disponibile nella sezione Programmi/Varie del Tank Commandos http://go.to/tankcommandos )

----------------------------FINE-------------------------

Per suggerimenti e C.

bakunin@meganmail.com

← previous
next →
loading
sending ...
New to Neperos ? Sign Up for free
download Neperos App from Google Play
install Neperos as PWA

Let's discover also

Recent Articles

Recent Comments

Neperos cookies
This website uses cookies to store your preferences and improve the service. Cookies authorization will allow me and / or my partners to process personal data such as browsing behaviour.

By pressing OK you agree to the Terms of Service and acknowledge the Privacy Policy

By pressing REJECT you will be able to continue to use Neperos (like read articles or write comments) but some important cookies will not be set. This may affect certain features and functions of the platform.
OK
REJECT