调整滚动的 Tkinter 文本框的大小 [英] Resizing a Scrolled Tkinter Text Box

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

问题描述

我想要一个滚动的 Tkinter 文本框来填充最大的分配空间.我有它的工作...

I'd like a to make a scrolled Tkinter textbox that fills the maximum alloted space. I have it working kind of...

出于某种原因,当我拉伸窗口时,文本小部件很好;但是,滚动条在 x 轴上有大量填充.

For some reason when I stretch the window the text widget is fine; However, the scroll bar gets a ton of padding on the x axis.

第二个问题是当我缩小窗口时,滚动条会移到屏幕上.

The second problem is when I shrink the window the scrollbar goes of the screen.

有人知道这两个程序的解决方案吗?

Anyone know the solutions to these two programs?

片段:

    self.Fr = Tkinter.Frame(self, width=self.Wi, height=self.He)
    self.Fr.pack(side='right', fill='both', expand='yes')


    self.Te = Tkinter.Text(self.Fr, font=self.Fo, fg=self.FG, bg=self.BG,
                           selectforeground=self.SFG,
                           selectbackground=self.SBG,
                           insertbackground=self.IBG, wrap='word',
                           undo=True, maxundo=100)
    #self.Te.grid(column=0, row=0, sticky='NSEW')
    self.Te.pack(side='left', fill='both', expand='yes')


    self.Sc = Tkinter.Scrollbar(self.Fr, elementborderwidth=1)
    #self.Sc.grid(column=1, row=0, sticky='NSEW')
    self.Sc.pack(side='right', fill='both', expand='yes')

    self.Te.configure(yscrollcommand=self.Sc.set)
    self.Sc.configure(command=self.Te.yview)

推荐答案

因为您使用了 fill='both',您的滚动条会获得所有填充.即使它是一个垂直滚动条,您也要求它沿 x 轴占用额外的空间,这会导致填充,因为滚动条本身不会拉伸以形成宽滚动条.您希望垂直滚动条仅填充 Y 方向,水平滚动条填充 X 方向.

Your scrollbar gets all the padding because you use fill='both'. Even though it's a vertical scrollbar you asked it to take up extra space along the x axis, which results in the padding since the scrollbar itself won't stretch to make a wide scrollbar. You want vertical scrollbars to only fill in the Y direction and horizontal ones to fill in the X direction.

关于滚动条离开屏幕,解释起来有点复杂,但它有一个简单的解决方案.

As to the scrollbar going off screen, that's a little complex to explain but it has a simple solution.

问题是这样的:如果你将一个由 pack 管理的窗口缩小到比内部小部件所需的小一点,它就会开始剪裁小部件.它的工作方式是按顺序处理小部件,布置窗口,然后为任何剩余的小部件分配任何剩余空间.这意味着,如果订单中较早的小部件占据了所有剩余的视觉空间,则任何后面的小部件都不会出现.

The problem is this: if you shrink a window managed by pack to a point where it's smaller than that required by the widgets inside, it starts clipping widgets. The way this works is it processes the widgets in order, laying out the window and then allocating any left-over space for any remaining widgets. This means that if a widget early in the order takes up all the remaining visual space, any later widgets will not appear.

上面提到的顺序"就是装箱单的顺序.具体来说,就是物品的包装顺序.因此,如果您打包文本小部件,然后滚动条 Tk 将首先布置文本小部件,任何剩余空间都将分配给滚动条.如果您先打包滚动条,它会被布置好,任何剩余的空间都将分配给文本小部件.

The "order" mentioned above is the order of the packing list. Specifically, the order in which items were packed. So, if you pack the text widget and then the scrollbar Tk will first lay out the text widget, and any remaining space will be allocated to the scrollbar. IF you had packed the scrollbar first, it would get laid out and any remaining space would be given to the text widget.

这一切听起来很复杂,但很酷的事情是,如果您按正确的顺序打包,一切都可以正常工作.

It all sounds very complex, but the cool thing is that if you pack things in the proper order it all just works.

然后,一般的经验法则是确保您打包的最后一个小部件是 expand 设置为 true 的小部件.这是您的弹性"小部件.这样所有固定大小的小部件将首先占用它们需要的任何空间,而您的弹性"小部件将占用剩下的所有空间.

The general rule of thumb, then, is to make sure the last widget you pack is the one with expand set to true. This is your "elastic" widget. That way all the fixed-size widgets will take up whatever space they need first, and your "elastic" widget will take up all that is left.

还有另一种解决方案,即为您的文本小部件提供所需的宽度和高度.这样,当打包程序最初分配空间时,它只会分配少量空间.因此,当窗口缩小时,文本小部件将缩小,直到缩小到那个小尺寸.但这不是很实用,因为 pack 的一大特色是您可以为所有小部件赋予其自然大小(或者,在按钮和标签的情况下,它们根据其内容假设其自然大小),而打包器会完成所有工作工作.如果您将宽度和高度设置为 1,您的初始窗口(除非明确设置为更大的尺寸)将相当小.

There is another solution which is to give your text widget a requested width and height of one. With that, when the packer initially allocates space it will allocate only a small amount of space. Thus, when the window shrinks the text widget will shrink until it gets down to that tiny size. This isn't very practical though, since one of the great features of pack is that you can give all widgets their natural size (or they assume their natural size based on their content in the case of buttons and labels) and the packer does all the work. If you set the width and height to one, your initial window (unless explicitly set to a larger size) will be rather small.

此行为全部记录在 pack 的手册页中,但您必须仔细阅读它才能完全掌握此行为.

This behavior is all documented in the man page for pack, though you have to read it really closely to fully grasp this behavior.

这篇关于调整滚动的 Tkinter 文本框的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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