web-dev-qa-db-fra.com

Barre de défilement Python Treeview

Mon environnement est Windows 7 utilisant Python 64 bits 3.4. J'essaie de joindre une barre de défilement à un widget arborescent. J'ai essayé plusieurs versions mais je n'ai pas réussi à le faire fonctionner. Ce que j'ai essayé jusqu'à présent:

# treeview example
from tkinter import ttk
from tkinter import *

# Create instance
win = Tk()

# Add a title       
    win.title("Treeview Test")

# Add a treeview
    tree = ttk.Treeview(win,selectmode='browse')
    vsb = ttk.Scrollbar(orient="vertical",command=tree.yview)
    tree.configure(yscrollcommand=vsb.set)

    tree.place(x = 30, y = 95)
    tree["columns"] = ("1", "2")
    tree['show'] = 'headings'
    tree.column("1", width=100, anchor='c')
    tree.column("2", width=100, anchor='c')
    tree.heading("1", text="Account")
    tree.heading("2", text="Type")
    tree.insert("",'end',text="L1",values=("Big1","Best"))
    tree.insert("",'end',text="L2",values=("Big2","Best"))
    tree.insert("",'end',text="L3",values=("Big3","Best"))
    tree.insert("",'end',text="L4",values=("Big4","Best"))
    tree.insert("",'end',text="L5",values=("Big5","Best"))
    tree.insert("",'end',text="L6",values=("Big6","Best"))
    tree.insert("",'end',text="L7",values=("Big7","Best"))
    tree.insert("",'end',text="L8",values=("Big8","Best"))
    tree.insert("",'end',text="L9",values=("Big9","Best"))
    tree.insert("",'end',text="L10",values=("Big10","Best"))
    tree.insert("",'end',text="L11",values=("Big11","Best"))
    tree.insert("",'end',text="L12",values=("Big12","Best"))

# Set Window Form Size and disable resizing
    win.minsize(width=1200,height=600)
    win.resizable(width=0,height=0)  

# Event Loop
    win.mainloop()
    quit()
5
Steve Baker

Vous devez place() votre barre de défilement dans la fenêtre.

from tkinter import ttk
import tkinter as tk

win = tk.Tk()
win.minsize(width=300, height=400)
win.resizable(width=0, height=0)

tree = ttk.Treeview(win, selectmode='browse')
tree.place(x=30, y=95)

vsb = ttk.Scrollbar(win, orient="vertical", command=tree.yview)
vsb.place(x=30+200+2, y=95, height=200+20)

tree.configure(yscrollcommand=vsb.set)

tree["columns"] = ("1", "2")
tree['show'] = 'headings'
tree.column("1", width=100, anchor='c')
tree.column("2", width=100, anchor='c')
tree.heading("1", text="Account")
tree.heading("2", text="Type")
tree.insert("",'end',text="L1",values=("Big1","Best"))
tree.insert("",'end',text="L2",values=("Big2","Best"))
tree.insert("",'end',text="L3",values=("Big3","Best"))
tree.insert("",'end',text="L4",values=("Big4","Best"))
tree.insert("",'end',text="L5",values=("Big5","Best"))
tree.insert("",'end',text="L6",values=("Big6","Best"))
tree.insert("",'end',text="L7",values=("Big7","Best"))
tree.insert("",'end',text="L8",values=("Big8","Best"))
tree.insert("",'end',text="L9",values=("Big9","Best"))
tree.insert("",'end',text="L10",values=("Big10","Best"))
tree.insert("",'end',text="L11",values=("Big11","Best"))
tree.insert("",'end',text="L12",values=("Big12","Best"))

win.mainloop()

 enter image description here

Mais place() nécessite des calculs manuels de x et y. Il est plus facile d'utiliser pack()

from tkinter import ttk
import tkinter as tk

win = tk.Tk()
win.resizable(width=0, height=0)

tree = ttk.Treeview(win, selectmode='browse')
tree.pack(side='left')

vsb = ttk.Scrollbar(win, orient="vertical", command=tree.yview)
vsb.pack(side='right', fill='y')

tree.configure(yscrollcommand=vsb.set)

tree["columns"] = ("1", "2")
tree['show'] = 'headings'
tree.column("1", width=100, anchor='c')
tree.column("2", width=100, anchor='c')
tree.heading("1", text="Account")
tree.heading("2", text="Type")
tree.insert("",'end',text="L1",values=("Big1","Best"))
tree.insert("",'end',text="L2",values=("Big2","Best"))
tree.insert("",'end',text="L3",values=("Big3","Best"))
tree.insert("",'end',text="L4",values=("Big4","Best"))
tree.insert("",'end',text="L5",values=("Big5","Best"))
tree.insert("",'end',text="L6",values=("Big6","Best"))
tree.insert("",'end',text="L7",values=("Big7","Best"))
tree.insert("",'end',text="L8",values=("Big8","Best"))
tree.insert("",'end',text="L9",values=("Big9","Best"))
tree.insert("",'end',text="L10",values=("Big10","Best"))
tree.insert("",'end',text="L11",values=("Big11","Best"))
tree.insert("",'end',text="L12",values=("Big12","Best"))

win.mainloop()

 enter image description here

Vous pouvez toujours mettre Treeview et Scrollbar dans Frame et ensuite déplacer ce cadre avec place(), pack() ou grid() au meilleur endroit.

14
furas

Après avoir créé vos widgets, vous devez les placer ou les disposer dans la fenêtre Tk. Les gestionnaires de disposition disponibles dans tkinter sont grid, pack et place. Vérifiez-les et ajoutez-les à vos widgets.

0
Sun Bear