如何设置框架的最小和最大高度或宽度?

发布于 2024-10-07 07:02:53 字数 691 浏览 2 评论 0原文

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

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

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

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

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

并且我添加了此事件处理程序:

# 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 size of Tkinter windows can be controlled via the following methods:

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

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

@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'

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

五里雾 2024-10-14 07:02:53

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

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

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()

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()
蓝咒 2024-10-14 07:02:53

解决方法 - 至少对于最小尺寸:您可以使用网格来管理 root 中包含的框架,并通过设置 Sticky='nsew' 使它们遵循网格尺寸。然后,您可以使用 root.grid_rowconfigure 和 root.grid_columnconfigure 来设置 minsize 的值,如下所示:

from tkinter import Frame, Tk

class MyApp():
    def __init__(self):
        self.root = Tk()

        self.my_frame_red = Frame(self.root, bg='red')
        self.my_frame_red.grid(row=0, column=0, sticky='nsew')

        self.my_frame_blue = Frame(self.root, bg='blue')
        self.my_frame_blue.grid(row=0, column=1, sticky='nsew')

        self.root.grid_rowconfigure(0, minsize=200, weight=1)
        self.root.grid_columnconfigure(0, minsize=200, weight=1)
        self.root.grid_columnconfigure(1, weight=1)

        self.root.mainloop()

if __name__ == '__main__':
    app = MyApp()

但正如 Brian 所写(2010 年:D),如果您不限制其 minsize,您仍然可以将窗口大小调整为小于框架。

A workaround - at least for the minimum size: You can use grid to manage the frames contained in root and make them follow the grid size by setting sticky='nsew'. Then you can use root.grid_rowconfigure and root.grid_columnconfigure to set values for minsize like so:

from tkinter import Frame, Tk

class MyApp():
    def __init__(self):
        self.root = Tk()

        self.my_frame_red = Frame(self.root, bg='red')
        self.my_frame_red.grid(row=0, column=0, sticky='nsew')

        self.my_frame_blue = Frame(self.root, bg='blue')
        self.my_frame_blue.grid(row=0, column=1, sticky='nsew')

        self.root.grid_rowconfigure(0, minsize=200, weight=1)
        self.root.grid_columnconfigure(0, minsize=200, weight=1)
        self.root.grid_columnconfigure(1, weight=1)

        self.root.mainloop()

if __name__ == '__main__':
    app = MyApp()

But as Brian wrote (in 2010 :D) you can still resize the window to be smaller than the frame if you don't limit its minsize.

世俗缘 2024-10-14 07:02:53
# this code works for me

import tkinter as tk


class tk(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.pack()


window = tk()
window.master.title("window")
window.master.minsize(100, 100)
window.master.maxsize(300, 300)
window.mainloop()
# this code works for me

import tkinter as tk


class tk(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.pack()


window = tk()
window.master.title("window")
window.master.minsize(100, 100)
window.master.maxsize(300, 300)
window.mainloop()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文