tkinter框架网格未按照网格配置进行调整

时间:2018-12-31 14:22:00

标签: python-3.x tkinter tkinter-layout

.gridcolumnconfig对输出无效:

我试图了解tkinter中的网格配置。在下面的示例代码中,无论我在.gridcolumnconfig中给出的权重为多少,输出均无效。请帮助我了解.gridcolumnconfig.gridrowconfig的工作方式。

预先感谢

    import tkinter

    class Frame_window(tkinter.Frame):
        def __init__(self, p, bg, r=0, c=0, bw=1):
            super().__init__(p)
            self.grid(row=r, column=c, sticky='nsew')
            self.config(bg=bg)
            self['borderwidth'] = bw
            self['relief']='sunken'



    root = tkinter.Tk()


    text = f'Welcome to login @page'
    root.title(text)
    root.minsize(480, 240)
    root.grid_columnconfigure(0, weight=1)
    root.grid_rowconfigure(0, weight=1)

    mainframe = Frame_window(root, 'light yellow')
    mainframe.grid_rowconfigure(0, weight=2)
    mainframe.grid_rowconfigure(1, weight=1)
    mainframe.grid_rowconfigure(2, weight=1)
    mainframe.grid_rowconfigure(3, weight=2)

    mainframe.grid_columnconfigure(0, weight=2)
    mainframe.grid_columnconfigure(1, weight=1)
    mainframe.grid_columnconfigure(2, weight=4)
    mainframe.grid_columnconfigure(3, weight=4)

    testfram1 = Frame_window(root, 'sky blue', 0, 0, 5)
    tkinter.Canvas(testfram1).grid(sticky='nsew')
    testfram2 = Frame_window(root, 'sky blue', 0, 1, 5)
    tkinter.Canvas(testfram2).grid(sticky='nsew')
    testfram3 = Frame_window(root, 'sky blue', 0, 2, 5)
    tkinter.Canvas(testfram3).grid(sticky='nsew')
    testfram4 = Frame_window(root, 'sky blue', 0, 3, 5)
    tkinter.Canvas(testfram4).grid(sticky='nsew')
    testfram5 = Frame_window(root, 'sky blue', 1, 0, 5)
    tkinter.Canvas(testfram5).grid(sticky='nsew')
    testfram6 = Frame_window(root, 'sky blue', 1, 1, 5)
    tkinter.Canvas(testfram6).grid(sticky='nsew')
    testfram7 = Frame_window(root, 'sky blue', 1, 2, 5)
    tkinter.Canvas(testfram7).grid(sticky='nsew')
    testfram8 = Frame_window(root, 'sky blue', 1, 3, 5)
    tkinter.Canvas(testfram8).grid(sticky='nsew')
    testfram9 = Frame_window(root, 'sky blue', 2, 0, 5)
    tkinter.Canvas(testfram9).grid(sticky='nsew')
    testfram10 = Frame_window(root, 'sky blue', 2, 1, 5)
    tkinter.Canvas(testfram10).grid(sticky='nsew')
    testfram11 = Frame_window(root, 'sky blue', 2, 2, 5)
    tkinter.Canvas(testfram11).grid(sticky='nsew')
    testfram12 = Frame_window(root, 'sky blue', 2, 3, 5)
    tkinter.Canvas(testfram12).grid(sticky='nsew')

    root.mainloop()

我希望网格布局可以根据我提供的权重比例进行调整。

1 个答案:

答案 0 :(得分:1)

您正在将所有权重与mainframe关联,但是您将所有帧都放在root中。 rowconfigurecolumnconfigure方法仅影响小部件内的子级。由于mainframe中没有其他内容,因此权重不起作用。