Tkinter/ttk - treeview 不占用整个框架
Tkinter/ttk - treeview does not take up the full frame
我正在 Tkinter/ttk 试用我的第一个应用程序。我正在 运行 遇到一个我无法解决的小格式问题。我正在创建框架中股票代码列表的树视图。当我 运行 应用程序时,只有前 10 个符号显示在 table 中,尽管框架足够大以显示列表中的所有 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()
您还必须展开根 window 的行=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)
我正在 Tkinter/ttk 试用我的第一个应用程序。我正在 运行 遇到一个我无法解决的小格式问题。我正在创建框架中股票代码列表的树视图。当我 运行 应用程序时,只有前 10 个符号显示在 table 中,尽管框架足够大以显示列表中的所有 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()
您还必须展开根 window 的行=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)