【问题标题】:tkinter Frame grids are not adjusting as per grid configtkinter 框架网格未根据网格配置进行调整
【发布时间】:2018-12-31 14:22:00
【问题描述】:

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

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

【问题讨论】:

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


    【解决方案1】:

    您将所有权重与mainframe 相关联,但您将所有帧放入rootrowconfigurecolumnconfigure 方法仅影响小部件内的子级。由于mainframe 中没有其他内容,因此权重没有任何作用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-16
      • 1970-01-01
      • 2018-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-02
      相关资源
      最近更新 更多