tkinter调整窗口大小而滚动条不消失 [英] tkinter resize window without scrollbar disappearing

查看:73
本文介绍了tkinter调整窗口大小而滚动条不消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have been coding an application which is pretty much a fancy text editor, and I have been getting problems with resizing the widgets inside it. My goal is for it to look a bit like IDLE with a horizontal scrollbar as well.

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 记事本.

I am using python 3.7, windows 10 and PyCharm. If you find a duplicate of this question, please comment and let me check it out before marking as a duplicate and or closing, for i have yet to find an answer. it works, just resizing is the issue. it should behave a bit like windows notepad with word wrap off.

推荐答案

这是一个比较容易解决的问题.

This is a relatively simple problem to solve.

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

If you wished to continue creating the program using the Grid geometry manager, you will need to familiarise yourself with the Grid.rowconfigure() and Grid.columnconfigure() functions.

这些允许您在容器小部件的范围内为 Grid 几何管理器设置配置选项.具体来说,我们关心的属性是weight.正如此处所述:

These allow you to set the configuration options for the Grid geometry manager within the confines of a container widget. Specifically, the attribute we care about is weight. As explained here:

每一列和每一行都有一个与之关联的权重"网格选项,它告诉它如果母版中有额外的空间来填充它应该增长多少.默认情况下,每列或每行的权重为 0,表示不扩展以填充空间.

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.

为了随后调整用户界面的大小,我们需要为要扩展的列赋予正权重.这是使用网格的columnconfigure"和rowconfigure"方法完成的.如果两列具有相同的权重,它们将以相同的速率扩展;如果一个权重为 1,另一个权重为 3,则后一个将每添加一个像素扩展三个像素.

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 小部件不会随着滚动条展开.其次,我们需要将以下代码段添加到程序中:

So, in this case we need to make a few changes. Firstly, we need to add sticky = "NESW" to both the frame.grid() and text_widget.grid() calls, otherwise the Text widget won't expand with the scrollbars. Secondly, we need to add the below snippet to the program:

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

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

最终得到以下程序(进行一些更改后,我可以实际运行提供的示例):

This ends up with the below program (after making some changes so I can actually run the provided example):

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 几何管理器,在调整小部件大小时(主观上)更智能:


As a side note, it would be very simple to rebuild this program using the Pack geometry manager which is (subjectively) more intelligent when it comes to resizing widgets:

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)

这篇关于tkinter调整窗口大小而滚动条不消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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