使文本小部件填充框架

Make text widget fill out frame

为什么文本小部件没有填满整个黄色框? 我什至确保将文本小部件设为粘性 - 我错过了什么?`

(can_frame 是 canvas_frame 的缩写 - 我给放在 canvas

中的框架起的名字
from tkinter import *
enter code here
class window:
def __init__(self, root):
    root.grid_rowconfigure   (0, weight=1)
    root.grid_columnconfigure(1, weight=1)

    self.frame = Frame(root)
    self.frame.grid(row=0, column=1, sticky='nsew')
    self.frame.grid_columnconfigure(0, weight=1)
    self.frame.grid_rowconfigure(0, weight=1)

    self.canvas = Canvas(self.frame)
    self.can_frame = Frame(self.canvas, bg='yellow')
    self.canvas.grid(   row=0, column=0, sticky='nsew')
    self.can_frame.grid(row=0, column=0, sticky='nsew')

    self.canvas.grid_columnconfigure((0,1), weight=1)
    self.canvas.grid_rowconfigure(   (0,1), weight=1)

    self._frame_id = self.canvas.create_window((1,1), window=self.can_frame, anchor='sw', tags="self.frame")
    self.canvas.bind(    '<Configure>', self.resize_frame)
    self.can_frame.bind( '<Configure>', self.onFrameConfigure)

    inp = Text(self.can_frame, width=40, relief='groove')   # TEXT WIDGET CREATED HERE
    inp.grid(row=0, column=0, sticky='nsew') # it's sticky so why isn't it filling out the -
                                             # - yellow frame?

def resize_frame(self, e):
    self.canvas.itemconfig(self._frame_id, height=e.height, width=e.width)

def onFrameConfigure(self, e):
    # update scrollregion after starting 'mainloop'
    # when all widgets are in canvas
    self.canvas.configure(scrollregion=self.canvas.bbox('all'))

请不要 post 任何没有 canvas 的解决方案,我需要 canvas 稍后创建滚动条,以便滚动浏览所有不同的文本小部件。 谢谢!

使用 grid 时,您应该始终至少为一行和一列赋予正权重。在您的情况下,您没有为 can_frame 中的任何列赋予权重,因此任何额外的 space 都将未被使用。