当前位置:  开发笔记 > 编程语言 > 正文

Python Treeview滚动条

如何解决《PythonTreeview滚动条》经验,为你挑选了1个好方法。

我的环境是使用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", c')
    tree.column("2", 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()

furas.. 15

你必须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", c')
tree.column("2", 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()需要手动计算xy.它更容易使用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", c')
tree.column("2", 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()

在此输入图像描述

你可以随时把TreeviewScrollbarFrame,然后移动这个框架有place(),pack()grid()在更好的地方.



1> furas..:

你必须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", c')
tree.column("2", 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()需要手动计算xy.它更容易使用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", c')
tree.column("2", 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()

在此输入图像描述

你可以随时把TreeviewScrollbarFrame,然后移动这个框架有place(),pack()grid()在更好的地方.

推荐阅读
Gbom2402851125
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有