Tkinter / TTK-树形视图不会占用整个帧

时间:2018-07-23 00:22:32

标签: python tkinter tkinter-layout

我正在Tkinter / ttk中尝试我的第一个应用程序。我遇到了一个无法解决的小格式问题。我正在创建框架中的股票代码列表的树状视图。当我运行该应用程序时,尽管框架足够大以显示列表中的所有20个符号,但表格中仅显示前10个符号。我必须向下滚动才能看到列表的其余部分。我已经研究了其他答案,但似乎没有给出正确答案。我还尝试了粘性,填充,对齐和高度设置的不同组合,但均未成功。谁能帮我弄清楚我需要做什么才能使树填满框架?

这是显示此问题的代码的最低版本。附加了png的输出。

感谢您的帮助!

#!/usr/local/bin/python3.6

import tkinter as tk
from tkinter import ttk

class ss:
    def __init__(self, root):
        left_frame = tk.Frame(root, height = 8000, width = 1000)
        right_frame = tk.Frame(root, height = 5000, width = 1000)
        left_top = tk.Frame(left_frame, height = 300, width = 1000)
        left_bottom = tk.Frame(left_frame, height = 2000)

        left_frame.grid(row = 0, column = 0, sticky = 'ns')
        right_frame.grid(row = 0, column = 1, sticky = 'ns')
        left_top.grid(row = 0, column = 0, sticky = 'ew')
        left_bottom.grid(row = 1, column = 0, sticky = 'sew')
        left_frame.rowconfigure(1, weight = 1)


        tk.Label(left_top, text = "Sort by:").grid(row = 0, column = 0)


        self.tree = ttk.Treeview(left_bottom, selectmode = 'browse')
        self.tree.pack(side = 'left', expand = 1, fill = 'both')

        scrollbar = ttk.Scrollbar(left_bottom, orient = "vertical", command = self.tree.yview)
        scrollbar.pack(side = 'left', expand = 1, fill = 'y')

        self.current_symbol = ""
        self.stock_label = tk.Label(right_frame,  text = self.current_symbol, anchor = 'n')
        self.stock_label.pack(side = 'top')

        self.tree.configure(yscrollcommand = scrollbar.set)
        self.tree\["columns"\] = ("1")
        self.tree\['show'\] = 'headings'
        self.tree.column("1", width = 100, anchor = 'w')

        self.tree.heading("1", text = "Symbol", anchor = 'w')

        for row in range(0, 20):
            symbol = "Stock" + str(row)
            self.tree.insert("", 'end', text=symbol, values = (symbol))


def main():
    root = tk.Tk()
    root.title("Stock Screener")
    root.geometry("2000x1000")
    ss(root)
    root.mainloop()

if __name__ == "__main__":
    main()

1 个答案:

答案 0 :(得分:2)

您还必须扩展根窗口的row = 0。让左框架展开NS。而且您忘记了在left_bottom方向上展开n

root.rowconfigure(0, weight = 1) # configure the root grid rows
left_frame.grid(row = 0, column = 0, sticky = 'ns')
right_frame.grid(row = 0, column = 1, sticky = 'ns')
left_top.grid(row = 0, column = 0, sticky = 'ew')
left_bottom.grid(row = 1, column = 0, sticky = 'nsew') # here add 'n'
left_frame.rowconfigure(1, weight = 1)