Tkinter 网格管理器 Frame/Canvas 填充到根底部 Window
Tkinter Grid Manager Frame/Canvas to Fill to Bottom of Root Window
我有以下设置。
一个根window。
在那个根 window 中,我有前 3 行 (0-2) 的按钮和标签。这一切都很好。
在其下方,我使用了一个框架,其中包含一个 canvas,其中包含一个滚动条和另一个填充有一堆标签和按钮的框架。这一切都有效。
但我不知道如何使 canvas 或框架延伸到屏幕底部。 (黑色区域)。我试过玩 sticky 和 grid_columnconfigure 但我无法理解所需的安排。非常感谢解决方案和解释!
注意:前 0-2 行不会显示任何内容,因为下面的代码 运行 不包括按钮和标签小部件。但我需要的是将黑色区域扩展到根 window 的底部。我相信解决方案将独立于填充小部件
enter image description here
这是我的代码。
import tkinter as tk
class GUI(tk.Tk):
NUM_COLS = 12
ROWSB4TABLE = 3
TABLESTARTROW = ROWSB4TABLE + 1
def __init__(self):
super().__init__()
#configure GUI
self.title("Test")
self.geometry("1000x500")
self.resizable(True, True)
#configure Grid
self.grid_columnconfigure(0, weight=2, uniform='col')
self.grid_columnconfigure(1, weight=2, uniform='col')
self.grid_columnconfigure(2, weight=2, uniform='col')
self.grid_columnconfigure(3, weight=2, uniform='col')
self.grid_columnconfigure(4, weight=2, uniform='col')
self.grid_columnconfigure(5, weight=2, uniform='col')
self.grid_columnconfigure(6, weight=2, uniform='col')
self.grid_columnconfigure(7, weight=2, uniform='col')
self.grid_columnconfigure(8, weight=1, uniform='col')
self.grid_columnconfigure(9, weight=1, uniform='col')
self.grid_columnconfigure(10, weight=1, uniform='col')
self.grid_columnconfigure(11, weight=1, uniform='col')
self.grid_rowconfigure(0, weight=0, uniform='row')
self.grid_rowconfigure(1, weight=0, uniform='row')
self.grid_rowconfigure(2, weight=0, uniform='row')
# create frame
self.frame_canvas = tk.Frame(self)
self.frame_canvas.grid(row=self.TABLESTARTROW, column=0, sticky=tk.EW, columnspan=self.NUM_COLS)
self.frame_canvas.grid_rowconfigure(0, weight=1)
self.frame_canvas.grid_columnconfigure(0, weight=1)
# create canvas
self.canvas = tk.Canvas(self.frame_canvas, bg="black")
self.canvas.grid(row=0, column=0, sticky=tk.EW)
# create scrollbar
vsb = tk.Scrollbar(self.frame_canvas, orient="vertical", command=self.canvas.yview)
vsb.grid(row=0, column=12, sticky='ns')
# set scrollbar to canvas
self.canvas.configure(yscrollcommand=vsb.set)
#create frame for canvas (to hold widgets)
self.tableframe = tk.Frame(self.canvas, bg='Black')
self.canvas.create_window((0,0), window=self.tableframe, anchor=tk.NW)
self.tableframe.grid_columnconfigure(0, weight=2, uniform='coltf')
self.tableframe.grid_columnconfigure(1, weight=2, uniform='coltf')
self.tableframe.grid_columnconfigure(2, weight=2, uniform='coltf')
self.tableframe.grid_columnconfigure(3, weight=2, uniform='coltf')
self.tableframe.grid_columnconfigure(4, weight=2, uniform='coltf')
self.tableframe.grid_columnconfigure(5, weight=2, uniform='coltf')
self.tableframe.grid_columnconfigure(6, weight=2, uniform='coltf')
self.tableframe.grid_columnconfigure(7, weight=2, uniform='coltf')
self.tableframe.grid_columnconfigure(8, weight=1, uniform='coltf')
self.tableframe.grid_columnconfigure(9, weight=1, uniform='coltf')
self.tableframe.grid_columnconfigure(10, weight=1, uniform='coltf')
self.tableframe.grid_columnconfigure(11, weight=1, uniform='coltf')
###self.tableframe.grid_columnconfigure(12, weight=1, uniform='coltf')
if __name__ == "__main__":
app = GUI()
app.mainloop()
Canvas
位于 frame_canvas
内,此框架的第一行需要 weight=1
self.grid_rowconfigure(self.TABLESTARTROW, weight=1)
接下来它需要 sticky="news"
和 canvas。
self.frame_canvas.grid(row=self.TABLESTARTROW, column=0, sticky='news', columnspan=self.NUM_COLS)
self.canvas.grid(row=0, column=0, sticky='news')
测试于 Linux
我有以下设置。 一个根window。 在那个根 window 中,我有前 3 行 (0-2) 的按钮和标签。这一切都很好。 在其下方,我使用了一个框架,其中包含一个 canvas,其中包含一个滚动条和另一个填充有一堆标签和按钮的框架。这一切都有效。
但我不知道如何使 canvas 或框架延伸到屏幕底部。 (黑色区域)。我试过玩 sticky 和 grid_columnconfigure 但我无法理解所需的安排。非常感谢解决方案和解释!
注意:前 0-2 行不会显示任何内容,因为下面的代码 运行 不包括按钮和标签小部件。但我需要的是将黑色区域扩展到根 window 的底部。我相信解决方案将独立于填充小部件
enter image description here
这是我的代码。
import tkinter as tk
class GUI(tk.Tk):
NUM_COLS = 12
ROWSB4TABLE = 3
TABLESTARTROW = ROWSB4TABLE + 1
def __init__(self):
super().__init__()
#configure GUI
self.title("Test")
self.geometry("1000x500")
self.resizable(True, True)
#configure Grid
self.grid_columnconfigure(0, weight=2, uniform='col')
self.grid_columnconfigure(1, weight=2, uniform='col')
self.grid_columnconfigure(2, weight=2, uniform='col')
self.grid_columnconfigure(3, weight=2, uniform='col')
self.grid_columnconfigure(4, weight=2, uniform='col')
self.grid_columnconfigure(5, weight=2, uniform='col')
self.grid_columnconfigure(6, weight=2, uniform='col')
self.grid_columnconfigure(7, weight=2, uniform='col')
self.grid_columnconfigure(8, weight=1, uniform='col')
self.grid_columnconfigure(9, weight=1, uniform='col')
self.grid_columnconfigure(10, weight=1, uniform='col')
self.grid_columnconfigure(11, weight=1, uniform='col')
self.grid_rowconfigure(0, weight=0, uniform='row')
self.grid_rowconfigure(1, weight=0, uniform='row')
self.grid_rowconfigure(2, weight=0, uniform='row')
# create frame
self.frame_canvas = tk.Frame(self)
self.frame_canvas.grid(row=self.TABLESTARTROW, column=0, sticky=tk.EW, columnspan=self.NUM_COLS)
self.frame_canvas.grid_rowconfigure(0, weight=1)
self.frame_canvas.grid_columnconfigure(0, weight=1)
# create canvas
self.canvas = tk.Canvas(self.frame_canvas, bg="black")
self.canvas.grid(row=0, column=0, sticky=tk.EW)
# create scrollbar
vsb = tk.Scrollbar(self.frame_canvas, orient="vertical", command=self.canvas.yview)
vsb.grid(row=0, column=12, sticky='ns')
# set scrollbar to canvas
self.canvas.configure(yscrollcommand=vsb.set)
#create frame for canvas (to hold widgets)
self.tableframe = tk.Frame(self.canvas, bg='Black')
self.canvas.create_window((0,0), window=self.tableframe, anchor=tk.NW)
self.tableframe.grid_columnconfigure(0, weight=2, uniform='coltf')
self.tableframe.grid_columnconfigure(1, weight=2, uniform='coltf')
self.tableframe.grid_columnconfigure(2, weight=2, uniform='coltf')
self.tableframe.grid_columnconfigure(3, weight=2, uniform='coltf')
self.tableframe.grid_columnconfigure(4, weight=2, uniform='coltf')
self.tableframe.grid_columnconfigure(5, weight=2, uniform='coltf')
self.tableframe.grid_columnconfigure(6, weight=2, uniform='coltf')
self.tableframe.grid_columnconfigure(7, weight=2, uniform='coltf')
self.tableframe.grid_columnconfigure(8, weight=1, uniform='coltf')
self.tableframe.grid_columnconfigure(9, weight=1, uniform='coltf')
self.tableframe.grid_columnconfigure(10, weight=1, uniform='coltf')
self.tableframe.grid_columnconfigure(11, weight=1, uniform='coltf')
###self.tableframe.grid_columnconfigure(12, weight=1, uniform='coltf')
if __name__ == "__main__":
app = GUI()
app.mainloop()
Canvas
位于 frame_canvas
内,此框架的第一行需要 weight=1
self.grid_rowconfigure(self.TABLESTARTROW, weight=1)
接下来它需要 sticky="news"
和 canvas。
self.frame_canvas.grid(row=self.TABLESTARTROW, column=0, sticky='news', columnspan=self.NUM_COLS)
self.canvas.grid(row=0, column=0, sticky='news')
测试于 Linux