tkinter resize window 滚动条不消失

tkinter resize window without scrollbar disappearing

我一直在编写一个应用程序,它几乎是一个花哨的文本编辑器,但我在调整其中的小部件大小时遇到​​了问题。我的目标是让它看起来有点像带有水平滚动条的 IDLE。

from tkinter import *
from tkinter import ttk

root = Tk()

width = preset frame width in relation to root
hieght = preset frame height in relation to root

frame = ttk.Frame(master=root, width=width, height=height)
frame.grid()
frame.grid_propagate(False)

vbar = ttk.Scrollbar(root, orient=VERTICAL)
hbar = ttk.Scrollbar(root, orient=HORIZONTAL)
vbar.grid(row=0, column=1, sticky=N S)
hbar.grid(row=1, column=0, sticky=E W)

text_widget = Text(frame, width=189, height=47, wrap=NONE, undo=True, yscrollcommand=vbar.set, xscrollcommand=hbar.set)
text_widget.grid()

vbar.config(command=text_widget.yview)
hbar.config(command=text_widget.xview)

我正在使用 python 3.7、windows 10 和 PyCharm。如果您发现此问题的重复项,请发表评论并让我在标记为重复项和/或关闭之前检查一下,因为我还没有找到答案。它有效,只是调整大小是问题所在。它应该表现得有点像 windows 带自动换行的记事本。

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

如果您希望继续使用 Grid 几何管理器创建程序,您需要熟悉 Grid.rowconfigure()Grid.columnconfigure() 函数。

这些允许您在容器小部件的范围内设置 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)

这以下面的程序结束(在做了一些更改之后我实际上可以 运行 提供的示例):

from tkinter import *
from tkinter import ttk

root = Tk()

frame = ttk.Frame(master=root)
frame.grid(sticky="NSEW")

vbar = ttk.Scrollbar(root, orient=VERTICAL)
hbar = ttk.Scrollbar(root, orient=HORIZONTAL)
vbar.grid(row=0, column=1, sticky="NS")
hbar.grid(row=1, column=0, sticky="EW")

text_widget = Text(frame, wrap=NONE, undo=True, yscrollcommand=vbar.set, xscrollcommand=hbar.set)
text_widget.grid(sticky="NSEW")

vbar.config(command=text_widget.yview)
hbar.config(command=text_widget.xview)

Grid.columnconfigure(root, 0, weight=1)
Grid.rowconfigure(root, 0, weight=1)

Grid.columnconfigure(frame, 0, weight=1)
Grid.rowconfigure(frame, 0, weight=1)

附带说明一下,使用 Pack 几何管理器重建此程序非常简单,它在调整小部件大小时(主观上)更智能:

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)