python滚动条变小_tkinter调整窗口大小而不消失滚动条

这是一个相对简单的问题。在

如果希望继续使用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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值