如何限制使用tkinter treeview显示的列?

时间:2016-08-18 12:30:12

标签: python python-3.x tkinter treeview ttk

我有一个显示树视图窗口的功能,其中包含用户提供的列表。我已经内置了一个水平和垂直滚动条来滚动。但是,如何限制显示的列数?如果为该功能提供了许多列,您必须使窗口更大才能查看,我不希望这样,我希望它停在那个宽度然后允许滚动来完成剩下的工作。这可能吗?我已经使用示例函数附加了我的代码。

提前致谢

import tkinter as tk
from tkinter import ttk 

def Display_Results(list_of_values):
    '''
    This will disply your results in a way similar to excel would, headers then values below them

    Sample list: testlist=[['A', 'B', 'C'], [1 , 2, 3], [4, '' ,5], [7, 8, 9]] 
    Use the sample list to show how values will be displayed 

    :param list_of_values: A list with lists, the first list must be the headers. Then the following lists the values for the headers.

    '''
    root=tk.Tk() #Makes window object
    root.config(width=900,height=400, bg='grey') #size of window, and background color



    tree = ttk.Treeview(root, selectmode="extended",columns=list_of_values[0])
    vsb = tk.Scrollbar(orient="vertical", command=tree.yview)
    hsb=tk.Scrollbar(orient="horizontal", command=tree.xview)
    tree.configure(yscrollcommand=vsb.set, xscrollcommand=hsb.set)

    vsb.place(x=100,y=300)
    hsb.place(x=300,y=310)


    tree.heading("#0", text="Data")
    tree.column("#0",minwidth=0,width=100, anchor='c')

    for x in list_of_values[0]:
        tree.heading(x, text=x)   
        tree.column(x,minwidth=0,width=100, anchor='c') 

    for y in range(1, len(list_of_values)):
        tree.insert("" , y-1,    text=("Data " + str(y)), values=list_of_values[y])




    tree.place(x=0,y=0)

    root.mainloop() #needed to keep the window displayed

testlist=[['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'], [1 , 2, 3], [4, '', 5], [7, 8, 9], [1 , 2, 3], [4, 5], [7, 8, 9], [1 , 2, 3], [4, 5], [7, 8, 9], [1 , 2, 3], [4, 5], [7, 8, 9], [1 , 2, 3], [4, 5], [7, 8, 9], [1 , 2, 3], [4, 5], [7, 8, 9], [1 , 2, 3], [4, 5], [7, 8, 9], [1 , 2, 3], [4, 5], [7, 8, 9], [1 , 2, 3], [4, 5], [7, 8, 9]] 
Display_Results(testlist)

0 个答案:

没有答案