这是一个相对简单的问题。在
如果希望继续使用Grid几何管理器创建程序,则需要熟悉Grid.rowconfigure()和{}函数。在
这些允许您在容器小部件的范围内为Grid几何管理器设置配置选项。具体来说,我们关心的属性是weight。如前所述here:Every column and row has a "weight" grid option associated with it, which tells it how much it should grow if there is extra room in the master to fill. By default, the weight of each column or row is 0, meaning don't expand to fill space.
For the user interface to resize then, we'll need to give a positive weight to the columns we'd like to expand. This is done using the "columnconfigure" and "rowconfigure" methods of grid. If two columns have the same weight, they'll expand at the same rate; if one has a weight of 1, another of 3, the latter one will expand three pixels for every one pixel added to the first.
(重点是我的)
所以,在这种情况下,我们需要做一些改变。首先,我们需要将sticky = "NESW"添加到frame.grid()和text_widget.grid()调用中,否则Text小部件不会随着滚动条展开。其次,我们需要在程序中添加以下代码片段:Grid.columnconfigure(root, 0, weight=1)
Grid.rowconfigure(root, 0, weight=1)
Grid.columnconfigure(frame, 0, weight=1)
Grid.rowconfigure(frame, 0, weight=1)
最后是下面的程序(在做了一些更改之后,我可以实际运行所提供的示例):
^{pr2}$
作为补充说明,使用^{}几何管理器重建此程序非常简单,当调整小部件的大小时,它(主观上)更智能:from tkinter import *
from tkinter import ttk
root = Tk()
frame = ttk.Frame(master=root)
frame.pack(expand=True, fill="both")
vbar = ttk.Scrollbar(frame, orient=VERTICAL)
hbar = ttk.Scrollbar(root, orient=HORIZONTAL)
vbar.pack(side="right", fill="y")
root.update()
hbar.pack(side="bottom", fill="x", padx=vbar.winfo_width())
text_widget = Text(frame, wrap=NONE, undo=True, yscrollcommand=vbar.set, xscrollcommand=hbar.set)
text_widget.pack(expand=True, fill="both")
vbar.config(command=text_widget.yview)
hbar.config(command=text_widget.xview)