⇨ MENU ⇨ MODULES ⇨ tkinter ⇨ controles standards ⇨ Canvas
tkinter.Canvas ( ).select_to ( )
⇨ MENU ⇨ MODULES ⇨ tkinter ⇨ controles standards ⇨ Canvas
tkinter.Canvas ( ).select_to ( )
DESCRIPTION.
Définie l'index du caractère terminant une sélection dans un élément texte d'une zone de dessin.
SYNTAXE.
CAN_Toile.select_to ( element , index )
CAN_Toile ⇨ instance quelconque de tkinter.Canvas ( ) [ OBLIGATOIRE ]
.select_to ( ) ⇨ 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.
Rappel : le premier caractère d'une chaine de caractères est à l'index 0.
EXEMPLE.
import tkinter
def FNC_Selection ( event ) :
if TKV_Texte.get ( ) == "" : return
CAN_Carte.select_from ( TKV_Texte.get ( ) , SCA_Debut.get ( ) )
CAN_Carte.select_to ( TKV_Texte.get ( ) , SCA_Fin.get ( ) - 1 )
def FNC_Texte ( event ) :
CAN_Carte.focus_set ( )
TKV_Texte.set ( CAN_Carte.find_withtag ( "current" ) [ 0 ] )
ktexte = CAN_Carte.itemcget ( TKV_Texte.get ( ) , "text")
SCA_Debut [ "to" ] = len ( ktexte ) - 1
SCA_Fin [ "to" ] = len ( ktexte )
LAB_Ligne [ "text" ] = ktexte
CAN_Carte.focus ( TKV_Texte.get ( ) )
FNC_Selection ( None )
LAB_Element [ "text" ] = f"Sélection sur l'élément : { CAN_Carte.select_item ( ) }"
TKI_Principal = tkinter.Tk ( )
TKV_Texte = tkinter.StringVar ( )
BUT_Quitter = tkinter.Button ( TKI_Principal , text = "Quitter" , command = TKI_Principal.destroy )
SCA_Debut = tkinter.Scale ( TKI_Principal , to = 1 , orient = "horizontal" , command = FNC_Selection )
SCA_Fin = tkinter.Scale ( TKI_Principal , from_ = 1 , to = 2 , orient = "horizontal" , command = FNC_Selection )
LAB_Element = tkinter.Label ( TKI_Principal , text = "Sélection sur l'élément : ?" , relief = "ridge" , bd = 3 )
LAB_Ligne = tkinter.Label ( TKI_Principal , text = "Cliquez sur une ligne." , relief = "ridge" , bd = 3 , height = 2 )
CAN_Carte = tkinter.Canvas ( TKI_Principal , bg = "white" , width = 200 , height = 100 )
CAN_Carte.create_text ( 100 , 30 , text = "Mon Python à moi." , fill = "orange" )
CAN_Carte.create_text ( 100 , 50 , text = "Un Python pour tous." )
CAN_Carte.create_text ( 195 , 95 , text = "Origine NERD" , anchor = "se" , fill = "blue" )
CAN_Carte.grid ( row = 0 , column = 0 , columnspan = 2 , sticky = "nesw" )
LAB_Element.grid ( row = 1 , column = 0 , columnspan = 2 , sticky = "nesw" )
LAB_Ligne.grid ( row = 2 , column = 0 , columnspan = 2 , sticky = "nesw" )
tkinter.Label ( TKI_Principal , text = "Début : " , justify = "right" ).grid ( row = 3 , column = 0 , sticky = "se" )
SCA_Debut.grid ( row = 3 , column = 1 , sticky = "nesw" )
tkinter.Label ( TKI_Principal , text = "Fin : " , justify = "right" ).grid ( row = 4 , column = 0 , sticky = "se" )
SCA_Fin.grid ( row = 4 , column = 1 , sticky = "nesw" )
BUT_Quitter.grid ( row = 5 , column = 0, columnspan = 2 , sticky = "nesw" )
CAN_Carte.tag_bind ( "current" , "<ButtonRelease-1>" , FNC_Texte )
TKI_Principal.mainloop ( )
Votre aide est précieuse pour améliorer ce site, alors n'hésitez pas à faire part de