Bonjour,

Je cherche à faire un arbre d'un texte bien segmenté par des sections, sous sections, contenu dans un fichier.
J'arrive au code suivant:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
import tkinter
from tkinter import *
from tkinter import ttk
 
class Arborescence():
 
    def __init__(self, tree):
#Appel des variables
        self.t = tree
 
    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=''
        noeud_section=''
        noeud_subsection=''
        noeud_subsubsection=''
        noeud_paragraph=''
        noeud_subparagraph=''
        noeud_input=''        
        self.action={}
        compteur=0
        decoupe=texte.split('\n')
        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)
                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)
                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)
                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)
                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)
                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)
                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])
 
 
 
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}
Voilà
"""
 
p=tkinter.Tk()
tree = ttk.Treeview(p)
arb=Arborescence(tree)
arb.arbre(letexte)
tree.pack(side=BOTTOM,fill=X)
#tree.see(tree.get_children())
tree.bind('<ButtonRelease-1>',arb.click_arbre)
p.mainloop()
Je n'arrive pas à configurer l'arbre et notamment je n'arrive pas à le faire <<déplier>> dès l'ouverture de l'instance; j'ai essayé sans succès avec .see.

je n'arrive pas à jouer sur la largeur.

Y-a-t-il aussi la possibilité de le faire aller à la ligne (comme par exemple WRAP dans un widgetText) si la ligne dépasse la largeur?

Merci pour vos réponses.

Gabriel