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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
| import tkinter
from tkinter import *
from tkinter import ttk
import re
class Arborescence():
def __init__(self, tree,stree,text,stext):
#Appel des variables
self.t = tree
self.s2=stree
self.texte1=text
self.s1=stext
def arbre(self,texte):
num_s=0
num_ss=0
num_sss=0
num_par=0
num_subpar=0
num_benu=0
num_enui=0
num_enuii=0
num_input=0
noeud_en_cours=''
decoupe=texte.split('\n')
noeud_section=''
noeud_subsection=''
noeud_subsubsection=''
noeud_paragraph=''
noeud_subparagraph=''
noeud_input=''
self.action={}
compteur=0
for ligne in decoupe:
if r'\section' in ligne:
titre=ligne.replace(r'\section{','')
titre=titre.replace(r'\section*{','')
num_s=num_s+1
num_ss=0
titre=titre[:-1]
noeud_section='s'+str(num_s)
self.t.insert('', 'end', noeud_section, text=titre,open=True)
self.action[noeud_section]=[0,compteur]
noeud_en_cours=noeud_section
if r'\subsection' in ligne:
titre=ligne.replace(r'\subsection{','')
titre=titre.replace(r'\subsection*{','')
num_ss=num_ss+1
num_sss=0
titre=titre[:-1]
noeud_subsection=noeud_section+'ss'+str(num_ss)
self.t.insert(noeud_section, 'end', noeud_subsection, text=titre,open=True)
self.action[noeud_subsection]=[0,compteur]
noeud_en_cours=noeud_subsection
if r'\subsubsection' in ligne:
titre=ligne.replace(r'\subsubsection{','')
titre=titre.replace(r'\subsubsection*{','')
num_sss=num_sss+1
num_par=0
titre=titre[:-1]
noeud_subsubsection=noeud_subsection+'sss'+str(num_sss)
self.t.insert(noeud_subsection, 'end', noeud_subsubsection, text=titre,open=True)
self.action[noeud_subsubsection]=[0,compteur]
noeud_en_cours=noeud_subsubsection
if r'\paragraph' in ligne:
titre=ligne.replace(r'\paragraph{','')
titre=titre.replace(r'\paragraph*{','')
num_par=num_par+1
num_subpar=0
titre=titre[:-1]
noeud_paragraph=noeud_subsubsection+'p'+str(num_par)
self.t.insert(noeud_subsubsection, 'end', noeud_paragraph, text=titre,open=True)
self.action[noeud_paragraph]=[0,compteur]
noeud_en_cours=noeud_paragraph
if r'\subparagraph' in ligne:
titre=ligne.replace(r'\subparagraph{','')
titre=titre.replace(r'\subparagraph*{','')
num_subpar=num_subpar+1
titre=titre[:-1]
noeud_subparagraph=noeud_paragraph+'sp'+str(num_subpar)
self.t.insert(noeud_paragraph, 'end', noeud_subparagraph, text=titre,open=True)
self.action[noeud_subparagraph]=[0,compteur]
noeud_en_cours=noeud_subparagraph
if r'\input' in ligne:
titre=ligne.replace(r'\input{','')
titre=titre[:-1]
noeud_input=noeud_en_cours+titre
self.t.insert(noeud_en_cours, 'end', noeud_input, text=titre,open=True)
self.action[noeud_input]=[1,titre]
compteur=compteur+1
def click_arbre(self,event=None):
c=self.t.focus()
if self.action[c][0]==0:
print('Aller à la ligne : ',self.action[c][1])
else:
print('Ouvrir le fichier : ',self.action[c][1])
def arbre_masque(self,event=None):
self.t.grid_remove()
self.s2.grid_remove()
def arbre_affiche(self,event=None):
self.t.grid()
self.s2.grid()
letexte=""" charabia
\subsection{En intro}
\section{Section 1}
du texte
\subsection{Sous section 1}
encore du texte
avec un saut le ligne pour voir
\begin{enumerate}
\item
item1
\begin{enumerate}
\item
item1a
\item
item1b
\end{enumerate}
\item
item2
\end{enumerate}
\subsection{Sous section 2}
du texte
\subsubsection{Sous sous section 1}
du texte
\subsubsection{Sous sous section 2}
du texte
\subsection{Sous section 3}
du texte
\section{Section 2}
du texte
\subsection{Sous section 1}
DU texte
\input{nom du fichier très très très très très long à cause du chemin}
Voilà
"""
p=tkinter.Tk()
f = tkinter.Frame(p, bg='gray', bd=1)
texte1 = tkinter.Text(f, wrap=tkinter.WORD)
s1 = tkinter.Scrollbar(f, orient=tkinter.VERTICAL)
s1.config(command=texte1.yview)
texte1.config(yscrollcommand=s1.set,font=('courier',11),height=42,width=90)
texte1.insert(tkinter.END,letexte)
tree = ttk.Treeview(f)
s2 = tkinter.Scrollbar(f, orient=tkinter.VERTICAL)
s2.config(command=tree.yview)
tree.config(yscrollcommand=s2.set)
arb=Arborescence(tree,s2,texte1,s1)
arb.arbre(letexte)
tree.column('#0',width=400)
tree.grid(row=0,column=0,sticky=tkinter.S+tkinter.N)
s2.grid(row=0,column=1, sticky=tkinter.S+tkinter.N)
texte1.grid(row=0,column=2,padx=10, sticky=tkinter.S+tkinter.N)
s1.grid(row=0,column=3, sticky=tkinter.S+tkinter.N)
tree.bind('<ButtonRelease-1>',arb.click_arbre)
p.bind('<Control-j>',arb.arbre_masque)
p.bind('<Control-J>',arb.arbre_affiche)
p.wm_state(newstate="zoomed")
f.pack(fill=X)
p.mainloop() |
Partager