如何让tkinter的树状视图适应你的框架大小

3
我希望我的树形视图能够完全适应我为GUI定义的窗口的几何大小,但显示内容并没有覆盖整个窗口,而是显示在中间位置。
from tkinter import ttk
import tkinter as tk


root = tk.Tk()
root.geometry("1200x680+50+20")


tree = ttk.Treeview(root)
tree.insert("", "0", "item1", text="LANGUAGE")
tree.insert("", "1", "item2", text="GUI")
tree.insert("item1", "0", text="pyhton")

#SUb treeview
style = ttk.Style(root)
style.configure("Treeview", rowheight=70)
tree.configure(style="Treeview")

    ############
tree.config(columns=("NOTE", "book"))   # this creates to seperate headings 
for treeview
tree.column("NOTE", width=300)
tree.heading("NOTE", text="Info")
tree.column("book", width=300)
tree.heading("book", text="profile")


tree.set("item1", "NOTE","Am using python version 3.6.1 \n on windows 
machine")
tree.set("item2","NOTE","This an example Tkinter Treeview in Python, which 
is from \nttk class make sure import ttk\n also from tkinter import *")


tree.pack()
root.mainloop()

我试图将它放在一个框架中,但这也不能显示树视图的内容。以下是代码:

fr = tk.Frame(root, width=1200, height=680, relief="groove") 

tree = ttk.Treeview(fr) 
1个回答

7
请替换以下代码行。
tree.pack()

使用以下行

tree.pack(fill='x')

这将使用根屏幕的完整空间。

之前: enter image description here 之后: enter image description here


为什么不能将y轴和x轴同时放置在包中,有任何原因吗?我问这个问题是因为当我更改主题时,它默认填充x轴。 - AD WAN
1
尝试使用以下代码: tree.pack(fill='both') - Fuzed Mass
@Bryan Oakley:感谢您的提示,但是当您将treeview作为toplevel窗口调用时,它并不会填充输入窗口。已经使用了fill both和fill x,有什么建议吗? - AD WAN
我不知道你说的"将treeview作为toplevel窗口调用"是什么意思 -- 你不能"调用"一个treeview,它也不是一个toplevel窗口。然而,要使其在两个方向上填充,请使用fill='both' - Bryan Oakley
已创建了一个按钮,当您单击它时会打开树视图,树视图会作为顶级窗口弹出。使用了fill="both"属性,但它并没有填充两个方向。 - AD WAN
显示剩余5条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接