Tkinter 树视图调整树视图大小以适合屏幕

Tkinter treeview resizing the treeview to fit screen

我在让 treeview 调整大小以适应 tkinter window 时遇到一个小问题,似乎无法从 google 得到答案。下面的代码工作正常,除了它似乎在“屏幕”内有一个固定大小的事实。我尝试了各种版本的“填充”和“拉伸”,但我似乎无法正确使用。谁能帮我解释一下这个问题?

代码:

import tkinter as tk
from tkinter import ttk

screen = tk.Tk() 
screen.title('This One')
screen.geometry('890x400')

cols = ('TOKEN', 'F-500', 'F-250', 'F-100', 'F-24', 'POS','NEG', 'NULL', 'VOLUME', 'VOLUME-FUT', 'RPP')
box = ttk.Treeview(screen, columns=cols, show='headings')
for col in cols:
    box.heading(col, text=col)
    box.grid(row=1, column=0, columnspan=2)


box.column("TOKEN", width=95)
box.column("F-500", width=85, anchor='e')
box.column("F-250", width=85, anchor='e')
box.column("F-100", width=85, anchor='e')
box.column("F-24", width=85, anchor='e')
box.column("POS", width=75, anchor='center')
box.column("NEG", width=75, anchor='center')
box.column("NULL", width=75, anchor='center')
box.column("VOLUME", width=90, anchor='center')
box.column("VOLUME-FUT", width=90, anchor='center')
box.column("RPP", width=45, anchor='center')

showScores = tk.Button(screen, text="Update", width=15).grid(row=10, column=1)
closeButton = tk.Button(screen, text="Close", width=15, command=exit).grid(row=10, column=0)

screen.mainloop()

这可能是一件非常简单的事情,但我就是看不到!

谢谢,

莫特

先试试说这个:

screen.grid_rowconfigure(1, weight=1)
screen.grid_columnconfigure(0, weight=1)

并确保还为树视图说 sticky='nsew',例如:

box.grid(row=1, column=0, columnspan=2,sticky='nsew') #say sticky='nsew'

这就是适合屏幕的意思吗?当您调整 window.

大小时,这将确保树视图始终适合屏幕

其他方法是通过说 screen.resizable(0,0)

禁用整个 window 的大小调整

希望这个答案对您有所帮助,如果有任何错误或疑问,请告诉我。

干杯