代码之家  ›  专栏  ›  技术社区  ›  DSTEP

Tkinter网格管理器框架/画布填充到根窗口底部

  •  0
  • DSTEP  · 技术社区  · 3 年前

    我有以下设置。 根窗口。 在这个根窗口中,我有前3行(0-2)的按钮和标签。这一切都很好。 下面我使用了一个框架,其中包含一个画布,一个滚动条和另一个填充了一堆标签和按钮的框架。这一切都有效。

    但我不知道如何使画布或框架延伸到屏幕底部。(黑色区域)。我试过玩粘性的,也有网格的,但我不明白需要什么安排。如能提供解决方案和解释,将不胜感激!

    注意:前0-2行不会显示任何内容,因为下面的代码运行不包括按钮和标签小部件。但我需要的是将黑色区域扩展到根窗口的底部。我相信解决方案将独立于填充小部件

    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()
    
    1 回复  |  直到 3 年前
        1
  •  1
  •   furas    3 年前

    Canvas 在里面 frame_canvas 第一排有这个框架的人需要 weight=1

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

    enter image description here

    接下来它需要 sticky="news" 对于这个框架和画布。

    self.frame_canvas.grid(row=self.TABLESTARTROW, column=0, sticky='news', columnspan=self.NUM_COLS)
    
    self.canvas.grid(row=0, column=0, sticky='news')
    

    enter image description here


    在Linux上测试