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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
| # Name: enregistrer/mémoriser/...et retrouver TAGs tkinter
# Author: chris33
#
# Created: 06/01/2025
# Copyright: (c) chris 2025
# Licence: <your licence>
#-------------------------------------------------------------------------------
import tkinter
from tkhtmlview import HTMLScrolledText
class Application(tkinter.Tk):
def __init__(self):
tkinter.Tk.__init__(self)
self.creer_widgets()
def creer_widgets(self):
# self.frame1
self.frame1 = tkinter.Frame(self, width=300, height=100, bg='grey')
self.frame1.pack(fill=tkinter.X)
# self.html_ScrollText1
self.html_ScrollText1 = HTMLScrolledText(self.frame1, state=tkinter.NORMAL, html='en attente')
self.html_ScrollText1.pack(expand=1,fill='both')
self.charger_fichier()
# self.frame2
self.frame2 = tkinter.Frame(self, width=100, height=50, bg='red') # -> pour OUTILS MISE EN FORME TEXTE
self.frame2.pack(fill=tkinter.X)
self.label2= tkinter.Label(self.frame1,text="OUTILS MISE EN FORME TEXTE")
self.label2.pack(fill=tkinter.X)
self.button_2=tkinter.Button(self.frame2,text='jaune',command=self.jaune,background='yellow')
self.button_2.pack()
def jaune(self):
print('______ jaune() ____________')
### ACTIVER LIGNES SUIVANTES POUR DEFINIR QUELQUES TAGS
#self.html_ScrollText1.tag_add('jaune','1.5','1.11')
#self.html_ScrollText1.tag_add('jaune','2.12','2.25')
#self.html_ScrollText1.tag_add('jaune','2.20','2.30')
#self.html_ScrollText1.tag_add('jaune','2.35','2.45')
#self.html_ScrollText1.tag_add('jaune','4.22','4.35')
#self.html_ScrollText1.tag_config("jaune",background="yellow",foreground="white")
# -------------------------- tag_ranges ----------------------------------
# self.html_ScrollText1.tag_ranges : Renvoie une liste décrivant toutes les plages de texte qui ont été balisées avec tagName.
# Les deux premiers éléments de la liste décrivent la première plage balisée dans le texte,
# les deux éléments suivants décrivent la deuxième plage, et ainsi de suite. Le premier élément de chaque paire contient
# l'index du premier caractère de la plage, et le deuxième élément de la paire contient l'index du caractère juste après
# le dernier de la plage. S'il n'y a aucun caractère balisé avec tag, une chaîne vide est renvoyée.
liste=self.html_ScrollText1.tag_ranges('jaune')
print('liste : ',liste)
for tag in liste:
print('tag',tag)
# REACTIVER LA LIGNE SUIVANTE POUR ENREGISTRER TAGGs
#self.enregistrer_list_tags(liste)
liste_tag=self.charger_list_tags()
cpteur=0
for tag in liste_tag:
try:
self.html_ScrollText1.tag_add('jaune2',liste_tag[cpteur],liste_tag[cpteur+1])
cpteur+=2
except:
pass
self.html_ScrollText1.tag_config("jaune2",background="green",foreground="white")
def charger_fichier(self):
"""
lire le contenu de mon fichier text et l'afficher dans html_ScrollText'
"""
chaineHtml=''
# Lecture fichier
with open(r"C:\Users\chris\Documents\mesScryptPython\fichier\fichier_aide.txt",'r') as fichier:
for ligne in fichier:
chaineHtml=chaineHtml+ligne+"<br>"
print('chaineHtml : ',chaineHtml)
# Affichage
self.html_ScrollText1.set_html(chaineHtml)
def enregistrer_list_tags(self,liste):
"""
La (et plus tard LES LISTES) tags sont enregistrés dans un fichier distint
"""
chaine=''
with open(r"C:\Users\chris\Documents\mesScryptPython\fichier\tags.txt",'w') as fichier:
for tag in liste:
chaine=chaine+str(tag)+';'
print('chaine : ',chaine[:-1])
fichier.write(chaine[:-1])
def charger_list_tags(self):
"""
exemple de liste renvoyée (pour un UNIQUE tag) : liste indice : ['1.5', '1.11', '2.12', '2.30', '2.35', '2.45', '4.22', '4.35']
"""
print("-------- charger_list_tags() ------------")
liste=[]
with open(r"C:\Users\chris\Documents\mesScryptPython\fichier\tags.txt",'r') as fichier:
for ligne in fichier :
ligne2=ligne.split((';'))
for indice in ligne2:
print('indice : ',indice)
liste.append(indice)
print('liste indice : ',liste)
return liste
if __name__ == "__main__":
app = Application()
app.title("Mémoriser tags)")
app.geometry('1900x1550')
#app.configure(background="blue")
app.mainloop() |
Partager