如何将ttk树视图的所有元素放在左侧?

时间:2019-05-24 16:14:35

标签: python tkinter treeview

我想要一个树状视图,其中所有元素都锚定在左侧,没有标题,并且带有一个小加号按钮,用于扩展子值。 由于某些原因,我无法同时使这三件事起作用。

这是我的代码:

import tkinter as tk
from tkinter import ttk

list1 = ["N_1", "N_13", "N_17"]
list2 = ["N_8", "N_3", "N_5", "N_19"]

root = tk.Tk()
frame = tk.Frame(root)
frame.place(x=0, y=0, relwidth=1, relheight=1)
tree = ttk.Treeview(frame)
tree["show"] = "tree"
tree.place(x=0, y=0, relwidth=1, relheight=1)
tree['columns'] = ["Col1"]
tree.column("Col1", anchor='w') #n, ne, e, se, s, sw, w, nw, or center
tree.insert("", "end" , values= "Num1")
for i in list1:
    tree.insert("", "end" , values= i)
id = tree.insert("", "end")
for i in list2:
    tree.insert(id, "end", values=i)

root.mainloop()

The code shown gives you this

如果将show属性更改为“ headings”(标题)

Then the result is this

1 个答案:

答案 0 :(得分:0)

创建树列时,可以将anchor作为参数传递。

import tkinter as tk
from tkinter import ttk

root = tk.Tk()
tree = ttk.Treeview(root)
header = ('size',)
tree['columns'] = header
tree.column('size', width=100, anchor='w') #n, ne, e, se, s, sw, w, nw, or center
tree.heading('size', text='Size')
tree.insert("",0,values=("Left",))
tree.pack()

root.mainloop()