简体   繁体   English

将小部件放置在框架内时如何保持框架尺寸?

[英]How to keep frame sizes when widgets are placed inside frames?

In an effort to learn Tkinter, I've put together the program below. 为了学习Tkinter,我在下面整理了程序。 When I leave the button out of the interface the two frames are the same size when displayed. 当我将按钮移出界面时,显示时两个框架的大小相同。 However, if the button is placed in either frame, the frame with the button shrinks. 但是,如果将按钮放在任一框架中,则具有按钮的框架会缩小。

import tkinter as tk
def main():
    main_win = tk.Tk()
    main_win.title('just playing around')
    main_win.geometry('350x450')

    main_win.rowconfigure(0, weight=1)
    main_win.rowconfigure(1, weight=1)
    main_win.columnconfigure(0, weight=1)
    main_win.columnconfigure(1, weight=1)
    main_win.columnconfigure(2, weight=1)

    frm1 = tk.Frame(main_win, bg='red', height=100, width=350)

    frm1.grid(row=0, sticky='nsew')
    frm1.rowconfigure(0, weight=1)
    frm1.rowconfigure(1, weight=1)
    frm1.rowconfigure(2, weight=1)
    frm1.columnconfigure(0, weight=1)

    frm2 = tk.Frame(main_win, bg='green', height=150, width=350)
    frm2.grid(row=1, sticky='nsew')
    frm2.rowconfigure(0, weight=1)
    frm2.rowconfigure(1, weight=1)
    frm2.rowconfigure(2, weight=1)
    frm2.columnconfigure(0, weight=1)

    quit_btn = tk.Button(frm1, bg='lightgrey', text='Quit',
                         command=main_win.destroy)
    quit_btn.grid(row=1)

    main_win.mainloop()

    return 0

if __name__ == '__main__':
    main()

Why is this happening, and what is needed to keep the frames the same size? 为什么会发生这种情况?保持框架大小相同又需要什么?

(Also, resizing the window causes the frames to resize vertically, but not horizontally. Why is that happening and how do I make the resizing happen in both directions (or turn it off in both directions)?) (此外,调整窗口大小会导致框架在垂直方向上调整大小,而在水平方向上不会调整。为什么会发生这种情况,以及如何在两个方向(或在两个方向都将其关闭)上进行大小调整?)

So because tkinter by default will force a frame to resize to the widgets even if you give it a size as soon as you place a widget in the frame it will resize to the widget. 因此,由于默认情况下tkinter会强制将框架调整为窗口小部件的大小,即使您在框架中放置窗口小部件后立即为其设置大小,也会将其调整为窗口小部件的大小。 Instead we need to tell tkinter to not allow propagation. 相反,我们需要告诉tkinter不允许传播。 So if we disable propagation inside of the frame that holds the button it will no longer resize to fit the button. 因此,如果我们禁用包含按钮的框架内的传播,它将不再调整大小以适合按钮。

For the issue of not expanding to the right when you resize that is because of the weights on column 1 and 2. Unless you have plans to add something to those columns you need to remove the weights for those columns. 由于由于第1列和第2列的权重而导致在调整大小时无法向右扩展的问题。除非计划在这些列中添加内容,否则需要删除这些列的权重。

Because you add a weight to all 3 columns the columns will resize at the same rate in the container. 因为您对所有3列都添加了权重,所以列的大小将在容器中以相同的比率调整。 In this case because column 1 and 2 start out at size zero when you start to resize the window you will notice column 0 is resizing but not fast enough to fill the window. 在这种情况下,因为当您开始调整窗口大小时,第1列和第2列的大小为零,所以您会注意到第0列正在调整大小,但速度不足以填充窗口。 This is because column 1 and 2 are also growing at the same rate as column 0 due to the weights being set. 这是因为由于设置了权重,第1列和第2列的增长速度也与第0列相同。

So to answer your comment you can keep the weights for column 1 and 2 if you plan on using those columns later and want those columns to also grow with the screen. 因此,要回答您的评论,如果您计划以后使用这些列,并且希望这些列也随屏幕增加,则可以保留第1列和第2列的权重。

Try this code and let me know if you have any questions. 尝试使用此代码,如果您有任何疑问,请告诉我。

import tkinter as tk


def main():
    main_win = tk.Tk()
    main_win.title('just playing around')
    main_win.geometry('350x450')

    # main_win.rowconfigure(0, weight=1) # disable this
    main_win.rowconfigure(1, weight=1)
    main_win.columnconfigure(0, weight=1)
    # main_win.columnconfigure(1, weight=1) # disable this
    # main_win.columnconfigure(2, weight=1) # disable this

    frm1 = tk.Frame(main_win, bg='red', height=100, width=350)
    frm1.grid(row=0, column=0, sticky='nsew')

    frm1.grid_propagate(False) # add grid_propagate(False) after your grid placement.

    frm1.rowconfigure(0, weight=1)
    frm1.rowconfigure(1, weight=1)
    frm1.rowconfigure(2, weight=1)
    frm1.columnconfigure(0, weight=1)

    frm2 = tk.Frame(main_win, bg='green', height=150, width=350)
    frm2.grid(row=1, column=0, sticky='nsew')
    frm2.rowconfigure(0, weight=1)
    frm2.rowconfigure(1, weight=1)
    frm2.rowconfigure(2, weight=1)
    frm2.columnconfigure(0, weight=1)

    quit_btn = tk.Button(frm1, bg='lightgrey', text='Quit', command=main_win.destroy)
    quit_btn.grid(row=1)
    main_win.mainloop()


if __name__ == '__main__':
    main()

Results: 结果:

As you can see I can resize the bottom frame without the top frame resizing vertically and both frames expand horizontally now. 如您所见,我可以调整底部框架的大小,而不必垂直调整顶部框架的大小,现在两个框架都可以水平扩展。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM