Scrollbar sur groupe de wigets

Résolu

Bonjour,

J'ai un code Tkinter qui crée une Frame qui contient un ensemble de Labels et Entry

Je voudrais y ajouter une scrollbar verticale, si quelqu'un a une piste...

Merci d'avance pour vos réponses
Windows / Edge 110.0.1587.57

4 réponses

  • Votes
  • Date
  • Date décroissante
  1. Bonjour,

    merci bien, ça marche

    je pensais que créer les widgets dans le canvas ( l = Label(graph_area,... ) était suffisant..

    1. Salut, le scroll s'applique au contenu du canvas, or dans ton code, ton canvas ne contient aucun item. Il faut donc intégrer tes items sur le canvas avec create_window.

      Par exemple.

      from tkinter import *
      
      WIDTH, HEIGHT = 1000, 600
      
      main_win = Tk()
      main_win.title('Test')
      main_win.geometry(f'{WIDTH}x{HEIGHT}+200+50')
      
      f1 = Frame(main_win, width=230, height=500, bd=2, relief=GROOVE)
      f1.place(x=10, y=10)
      graph_area = Canvas(f1, height=580, width=185)
      graph_area.pack(side=LEFT)
      
      e = []
      for k in range(40):
          l = Label(graph_area, text='{:0>2d}'.format(k + 1))
          graph_area.create_window(10, k * 25, window=l)
          e.append(Entry(graph_area))
          graph_area.create_window(110, k * 25, window=e[k])
      
      scroll = Scrollbar(f1, command=graph_area.yview)
      scroll.pack(side='right', fill=Y)
      graph_area.config(
          yscrollcommand=scroll.set,
          yscrollincrement=25,
          scrollregion=graph_area.bbox(ALL),
      )
      
      main_win.mainloop()

      Au lieu de label, tu pourrais à la place utiliser canvas.text.

      1. Bonjour yg_be,

        Le code avec ajout de scrollbar :

        # -*- coding:Utf-8 -*-
        # 28/02/2023 14:45:16
        
        from tkinter import *
        
        WIDTH, HEIGHT = 1000, 600
        
        main_win = Tk()
        main_win.title('Test')
        main_win.geometry(str(WIDTH)+'x'+str(HEIGHT)+'+200+50')
        
        f1 =Frame(main_win, width = 230, height = 500, bd = 2, relief = GROOVE)
        f1.place(x=10,y=10)
        graph_area = Canvas(f1,height=580,width=185)
        graph_area.pack(side =LEFT)
        
        yp = 10
        e = []
        for k in range(40):
            l = Label(graph_area, text = '{:0>2d}'.format(k))
            l.place(x=10,y = yp)
        
            e.append(Entry(graph_area))
            e[k].place(x=30,y = yp)
            yp += 25
        
        scrol =Scrollbar(f1, command = graph_area.yview)
        scrol.pack(side = 'right', fill = Y)
        graph_area.config(yscrollcommand = scrol.set)
        
        main_win.mainloop()
        
        1. Contributeur

          bonjour,

          si tu partages ton code, nous pouvons essayer.

          cela coince où?