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
   |  
import tkinter as tki
 
class wtk_VerticalScrolledFrame(tki.LabelFrame):
    def __init__(self, parent, titre, **kwargs):
 
        #couleurs optionnelles
        bgcolor = kwargs.get('background', None) 
 
        #mise en place
        tki.LabelFrame.__init__(self, parent, text=titre, **kwargs)
        self.vscrollbar = tki.Scrollbar(self, orient=tki.VERTICAL, **kwargs)
        self.vscrollbar.pack(fill=tki.Y, side= tki.RIGHT, expand = False)
        self.canvas = tki.Canvas(self, bd=0, highlightthickness=0, yscrollcommand=self.vscrollbar.set, **kwargs)
        self.canvas.pack(side=tki.LEFT, fill=tki.BOTH, expand= True)
        self.vscrollbar.config(command = self.canvas.yview)
 
        self.canvas.xview_moveto(0)
        self.canvas.yview_moveto(0)
 
        self.interior = tki.Frame(self.canvas)
        self.interior_id = self.canvas.create_window(0,0,window=self.interior, anchor=tki.NW)
 
        #bindings
        self.interior.bind('<Configure>', self._configure_interior)
        self.canvas.bind('<Configure>', self._configure_canvas)
 
 
    def _configure_interior(self, event):
        size = (self.interior.winfo_reqwidth(), self.interior.winfo_reqheight())
        self.canvas.config(scrollregion="0 0 %s %s" % size)
        if self.interior.winfo_reqwidth() != self.canvas.winfo_width():
                self.canvas.config(width= self.interior.winfo_reqwidth())
 
    def _configure_canvas(self, event):
        if self.interior.winfo_reqwidth() != self.canvas.winfo_width():
            self.canvas.itemconfigure(self.interior_id, width=self.canvas.winfo_width())
 
    def supprime_tout(self):
        for o in self.interior.winfo_children():
            o.destroy()
    def settitle(self, titre):
        self.configure(text = titre) 
 
class testapp_textframescrollable(tki.Frame):
    def __init__(self, parent):
        tki.Frame.__init__(self, parent)
        f = wtk_VerticalScrolledFrame(self, 'Textes')
        f.pack(expand = True, fill = tki.BOTH)
        self.pack(expand = True, fill = tki.BOTH)
 
        std = "Hello world \n ça va t'y ? \n tout va bien ? \n bonjour touloooouuuuussssseeee !!!!! \n Hihaaaaaaaaaaaaaaa"
 
        textes = []
        for i in range(10):
            textes.append(tki.Text(f.interior))
            textes[-1].configure(height = 5)
            textes[-1].insert(tki.END, std)
 
            textes[-1].pack(fill = tki.X)
 
if __name__ == '__main__':
 
    root = tki.Tk()
    app = testapp_textframescrollable(root)
    app.mainloop() | 
Partager