Python Tkinter - resize widgets evenly in a window

放肆的年华 提交于 2019-11-28 12:04:38

Give all rows and columns the same non-zero weight.

For example:

self.grid_columnconfigure(0,weight=1)
self.grid_columnconfigure(1,weight=1)
self.grid_columnconfigure(2,weight=1)
self.grid_rowconfigure(0,weight=1)
self.grid_rowconfigure(1,weight=1)
aturegano

Completing the answer provided by Bryan Oakley, the code for solving it in python 3 is the following.

Note that one option to manage the proportion for which the window resizes is setting the weight parameters for functions grid_columnconfigure(1,weight=1) and grid_rowconfigure(1,weight=1) to different values.

import tkinter

class simpleapp_tk(tkinter.Tk):
    def __init__(self,parent):
        tkinter.Tk.__init__(self,parent)
        self.parent = parent
        self.initialize()

    def initialize(self):
        self.grid()

        label = tkinter.Label(self,anchor="center",bg="green")
        label.grid(column=0,row=0,sticky='NSEW')

        label2 = tkinter.Label(self,anchor="center",bg="black")
        label2.grid(column=1,row=0,sticky='NSEW')

        label3 = tkinter.Label(self,anchor="center",bg="red")
        label3.grid(column=2,row=0,sticky='NSEW')

        label4 = tkinter.Label(self,anchor="center",bg="purple")
        label4.grid(column=0,row=1,sticky='NSEW')

        label5 = tkinter.Label(self,anchor="center",bg="blue")
        label5.grid(column=1,row=1,sticky='NSEW')

        label6 = tkinter.Label(self,anchor="center",bg="yellow")
        label6.grid(column=2,row=1,sticky='NSEW')


        self.grid_columnconfigure(0,weight=1)
        self.grid_columnconfigure(1,weight=1)
        self.grid_columnconfigure(2,weight=1)
        self.grid_rowconfigure(0,weight=1)
        self.grid_rowconfigure(1,weight=1)


if __name__ == "__main__":
    app = simpleapp_tk(None)
    app.title("Test App")
    app.mainloop()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!