使文本小部件填写框架

时间:2017-08-29 17:54:10

标签: tkinter tkinter-canvas

enter image description here 为什么文本小部件不会填满整个黄框? 我甚至确保文本小部件变得粘稠 - 我缺少什么?“

can_frame是canvas_frame的缩写 - 我给画布放入画布的名称

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'))

请不要在没有画布的情况下发布任何解决方案,我需要画布稍后创建滚动条,以滚动浏览所有不同的文本小部件。 的谢谢!

1 个答案:

答案 0 :(得分:1)

使用grid时,您应始终至少为一行和一列提供正重量。在您的情况下,您没有给can_frame中的任何列赋予权重,因此任何额外的空间都将被闲置。