Pb : barre de scroll python tkinter pour un tableau

TimPython -  
Phil_1857 Messages postés 1883 Date d'inscription   Statut Membre Dernière intervention   -
Bonjour à tous, j'avais fait un tableau python qui fonctionnait très bien, sauf qu'ensuite j'ai voulu ajouter une barre de scroll (de défilement). Et maintenant, je n'arrives plus du tout à corriger les erreurs... Pouvez-vous m'aider s'il vous plaît.

Voci mon code :


from tkinter import *

fen = Tk()
fen.geometry('1500x800')
fen.grid()

tableau1 = Frame(fen, width=1100, height=700,)
tableau1.config(background="#45B39D")
tableau1.grid_propagate(0)

data = [["NOM","Prénom","DS1","TP2","IE4"],["Coef","",4,0.5,1],["Gérard","Gérard",20,14,3],["Hardelepique","Helmut",10,6,"abs"],["NOM","Prénom","DS1","TP2","IE4"],["Coef","",4,0.5,1],["Gérard","Gérard",20,14,3],["Hardelepique","Helmut",10,6,"abs"],["NOM","Gérard","DS1","TP2","IE4"],["Coef","",4,0.5,1],["Gérard","Gérard",20,14,3],["Hardelepique","Helmut",10,6,"abs"],["NOM","Prénom","DS1","TP2","IE4"],["Coef","",4,0.5,1],["Gérard","Bruno",20,14,3],["Hardelepique","Helmut",10,6,"abs"],["NOM","Prénom","DS1","TP2","IE4"],["Coef","",4,0.5,1],["Gérard","Bruno",20,14,3],["Hardelepique","Helmut",10,6,"abs"],["NOM","Prénom","DS1","TP2","IE4"],["Coef","",4,0.5,1],["Gérard","Bruno",20,14,3],["Hardelepique","Helmut",10,6,"abs"],["NOM","Prénom","DS1","TP2","IE4"],["Coef","",4,0.5,1],["Gérard","Bruno",20,14,3],["Hardelepique","Helmut",10,6,"abs"],["NOM","Prénom","DS1","TP2","IE4"],["Coef","",4,0.5,1],["Gérard","Bruno",20,14,3],["Hardelepique","Helmut",10,6,"abs"],["NOM","Prénom","DS1","TP2","IE4"],["Coef","",4,0.5,1],["Gérard","Bruno",20,14,3],["Hardelepique","Helmut",10,6,"abs"]]

class tableau:
def __init__(self, tableau1):

h = Scrollbar(tableau1, orient='horizontal')
h.pack(side=BOTTOM, fill=X)
v = Scrollbar(tableau1)
v.pack(side=RIGHT, fill=Y)

for i in range(total_rows):
for j in range(total_columns):

self.e = Entry(tableau1, width=10, fg='blue',font=('Arial',16), xscrollcommand=h.set, yscrollcommand=v.set)
self.e.grid(row=i, column=j)
self.e.insert(END, data[i][j])
self.e.config(state="disabled")

self.e.pack(side=TOP, fill=NONE)
h.config(command=e.xview)
v.config(command=e.yview)

total_rows = len(data)
total_columns = len(data[0])

tableau(tableau1)



Merci bcp d'avance

Configuration: Windows / Chrome 100.0.4896.127

7 réponses

  1. Phil_1857 Messages postés 1883 Date d'inscription   Statut Membre Dernière intervention   169
     
    Bonjour,

    Tu devrais afficher ton code avec les balises de code Python:
    mode d'emploi:
    https://codes-sources.commentcamarche.net/faq/11288-les-balises-de-code

    Visuellement, ça doit ressembler à ça:

    for k in range(10):
        print(k)
    


    Sinon, bizarre, pas de fen.mainloop() dans ton code

    tableau(tableau1) :


    en général, on crée une instance d'une classe comme ceci:

    mon_tableau = tableau(tableau1)


    On se demande si tu ne voulais pas plutôt créer une fonction tableau au lieu d'une classe ...

    Tu as quoi comme erreur ?
    0
  2. TimPython
     
    Oups désolé c'est la première fois que je poste ici :)

    from tkinter import *
    
    fen = Tk()
    fen.geometry('1500x800')
    fen.grid()
    
    tableau1 = Frame(fen, width=1100, height=700,)
    tableau1.config(background="#45B39D")
    tableau1.grid_propagate(0)
    
    data = [["NOM","Prénom","DS1","TP2","IE4"],["Coef","",4,0.5,1],["frfrf","Bruno",20,14,3],["Hardelepique","Helmut",10,6,"abs"],["NOM","Prénom","DS1","TP2","IE4"],["Coef","",4,0.5,1],["frfrfr","Bruno",20,14,3],["Hardelepique","Helmut",10,6,"abs"],["NOM","Prénom","DS1","TP2","IE4"],["Coef","",4,0.5,1],["frfrfr","Bruno",20,14,3],["Hardelepique","Helmut",10,6,"abs"],["NOM","Prénom","DS1","TP2","IE4"],["Coef","",4,0.5,1],["frfrfrf","Bruno",20,14,3],["Hardelepique","Helmut",10,6,"abs"],["NOM","Prénom","DS1","TP2","IE4"],["Coef","",4,0.5,1],["frfrfr","Bruno",20,14,3],["Hardelepique","Helmut",10,6,"abs"],["NOM","Prénom","DS1","TP2","IE4"],["Coef","",4,0.5,1],["frfrfr","Bruno",20,14,3],["Hardelepique","Helmut",10,6,"abs"],["NOM","Prénom","DS1","TP2","IE4"],["Coef","",4,0.5,1],["frfrfr","Bruno",20,14,3],["Hardelepique","Helmut",10,6,"abs"],["NOM","Prénom","DS1","TP2","IE4"],["Coef","",4,0.5,1],["frfrfr","Bruno",20,14,3],["Hardelepique","Helmut",10,6,"abs"],["NOM","Prénom","DS1","TP2","IE4"],["Coef","",4,0.5,1],["frfrfr","Bruno",20,14,3],["Hardelepique","Helmut",10,6,"abs"]]
    
    
    class tableau:
        def __init__(self, tableau1):
    
            h = Scrollbar(tableau1, orient='horizontal')
            h.pack(side=BOTTOM, fill=X)
            v = Scrollbar(tableau1)
            v.pack(side=RIGHT, fill=Y)
    
            for i in range(total_rows):
                for j in range(total_columns):
    
                    self.e = Entry(tableau1, width=10, fg='blue',font=('Arial',16), xscrollcommand=h.set, yscrollcommand=v.set)
                    self.e.grid(row=i, column=j)
                    self.e.insert(END, data[i][j])
                    self.e.config(state="disabled")
    
            self.e.pack(side=TOP, fill=NONE)
            h.config(command=e.xview)
            v.config(command=e.yview)
    
    
    total_rows = len(data)
    total_columns = len(data[0])
    
    tableau(tableau1)
    
    fen.mainloop()
    
    


    Et j'ai ces erreurs là :

    Traceback (most recent call last):
    File "C:\Users\timle\Documents\04-COURS\05-NSI\testtablo.py", line 38, in <module>
    tableau(tableau1)
    File "C:\Users\timle\Documents\04-COURS\05-NSI\testtablo.py", line 25, in __init__
    self.e = Entry(tableau1, width=10, fg='blue',font=('Arial',16), xscrollcommand=h.set, yscrollcommand=v.set)
    File "C:\Users\timle\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 3064, in __init__
    Widget.__init__(self, master, 'entry', cnf, kw)
    File "C:\Users\timle\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 2601, in __init__
    self.tk.call(
    _tkinter.TclError: unknown option "-yscrollcommand"

    Merci de m'aider !
    0
  3. Phil_1857 Messages postés 1883 Date d'inscription   Statut Membre Dernière intervention   169
     
    Ok, mais .... ma remarque sur l'utilisation de la classe, tu en penses quoi ?
    0
    1. TimPython
       
      J'avoue que je ne sais pas trop à quoi sert la class, j'ai récupéré un bout de code sur internet qui fasait un beau tableau, et il été dans une class
      0
  4. Phil_1857 Messages postés 1883 Date d'inscription   Statut Membre Dernière intervention   169
     
    bizarre tout ça, moi quand j'enlève tout ce qui concerne le scroll, j'ai une fenêtre vide ...

    je suis curieux de voir le code original, tu as le lien vers le site ?
    0
    1. TimPython
       
      Autant pour moi, j'avais oublié de copier le "tableau1.place(x=10, y=50)"

      Ce qui donne donc (sans le scroll) :

      fen.geometry('1500x800')
      fen.grid()
      
      tableau1 = Frame(fen, width=1100, height=700,)
      tableau1.config(background="#45B39D")
      tableau1.grid_propagate(0)
      
      
      
      tableau1.place(x=10, y=50)
      
      data = [["NOM","Prénom","DS1","TP2","IE4"],["Coef","",4,0.5,1],["frfrf","Bruno",20,14,3],["Hardelepique","Helmut",10,6,"abs"],["NOM","Prénom","DS1","TP2","IE4"],["Coef","",4,0.5,1],["frfrfr","Bruno",20,14,3],["Hardelepique","Helmut",10,6,"abs"],["NOM","Prénom","DS1","TP2","IE4"],["Coef","",4,0.5,1],["frfrfr","Bruno",20,14,3],["Hardelepique","Helmut",10,6,"abs"],["NOM","Prénom","DS1","TP2","IE4"],["Coef","",4,0.5,1],["frfrfrf","Bruno",20,14,3],["Hardelepique","Helmut",10,6,"abs"],["NOM","Prénom","DS1","TP2","IE4"],["Coef","",4,0.5,1],["frfrfr","Bruno",20,14,3],["Hardelepique","Helmut",10,6,"abs"],["NOM","Prénom","DS1","TP2","IE4"],["Coef","",4,0.5,1],["frfrfr","Bruno",20,14,3],["Hardelepique","Helmut",10,6,"abs"],["NOM","Prénom","DS1","TP2","IE4"],["Coef","",4,0.5,1],["frfrfr","Bruno",20,14,3],["Hardelepique","Helmut",10,6,"abs"],["NOM","Prénom","DS1","TP2","IE4"],["Coef","",4,0.5,1],["frfrfr","Bruno",20,14,3],["Hardelepique","Helmut",10,6,"abs"],["NOM","Prénom","DS1","TP2","IE4"],["Coef","",4,0.5,1],["frfrfr","Bruno",20,14,3],["Hardelepique","Helmut",10,6,"abs"]]
      
      class tableau:
          def __init__(self, tableau1):
      
              for i in range(total_rows):
                  for j in range(total_columns):
      
                      self.e = Entry(tableau1, width=10, fg='blue',font=('Arial',16))
                      self.e.grid(row=i, column=j)
                      self.e.insert(END, data[i][j])
                      self.e.config(state="disabled")
      
      
      
      total_rows = len(data)
      total_columns = len(data[0])
      
      tableau(tableau1)
      
      fen.mainloop()
      
      0
  5. Vous n’avez pas trouvé la réponse que vous recherchez ?

    Posez votre question
  6. TimPython
     
    Et ce qui donne avec le scroll qui fait des erreurs :

    from tkinter import *
    
    fen = Tk()
    fen.geometry('1500x800')
    fen.grid()
    
    tableau1 = Frame(fen, width=1100, height=700,)
    tableau1.config(background="#45B39D")
    tableau1.grid_propagate(0)
    
    tableau1.place(x=10, y=50)
    
    
    data = [["NOM","Prénom","DS1","TP2","IE4"],["Coef","",4,0.5,1],["frfrf","Bruno",20,14,3],["Hardelepique","Helmut",10,6,"abs"],["NOM","Prénom","DS1","TP2","IE4"],["Coef","",4,0.5,1],["frfrfr","Bruno",20,14,3],["Hardelepique","Helmut",10,6,"abs"],["NOM","Prénom","DS1","TP2","IE4"],["Coef","",4,0.5,1],["frfrfr","Bruno",20,14,3],["Hardelepique","Helmut",10,6,"abs"],["NOM","Prénom","DS1","TP2","IE4"],["Coef","",4,0.5,1],["frfrfrf","Bruno",20,14,3],["Hardelepique","Helmut",10,6,"abs"],["NOM","Prénom","DS1","TP2","IE4"],["Coef","",4,0.5,1],["frfrfr","Bruno",20,14,3],["Hardelepique","Helmut",10,6,"abs"],["NOM","Prénom","DS1","TP2","IE4"],["Coef","",4,0.5,1],["frfrfr","Bruno",20,14,3],["Hardelepique","Helmut",10,6,"abs"],["NOM","Prénom","DS1","TP2","IE4"],["Coef","",4,0.5,1],["frfrfr","Bruno",20,14,3],["Hardelepique","Helmut",10,6,"abs"],["NOM","Prénom","DS1","TP2","IE4"],["Coef","",4,0.5,1],["frfrfr","Bruno",20,14,3],["Hardelepique","Helmut",10,6,"abs"],["NOM","Prénom","DS1","TP2","IE4"],["Coef","",4,0.5,1],["frfrfr","Bruno",20,14,3],["Hardelepique","Helmut",10,6,"abs"]]
    
    
    class tableau:
        def __init__(self, tableau1):
    
            h = Scrollbar(tableau1, orient='horizontal')
            h.pack(side=BOTTOM, fill=X)
            v = Scrollbar(tableau1)
            v.pack(side=RIGHT, fill=Y)
    
            for i in range(total_rows):
                for j in range(total_columns):
    
                    self.e = Entry(tableau1, width=10, fg='blue',font=('Arial',16), xscrollcommand=h.set, yscrollcommand=v.set)
                    self.e.grid(row=i, column=j)
                    self.e.insert(END, data[i][j])
                    self.e.config(state="disabled")
    
            self.e.pack(side=TOP, fill=NONE)
            h.config(command=e.xview)
            v.config(command=e.yview)
    
    
    total_rows = len(data)
    total_columns = len(data[0])
    
    tableau(tableau1)
    
    fen.mainloop()
    
    


    Toujours les mêmes erreurs...
    0
  7. Phil_1857 Messages postés 1883 Date d'inscription   Statut Membre Dernière intervention   169
     
    Bonjour,

    Si je comprends bien, tu as récupéré ce code sur Internet, tu ne sait pas trop ce qu'est une classe et à quoi ca sert...
    Est-ce que tu comprends, par exemple, que dans ta double boucle, tu crée des Entry en les nommant "e" à chaque tour, et donc, en fin de boucle, "e" désigne la dernière Entry créée, et pas l'ensemble ...

    A quoi sert la ligne 33 ?

    Il doit y avoir une erreur, la ligne des titres (Nom, Prénom, ...) se répète régulièrement ...
    0
  8. Phil_1857 Messages postés 1883 Date d'inscription   Statut Membre Dernière intervention   169
     
    A tout hasard, je te donne un code qui fait du scroll et qui fonctionne: à toi de l'adapter ...

    from tkinter import *
    
    window = Tk()
    
    scrollbar_v = Scrollbar(window)
    scrollbar_v.pack(side="right", fill="y")
    
    scrollbar_h = Scrollbar(window, orient=HORIZONTAL)
    scrollbar_h.pack(side="bottom", fill="x")
    
    listbox = Listbox(window, xscrollcommand=scrollbar_h.set, yscrollcommand=scrollbar_v.set)
    listbox.insert("end", 'aaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbb')
    for i in range(50): listbox.insert("end", str(i))
    listbox.pack(side="left", fill="both")
    
    scrollbar_v.config(command=listbox.yview)
    scrollbar_h.config(command=listbox.xview)
    
    window.mainloop()
    0