IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Tkinter Python Discussion :

la configuration d'un treeview


Sujet :

Tkinter Python

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Profil pro
    Inscrit en
    Novembre 2010
    Messages
    147
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2010
    Messages : 147
    Par défaut la configuration d'un treeview
    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

  2. #2
    Membre confirmé
    Profil pro
    Inscrit en
    Novembre 2010
    Messages
    147
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2010
    Messages : 147
    Par défaut
    Bonjour,

    Et bien je me réponds... après lecture et relecture de la doc sur ttk, j'ai fini par comprendre que '#0' désigne la colonne par défaut de l'arbre, puis qu'il y a la commande open=True/False pour déplier ou non l'arbre.
    Le code devient:
    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
    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()
    A bientôt
    Gabriel

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. xml, treeview, configuration
    Par Dokare dans le forum Général Dotnet
    Réponses: 1
    Dernier message: 08/01/2014, 17h36
  2. TreeView et menu contextuel...
    Par agh dans le forum Composants VCL
    Réponses: 6
    Dernier message: 06/04/2009, 12h23
  3. Configurer OpenGL/Glut avec C++Bluider
    Par MiGoN dans le forum OpenGL
    Réponses: 2
    Dernier message: 13/09/2002, 23h18
  4. [Pointer]Treeview.Data
    Par rbag dans le forum Composants VCL
    Réponses: 7
    Dernier message: 31/08/2002, 01h44
  5. BDE : Configurer automatiquement le NETDIR
    Par Harry dans le forum Paradox
    Réponses: 10
    Dernier message: 29/07/2002, 11h33

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo