1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| #-------------------------------------------------------------------------------
# Name: HTMLScrolledText
# Purpose: Sélectionne des mots dans le widget HTMLScrolledText de tkhtmlview
# Author: Chris33#
# Created: 26/11/2024
#-------------------------------------------------------------------------------
import tkinter
from tkhtmlview import HTMLLabel, HTMLScrolledText
class Application(tkinter.Tk):
def __init__(self):
tkinter.Tk.__init__(self)
self.creer_widgets()
def creer_widgets(self):
# widget html_ScrollText1
self.html_ScrollText1= HTMLScrolledText(self)
self.html_ScrollText1.bind("<Double-Button>", self.doubleClick)
self.html_ScrollText1.pack()
self.html_ScrollText1.set_html("""Many of the widget commands for texts take one or more indices as arguments. An index is a string used to indicate a particular place within a text, such as a place to insert characters or one endpoint of a range of characters to delete. Indices have the syntax
base modifier modifier modifier ...
Where base gives a starting point and the modifiers adjust the index from the starting point (e.g. move forward or backward one character). Every index must contain a base, but the modifiers are optional.
The base for an index must have one of the following forms:
line.char
Indicates char'th character on line line. Lines are numbered from 1 for consistency with other UNIX programs that use this numbering scheme. Within a line, characters are numbered from 0. If char is end then it refers to the newline character that ends the line.
<br>wordstart<br>
Adjust the index to refer to the first character of the word containing the current index. A word consists of any number of adjacent characters that are letters, digits, or underscores, or a single character that is not one of these.
<br>wordend<br>
Adjust the index to refer to the character just after the last one of the word containing the current index. If the current index refers to the last character of the text then it is not modified.
If more than one modifier is present then they are applied in left-to-right order. For example, the index ``end - 1 chars'' refers to the next-to-last character in the text and ``insert wordstart - 1 c'' refers to the character just before the first one in the word containing the insertion cursor. Modifiers are applied one by one in this left to right order, and after each step the resulting index is constrained to be a valid index in the text widget. So, for example, the index ``1.0 -1c +1c'' refers to the index ``2.0''""")
def doubleClick(self,event):
print("---------- doubleClick ________________")
posClick=self.html_ScrollText1.index(tkinter.CURRENT) # debut (ligne,col)
# Utilisation des indice 'wordstart' & 'wordend' pour chercher les positions de début et fin
debut=self.html_ScrollText1.index("{} wordstart".format(posClick))
fin=self.html_ScrollText1.index("{} wordend".format(posClick))
mot_selectionne=self.html_ScrollText1.get(debut, fin)
# Mise en forme de la sélection
self.html_ScrollText1.tag_delete("ma_selection")
self.html_ScrollText1.tag_add("ma_selection",debut,fin)
self.html_ScrollText1.tag_config("ma_selection",background="black",foreground="white")
if __name__ == "__main__":
app = Application()
app.title("Sélectionner texte dans HTMLLabel ou html_ScrollText1")
app.mainloop() |
Partager