简体   繁体   中英

Tkinter Grid Manager Frame/Canvas to Fill to Bottom of Root Window

I have the following setup. A root window. In that root window I have the first 3 rows (0-2) of buttons and labels. This is all good. Below that I use a frame, that holds a canvas, that holds a scrollbar and another frame that is populated with a bunch of labels and buttons. This all works.

But I cannot figure out how to make that canvas or frame to extend to the bottom of the screen. (the black area). I have tried playing with the sticky and also grid_columnconfigure but I cannot understand the arrangement needed. A solution and an explanation will be greatly appreciated!

Note: The first 0-2 rows will not show up with anything as the below code run dies not include the button and label widgets. But what I need is for the black area to expand to the bottom of the root window. I believe the solution will be independent of the populating widgets

enter image description here

Here is my code.

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 is inside frame_canvas and first row with this frame needs weight=1

self.grid_rowconfigure(self.TABLESTARTROW, weight=1)

在此处输入图像描述

And next it needs sticky="news" for this frame and 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')

在此处输入图像描述


Tested on Linux

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM