Python 展开 Text 小部件以填充 Tkinter 中的整个父 Frame
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28419763/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Expand Text widget to fill the entire parent Frame in Tkinter
提问by So Many Goblins
I got this Text
widget, and I'd like for it to expand and fill its entire parent, using the Grid geometry manager.
我得到了这个Text
小部件,我希望它使用网格几何管理器扩展并填充其整个父级。
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()
Any help is appreciated
任何帮助表示赞赏
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.
对于它的价值,我在 Python 2.7(最新的 2.x 版本)中运行,并在 PyCharm 中编码,尽管我认为 IDE 不相关。
采纳答案by Bryan Oakley
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.
使用网格时,父项中的任何额外空间都按行和/或列的“权重”成比例分配(即:权重为 2 的列获得的空间是权重为 1 的列的两倍)。默认情况下,行和列的权重为 0(零),这意味着没有给它们额外的空间。
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.
您还需要为该行指定一个权重,N+S+E+W
如果您希望它向各个方向增长,还需要指定一个粘性值。
回答by Bryan Oakley
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 managerinstead of grid
由于您的窗口仅包含一个小部件并且您希望该小部件填充整个窗口,因此使用pack
几何管理器而不是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.
expand=True
告诉 Tkinter 允许小部件扩展以填充几何母版中的任何额外空间。 fill='both'
使小部件能够水平和垂直扩展。
回答by Ajay
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.
不知道这是否是你想要的。但这会填满整个屏幕。