展开 Text 小部件以填充 Tkinter 中的整个父 Frame [英] Expand Text widget to fill the entire parent Frame in Tkinter

查看:37
本文介绍了展开 Text 小部件以填充 Tkinter 中的整个父 Frame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了这个 Text 小部件,我希望它使用网格几何管理器扩展并填充其整个父级.

I got this Text widget, and I'd like for it to expand and fill its entire parent, using the Grid geometry manager.

根据我看到的例子,这个示例程序应该可以工作,可惜它没有,当展开窗口时,内容没有调整大小.

According to the examples I've seen, this sample program should work, alas it doesn't, when expanding the window, the contents are not resizing.

from Tkinter import *
root = Tk()

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

root.mainloop()

感谢任何帮助

就其价值而言,我在 Python 2.7(最新 2.x 版本)中运行,并在 PyCharm 中编码,尽管我认为 IDE 不相关.

For what it's worth, I'm running in Python 2.7 (latest 2.x version), and coding in PyCharm, though I don't think the IDE is relevant.

推荐答案

使用网格时,父级中的任何额外空间都按行和/或列的权重"成比例分配(即:具有权重为 2 的空间是权重为 1 的空间的两倍).默认情况下,行和列的权重为 0(零),这意味着没有给它们额外的空间.

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)

如果您希望它向各个方向增长,您还需要指定行的权重和 N+S+E+W 粘性值.

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.

这篇关于展开 Text 小部件以填充 Tkinter 中的整个父 Frame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆