Python Tkinter - evenly scale widgets in a window

I have a small Python test application as I am learning Python and Tkinter, and I'm trying to figure out how to evenly resize the label grid in a window. I would like to make a large grid of different colored squares, so I use labels with the background color set to create the squares. I would like to resize the squares automatically when the user expands the window and then resizes and zooms out so that the size is the same as the window size is reduced.

Something like that:

Default "square" size. Opens up at this size by default.

This changes and scales evenly:

Size when the window is expanded in size.

In other words: each label should scale evenly on the window scale. I'm not even sure if I use the correct terms with the "window" and the "widgets". But I put labels on gui.

Test code that I use to try to get this to work:

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='EW') label2 = Tkinter.Label(self,anchor="center",bg="black") label2.grid(column=1,row=0,sticky='EW') label3 = Tkinter.Label(self,anchor="center",bg="red") label3.grid(column=2,row=0,sticky='EW') label4 = Tkinter.Label(self,anchor="center",bg="purple") label4.grid(column=0,row=1,sticky='EW') label5 = Tkinter.Label(self,anchor="center",bg="blue") label5.grid(column=1,row=1,sticky='EW') label6 = Tkinter.Label(self,anchor="center",bg="yellow") label6.grid(column=2,row=1,sticky='EW') self.grid_columnconfigure(0,weight=0) if __name__ == "__main__": app = simpleapp_tk(None) app.title("Test App") app.mainloop() 
+6
source share
2 answers

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

For instance:

 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) 
+8
source

Concluding the answer provided by Brian Oakley , the code for solving it in python 3 is as follows :.

Note that one parameter to control the proportion for which the window is resized sets the weight parameters for the grid_columnconfigure(1,weight=1) and grid_rowconfigure(1,weight=1) functions for 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() 
+3
source

Source: https://habr.com/ru/post/971946/


All Articles