我的环境是使用Python 64位3.4的Windows 7。我试图将滚动条附加到树视图小部件。我尝试了几个版本,但一直无法使用它。到目前为止我尝试了什么:
# 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()
答案 0 :(得分:14)
您必须在窗口中place()
滚动条。
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()
但place()
需要手动计算x
和y
。使用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()
您始终可以将Treeview
和Scrollbar
放入Frame
,然后将此框架与place()
,pack()
或grid()
一起移到更好的位置
答案 1 :(得分:0)
创建小部件后,您需要在Tk窗口上放置或布置小部件。 tkinter中可用的布局管理器是网格,包和地方。检查它们并将它们添加到您的小部件中。
答案 2 :(得分:0)
import tkinter as tk
from tkinter.ttk import Treeview, Scrollbar
top = tk.Tk()
top.geometry('1400x800')
top.configure(background="#f7f7f7")
# treeview initialization
treeview = Treeview(top)
# scrollbars
vsb = Scrollbar(top, orient="vertical", command=treeview.yview)
vsb.place(relx=0.978, rely=0.175, relheight=0.713, relwidth=0.020)
hsb = Scrollbar(top, orient="horizontal", command=treeview.xview)
hsb.place(relx=0.014, rely=0.875, relheight=0.020, relwidth=0.965)
treeview.configure(yscrollcommand=vsb.set, xscrollcommand=hsb.set)
也许这可以帮助您。