简体   繁体   English

在tkinter中扩展多个框架

[英]Expanding with multiple frames in tkinter

I am trying to get the equivalent of the red box on the bottom(in my real program it is labels and checkboxes) to expand to the size of the green box on top(which is also checkboxes in my real program). 我试图使底部的红色框(在我的真实程序中是标签和复选框)等效,以扩展到顶部的绿色框(在我的真实程序中,也是复选框)的大小。 Fill and expand both seem to be not working. 填充和扩展都似乎无法正常工作。 I can't just change the widths either. 我也不能只改变宽度。 What am i doing wrong? 我究竟做错了什么?

try:
    from tkinter import *
except ImportError:
    from Tkinter import *

class Application(Frame):
    def __init__(self,master=None):
        root = Tk()
        Frame.__init__(self,master)

        topframe = Frame(self)
        botframe = Frame(self)

        Frame(topframe, background="green", width=300, height = 200).grid(row =0, column = 0)
        Frame(botframe, background="red", width=200, height = 200).grid(row =1, column = 0, sticky = E)

        topframe.pack(side = TOP)
        botframe.pack(side = BOTTOM, fill = BOTH, expand = True)

        self.pack(fill="both", expand=True)
        root.mainloop()
Application()

I fixed it, take a look if you have a similar problem 我解决了,看看是否有类似的问题

try:
    from tkinter import *
except ImportError:
    from Tkinter import *

class Application(Frame):
    def __init__(self,master=None):
        root = Tk()
        Frame.__init__(self,master)

        topframe = Frame(root)
        botframe = Frame(root)

        topframe.grid(row =0, column = 0, sticky=N+E+W)
        botframe.grid(row =1, column = 0, sticky=S+E+W)

        topframe.columnconfigure(0, weight=1)
        botframe.columnconfigure(0, weight=1)

        Frame(topframe, background="green", width=300, height = 200).grid(row =0, column = 0, sticky=N+E+W)
        Frame(botframe, background="red", width=200, height = 200).grid(row =1, column = 0, sticky = S+E+W)

        root.mainloop()
Application()

When you use grid, a rule of thumb is that you should grid_rowconfigure and grid_columnconfigure to give at least one row and one column a non-zero weight. 使用网格时,经验法则是您应该grid_rowconfiguregrid_columnconfigure给至少一行和一列赋予非零权重。 This will cause that row and that column to "take up the slack" in a layout. 这将导致该行和该列在布局中“占据余地”。

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

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