将按钮与树视图行对齐

时间:2015-04-23 13:56:21

标签: python tkinter

我正在尝试制作这样的界面:

example of gui

我想坚持使用tkinter。 我的一个想法是在左表中使用Treeview。但我很难让右侧的按钮与桌子的行对齐。 我试图在按钮顶部放一个空框架,但似乎无法使它工作。

这是我的代码:

import tkinter as tk
from tkinter import ttk

class ButtonTree(ttk.Frame):
    def __init__(self, parent, items):
        ttk.Frame.__init__(self, parent)
        self.parent = parent
        self.items = items        
        self.buttons = self.create_buttons()
        self.tree = MyTree(self, items)

        self.tree.grid(column = 0, row = 0, rowspan = len(items)+1)
        self.empty_space = ttk.Frame(self)
        self.empty_space.grid(column = 1, row = 0)
        self.buttons.grid(column = 1, row = 1)        

    def create_buttons(self):
        buttons = ttk.Frame(self)
        for item in self.items:
            button = ttk.Button(buttons, text = "Do Something...")
            button.pack(side = tk.TOP)
        return buttons

class MyTree(ttk.Treeview):
    def __init__(self, parent, items):
        ttk.Treeview.__init__(self, parent, columns = ("A"))
        self.heading("A", text = "Some A")
        self.parent = parent
        self.items = items
        for item in self.items:
            self.insert(parent = "", index = "end", text = item, values = "hello")

if __name__ == "__main__":
    root = tk.Tk()
    buttonTree = ButtonTree(root, ["Hello", "Bye"])
    buttonTree.pack()
    root.mainloop()

我有一个想法是获得按钮的大小并使表中行的高度与按钮的大小相匹配,但我找不到如何从按钮获取该信息。

我做的另一种尝试是一种只有框架和按钮的桌子。但是按钮总是使用比框架更多的垂直空间,我无法保持它们对齐。

如何使按钮与树视图的行对齐?

1 个答案:

答案 0 :(得分:2)

这使他们全部排成一行:

import tkinter as tk
from tkinter import ttk

class ButtonTree(ttk.Frame):
    def __init__(self, parent, items):
        ttk.Frame.__init__(self, parent)
        self.parent = parent
        self.items = items
        self.tree = MyTree(self, items)
        self.tree.grid(column=0, row=0, sticky=tk.N)
        self.buttons = ttk.Frame(self, width=100)
        self.buttons.grid(column=1, row=0, sticky=tk.N)
        print(self.tree.config())

    def create_buttons(self):
        self.update()
        so_far = header_height = 16 + 2
        ttk.Frame(self.buttons).place(in_=self.buttons, x=0, y=0, width=100, height=header_height)

        for item_name, item in zip(self.tree.get_children(), self.items):
            print(item_name, item,self.tree.bbox(item_name))
            button = ttk.Button(self.buttons, text = "Do Something...")
            h = self.tree.bbox(item_name)[-1]
            button.place(in_=self.buttons, x=0, y=so_far, width=100, height=h)
            so_far += h

        self.buttons["height"] = len(self.items)*20 + header_height

class MyTree(ttk.Treeview):
    def __init__(self, parent, items):
        ttk.Treeview.__init__(self, parent, columns = ("A"), padding=1)
        self.heading("A", text = "Some A")
        self.parent = parent
        self.items = items
        for item in self.items:
            self.insert(parent = "", index = "end", text = item, values = "hello")

if __name__ == "__main__":
    root = tk.Tk()
    buttonTree = ButtonTree(root, list(range(10)))
    buttonTree.pack()
    buttonTree.create_buttons()
    root.mainloop()

给出:

enter image description here

项目高度由Treeview.bbox(item)找到。但是,滚动时按钮不会移动,并且猜测了标题高度。更灵活的解决方案是使用Treeview.tag_bind,但这意味着您将没有按钮,但可以点击列表项。