Expand Text widget to fill the entire parent Frame in Tkinter

帅比萌擦擦* 提交于 2019-11-30 11:45:13

When using grid, any extra space in the parent is allocated proportionate to the "weight" of a row and/or a column (ie: a column with a weight of 2 gets twice as much of the space as one with a weight of 1). By default, rows and columns have a weight of 0 (zero), meaning no extra space is given to them.

You need to give the column that the widget is in a non-zero weight, so that any extra space when the window grows is allocated to that column.

root.grid_columnconfigure(0, weight=1)

You'll also need to specify a weight for the row, and a sticky value of N+S+E+W if you want it to grow in all directions.

Since your window only contains one widget and you want this widget to fill the entire window, it would be easier to use the pack geometry manager instead of grid

input_text_area.pack(expand=True, fill='both')

expand=True tells Tkinter to allow the widget to expand to fill any extra space in the geometry master. fill='both' enables the widget to expand both horizontally and vertically.

from tkinter import *
root = Tk()

input_text_area = Text(root)
input_text_area.grid(row=0, column=0, columnspan=4, sticky=N+S+W+E)
input_text_area.configure(background='#4D4D4D')
root.grid_columnconfigure(0, weight=1)
root.grid_rowconfigure(0, weight=1)

root.mainloop()

not sure if this is what you want. but this fills the entire screen.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!