⇨ MENU ⇨ MODULES ⇨ tkinter ⇨ controles standards ⇨ Canvas
tkinter.Canvas ( ).select_adjust ( )
⇨ MENU ⇨ MODULES ⇨ tkinter ⇨ controles standards ⇨ Canvas
tkinter.Canvas ( ).select_adjust ( )
DESCRIPTION.
Ajuste la sélection dans un élément texte d'une zone de dessin jusqu'à un index précis.
SYNTAXE.
CAN_Toile.select_adjust ( element , index )
CAN_Toile ⇨ instance quelconque de tkinter.Canvas ( ) [ OBLIGATOIRE ]
.select_adjust ( ) ⇨ appel de la méthode [ OBLIGATOIRE ]
element ⇨ élément texte de référence [ OBLIGATOIRE ]
index ⇨ index de référence [ OBLIGATOIRE ]
REMARQUES.
element peut être un int ( ) représentant l'identifiant numérique, un str ( ) indiquant un tag ou la variable, désignant un élément texte, c'est à dire créé uniquement avec la méthode tkinter.Canvas ( ).create_text ( ).
index est un int ( ) ou l'un des str ( ) d'index spécifiques indiquant jusqu'à quel index il faut étendre la sélection.
index peut être la représentation d'un index spécifique. Les index spécifiques sont des constantes exprimées sous la forme d'un str ( ) et peuvent prendre l'une des valeurs suivantes :
- "insert", correspond à l'index du curseur d'insertion ;
- "sel.first", correspond à l'index du premier caractère de la sélection dans le texte ;
- "sel.last", correspond à l'index du dernier caractère de la sélection dans le texte ;
- "end", correspond à l'index du dernier caractère de la sélection dans le texte ;
- "@x,y", correspond à l'index du caractère à la position du pixel aux coordonnées (x,y) (voir tkinter.Canvas ( ).index ( )).
Si l'élément texte element ne possède pas de sélection au moment de l'appel, les index spécifiques "sel.first", "sel.last", feront lever une exception.
Donc, quand elle est présente dans element, la sélection va de "sel.first" à "sel.first". Si :
- index est inférieur "sel.first" alors "sel.first" devient index et "sel.last", reste inchangé ;
- index est supérieur "sel.last" alors "sel.last" devient index et "sel.first", reste inchangé.
Rappel : le premier caractère d'une chaine de caractères est à l'index 0.
EXEMPLE.
import tkinter
def FNC_Ajuster ( event ) :
FNC_Annuler ( )
CAN_Carte.select_from ( 1 , SCA_Cursor.get ( ) )
CAN_Carte.select_adjust ( 1 , SCA_Pivot.get ( ) )
def FNC_Annuler ( ) :
CAN_Carte.select_clear ( )
def FNC_Curseur ( event ) :
CAN_Carte.icursor ( 1 , SCA_Cursor.get ( ) )
def FNC_Inserer ( ) :
if TKV_Texte.get ( ) == "" : return
CAN_Carte.insert ( 1 , "insert" , TKV_Texte.get ( ) )
FNC_Curseur ( None )
FNC_Longeur ( )
def FNC_Longeur ( ) :
ktexte = CAN_Carte.itemcget ( 1 , "text" )
SCA_Cursor [ "to" ] = len ( ktexte )
SCA_Pivot [ "to" ] = len ( ktexte )
def FNC_Supprimer ( ) :
if CAN_Carte.select_item ( ) :
CAN_Carte.dchars ( 1 , "sel.first" , "sel.last" )
FNC_Longeur ( )
TKI_Principal = tkinter.Tk ( )
TKV_Texte = tkinter.StringVar ( )
BUT_Quitter = tkinter.Button ( TKI_Principal , text = "Quitter" , command = TKI_Principal.destroy )
BUT_Ajuster = tkinter.Button ( TKI_Principal , text = "Ajuster" , command = lambda : FNC_Ajuster ( None ) )
BUT_Inserer = tkinter.Button ( TKI_Principal , text = "Insérer" , command = FNC_Inserer )
BUT_Effacer = tkinter.Button ( TKI_Principal , text = "Annuler la sélection" , command = FNC_Annuler )
BUT_Supprimer = tkinter.Button ( TKI_Principal , text = "Supprimer la sélection" , command = FNC_Supprimer )
ENT_Texte = tkinter.Entry ( TKI_Principal , textvariable = TKV_Texte , width = 15 )
SCA_Cursor = tkinter.Scale ( TKI_Principal , orient = "horizontal" , length = 120 , command = FNC_Curseur )
SCA_Pivot = tkinter.Scale ( TKI_Principal , orient = "horizontal" , length = 120 , command = FNC_Ajuster )
CAN_Carte = tkinter.Canvas ( TKI_Principal , bg = "white" , height = 100 , width = 200 )
CAN_Carte.create_text ( 100 , 50 , text = "Mon Python à moi.\nUn Python pour tous.\nOrigine NERD" , justify = "center" )
ENT_Texte.bind ( "<Leave>" , lambda event : CAN_Carte.focus_set ( ) )
CAN_Carte.grid ( row = 0 , column = 0 , columnspan = 2 , sticky = "nesw" )
tkinter.Label ( TKI_Principal , text = "Curseur :" , justify = "right" ).grid ( row = 1 , column = 0 , sticky = "se" )
SCA_Cursor.grid ( row = 1 , column = 1 , sticky = "nesw" )
BUT_Ajuster.grid ( row = 2 , column = 0 , sticky = "nesw" )
SCA_Pivot.grid ( row = 2 , column = 1 , sticky = "nesw" )
BUT_Inserer.grid ( row = 3 , column = 0 , sticky = "nesw" )
ENT_Texte.grid ( row = 3 , column = 1 , sticky = "nesw" )
BUT_Effacer.grid ( row = 4 , column = 0 , columnspan = 2 , sticky = "nesw" )
BUT_Supprimer.grid ( row = 5 , column = 0 , columnspan = 2 , sticky = "nesw" )
BUT_Quitter.grid ( row = 6 , column = 0 , columnspan = 2 , sticky = "nesw" )
CAN_Carte.focus_set ( )
CAN_Carte.focus ( 1 )
FNC_Longeur ( )
TKI_Principal.mainloop ( )
Votre aide est précieuse pour améliorer ce site, alors n'hésitez pas à faire part de