如何设置框架的最小和最大高度或宽度? [英] How to set the min and max height or width of a Frame?

查看:56
本文介绍了如何设置框架的最小和最大高度或宽度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Tkinter 窗口的大小可以通过以下方法控制:

The size of Tkinter windows can be controlled via the following methods:

.minsize()
.maxsize()
.resizable()

是否有等效的方法来控制 Tkinter 或 ttk Frames 的大小?

Are there equivalent ways to control the size of Tkinter or ttk Frames?

@Bryan:我将您的 frame1.pack 代码更改为以下内容:

@Bryan: I changed your frame1.pack code to the following:

frame1.pack(fill='both', expand=True)
frame1.bind( '<Configure>', maxsize )

我添加了这个事件处理程序:

And I added this event handler:

# attempt to prevent frame from growing past a certain size
def maxsize( event=None ):
    print frame1.winfo_width()
    if frame1.winfo_width() > 200:
        print 'frame1 wider than 200 pixels'
        frame1.pack_propagate(0)
        frame1.config( width=200 )
        return 'break'

上述事件处理程序检测到框架的宽度太大,但无法阻止尺寸增加的发生.这是 Tkinter 的限制还是我误解了您的解释?

The above event handler detects that a frame's width is too big, but is unable to prevent the increase in size from happening. Is this a limitation of Tkinter or have I misunderstood your explanation?

推荐答案

没有单一的魔法函数可以将框架强制为最小或固定大小.但是,您当然可以通过为框架指定宽度和高度来强制设置框架的大小.然后,您可能还需要做两件事:当您将此窗口放入容器中时,您需要确保几何管理器不会缩小或扩展窗口.第二,如果框架是其他小部件的容器,请关闭网格或包传播,以便框架不会缩小或扩展以适应其自身的内容.

There is no single magic function to force a frame to a minimum or fixed size. However, you can certainly force the size of a frame by giving the frame a width and height. You then have to do potentially two more things: when you put this window in a container you need to make sure the geometry manager doesn't shrink or expand the window. Two, if the frame is a container for other widget, turn grid or pack propagation off so that the frame doesn't shrink or expand to fit its own contents.

但是请注意,这不会阻止您将窗口大小调整为小于内部框架.在这种情况下,框架将被剪裁.

Note, however, that this won't prevent you from resizing a window to be smaller than an internal frame. In that case the frame will just be clipped.

import Tkinter as tk

root = tk.Tk()
frame1 = tk.Frame(root, width=100, height=100, background="bisque")
frame2 = tk.Frame(root, width=50, height = 50, background="#b22222")

frame1.pack(fill=None, expand=False)
frame2.place(relx=.5, rely=.5, anchor="c")

root.mainloop()

这篇关于如何设置框架的最小和最大高度或宽度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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