如何让 Treeview 列适合它所在的框架 [英] How to get a Treeview columns to fit the Frame it is within

查看:57
本文介绍了如何让 Treeview 列适合它所在的框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

动态更改树视图数据时,我希望行扩展到树视图框架的大小.目前,如果我使 GUI 全屏并重新填充数据,它只使用可用屏幕大小的一小部分,即列不会调整大小或拉伸以填充屏幕的宽度(我不太关心行填充).我在下面制作了一个最小的工作示例:

When dynamically changing a treeviews data, I want the rows to expand to the size of the treeview's frame. Currently if I make the GUI fullscreen and re-fill the data it only uses a small portion of the available screen-size, i.e. the columns don't resize or stretch to fill the width of the screen (i'm not too concerned with the rows filling). I have produced a minimal working example below:


import tkinter as tk
from tkinter import ttk
import random 

class App():

    def __init__(self):

        self.root = tk.Tk()

        self.frame = tk.Frame(self.root)
        self.frame.pack(expand=True, fill=tk.BOTH)

        self.tree = ttk.Treeview(self.frame, show="headings")
        self.tree.pack(expand=True, fill=tk.BOTH)

        self.button = ttk.Button(self.root, text="Fill", command=self.fill)
        self.button.pack(side=tk.BOTTOM,expand=True,fill=tk.X)

        self.root.mainloop()

    def fill(self):

        if self.has_data():
            self.tree.delete(*self.tree.get_children())

        i = random.randrange(1,10)
        self.tree["columns"]=tuple([str(i) for i in range(i)])

        for col in self.tree['columns']:
            self.tree.heading(col, text="Column {}".format(col), anchor=tk.CENTER)
            self.tree.column(col, anchor=tk.CENTER)

        j = random.randrange(10)

        for j in range(j):

            self.tree.insert("", "end", values = tuple([k for k in range(i)]))

    def has_data(self):

        has_tree = self.tree.get_children()

        return True if has_tree else False


App()

请注意,自己调整 GUI 大小会自动调整列的大小.

Note that resizing the GUI myself resizes the columns automatically.

推荐答案

问题是列是用初始大小创建的(我认为默认为 200 像素),并且只有在创建后调整树视图大小时才会拉伸.因此,您必须手动设置列宽以占用整个可用空间.为此,您可以使用树视图的 column() 方法的 width 参数:

The issue is that the columns are created with an initial size (default is 200 pixels I think) and only stretch if the treeview is resized after their creation. So you will have to manually set the column width to occupy the whole available space. To do so you can use the width argument of the column() method of the treeview:

self.tree.column(col, width=col_width)

其中 col_width 是总宽度除以列数.将此代码合并到 fill() 函数中可以得到

where col_width is the total width divided by the number of columns. Incorporating this code in the fill() function gives

def fill(self):

    if self.has_data():
        self.tree.delete(*self.tree.get_children())

    i = random.randrange(1,10)
    self.tree["columns"]=tuple([str(i) for i in range(i)])

    col_width = self.tree.winfo_width() // i # compute the width of one column

    for col in self.tree['columns']:
        self.tree.heading(col, text="Column {}".format(col), anchor=tk.CENTER)
        self.tree.column(col, anchor=tk.CENTER, width=col_width)  # set column width

    j = random.randrange(10)

    for j in range(j):

        self.tree.insert("", "end", values = tuple([k for k in range(i)]))

这篇关于如何让 Treeview 列适合它所在的框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆