Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Handling Resizing

00:00 Handling Window Resizing. By using .columnconfigure() and .rowconfigure() on the window object, you can adjust how the rows and columns of the grid grow as the window is resized.

00:13 Remember, the grid is attached to window even though you’re calling .grid() on each Frame widget. Both .columnconfigure() and .rowconfigure() take three essential arguments.

00:24 The index of the grid column or row that you want to configure or a list of indices to configure multiple rows or columns at the same time. A keyword argument called weight that determines how the column or row should respond to window resizing relative to the other columns and rows.

00:41 A keyword argument called minsize that sets the minimum size of the row height or column width in pixels. weight is set to zero by default, which means that the column or row doesn’t expand as the window resizes.

00:55 If every column or row is given a weight of one, then they all grow at the same rate. If one column has a weight of one and another, a weight of two, then the second column expands at twice the rate of the first.

01:09 Adjust your previous script to better handle window resizing.

01:17 columnconfigure and rowconfigure are placed in the body of the outer for loop. You could explicitly configure each column and row outside of the for loop, but that would require writing an additional six lines of code. On each iteration of the loop, the i column and row are configured to have a weight of one.

01:35 This ensures that the row and column expand at the same rate whenever the window is resized. The minsize argument is set to 75 for each column and 50 for each row.

01:47 This ensures that the Label widget always displays its text without chopping off any characters, even if the window size is extremely small.

01:57 On screen, you can see the result of this code. Try it for yourself to get a feel for how it works. Play around with the weight and minsize parameters to see how they affect the grid.

02:12 By default, widgets are centered in their grid cells. This code creates two Label widgets and places them in a grid with one column and two rows.

02:22 Each grid cell is 250 pixels wide and a hundred pixels tall.

02:42 The labels are placed in the center of each cell as you can see on screen.

02:49 You can change the location of each label inside of the grid cell using the sticky parameter, which accepts a string containing one or more of the letters seen on screen.

02:59 These letters come from the cardinal directions north, south, east, and west. And note that they are not case sensitive setting sticky to N on both labels in the previous code positions each label at the top center of its grid cell.

03:19 You can see the effect of this on screen.

03:24 You can combine multiple letters in a single string to position each label in the corner of its grid cell. Here, label one is set to ne, which places the label at the top right corner of its grid cell, and label two is positioned in the bottom left corner by setting sticky to sw. On screen you can see the effect of this change.

03:49 When a widget is positioned with sticky the size of the widget itself is just big enough to contain any text and other contents inside it. It won’t fill the entire grid cell.

04:00 In order to fill the grid, you can specify ns to force the widget to fill the cell in the vertical direction or ew to fill the cell in the horizontal direction to fill the entire cell set sticky to nsew.

04:16 The code you’ll see on screen next illustrates each of these options.

05:00 On screen, you can see this code in action. This example illustrates how the .grid() geometry manager’s sticky parameter can be used to achieve the same effects as the .pack() geometry manager’s fill parameter.

05:16 The correspondence between the two parameters is shown on screen.

05:22 .grid() is a powerful geometry manager. 94 It’s often easier to understand than .pack() and is much more flexible than .place().

05:29 When you’re creating new TKinter applications you should consider using .grid() as your primary geometry manager. .grid() offers more flexibility than you’ve seen here.

05:39 And for more information on this, check out the Grid Geometry Manager section of the TkDocs tutorial linked on screen. Now that you’ve got the fundamentals of geometry managers in Tkinter, the next step is to assign actions to buttons to bring your applications to life, and that’s what you’ll be doing in the next section of the course.

Become a Member to join the conversation.