【问题标题】:Tkinter/ttk - treeview does not take up the full frameTkinter/ttk - 树视图不占用全帧
【发布时间】:2018-12-30 09:40:21
【问题描述】:

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

这是显示问题的代码的最小版本。附上输出的 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()

【问题讨论】:

    标签: python tkinter tkinter-layout


    【解决方案1】:

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

    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)
    

    【讨论】:

    • 就是这样。感谢您的快速解决方案。
    猜你喜欢
    • 2017-02-06
    • 2018-02-13
    • 1970-01-01
    • 1970-01-01
    • 2011-08-27
    • 1970-01-01
    • 2014-07-03
    • 2020-07-21
    • 2011-12-15
    相关资源
    最近更新 更多