Aquesta és una revisió antiga del document —-
1.3 Settling widgets in the window's interior
Settling widgets
A familiarity with the Button widget allows us to show you some ways of putting the widgets (not only the buttons) inside windows. There are more of them than just place(), which you learned about in the previous section. To be precise, there are three different methods.
These methods are implemented by geometry managers.
Place is the most detailed one. It forces you to precisely declare a widget's location, pixel by pixel. It won't, however, protect you from some common mistakes causing the widgets to overlap each other or to place some of them, partially or fully, outside the window.
If you don't want to deploy the widgets manually and worry about possible conflicts and failures, you may entrust the whole problem to tkinter. It'll try to guess your intentions and to find the best location for each widget. Unfortunately, its assumptions may not live up to your expectations, and the final result can be really disappointing. This method of settling widgets is implemented by the pack geometry manager.
The grid geometry manager is in the middle, in between the other two geometry managers. It gives you a chance to express your general wishes and tries to deploy the widgets according to them. Note the word general – they aren't as precise as the ones used by place, but are far more detailed than those utilized by pack.
There is one very important aspect of the issue that must be mentioned here: these managers cannot be mixed. Only one of them can be used in one application, unless you want to turn your window into a big mess.
We're talking seriously about it. Don't take it as a joke.
The place geometry manager demands the usage of the place() method. Note: the method is invoked from within the widget's object, not the window, as the widget is always aware of the window it belongs to (it gets the information from the constructor's very first argument).
The most usable place() method parameters are as follows (all of them are passed as keyword arguments):
height=h– the widget's desired height measured in pixels; if the parameter is omitted, the widget's height will be determined automatically;width=w– the widget's desired width measured in pixels; if the parameter is omitted, the widget's width will be determined automatically;x=x– the widget's top-left pixel's horizontal coordinate measured relative to the home window's top-left corner;y=y– the widget's top-left pixel's vertical coordinate measured relative to the home window's top-left corner.
Let's see them all in action.
import tkinter as tk window = tk.Tk() button_1 = tk.Button(window, text="Button #1") button_2 = tk.Button(window, text="Button #2") button_3 = tk.Button(window, text="Button #3") button_1.place(x=10, y=10) button_2.place(x=20, y=40) button_3.place(x=30, y=70) window.mainloop()
The snippet we've prepared for you shows how the place() method works.
It places three buttons in a cascade-like order. Try to guess what these buttons will look like inside the window. Yes, this is what we expected, isn't it?
Now let's play with width and height for a moment.
Look, we've added some arguments to the previous snippet. Two buttons (b1 and b2) should look different now – can we be sure of it?
import tkinter as tk window = tk.Tk() button_1 = tk.Button(window, text="Button #1") button_2 = tk.Button(window, text="Button #2") button_3 = tk.Button(window, text="Button #3") button_1.place(x=10, y=10, width=150) button_2.place(x=20, y=40) button_3.place(x=30, y=70, height=50) window.mainloop()
Yes, we can!
As you can see, using place() gives you full control over the window's image. There is only one important but — full control means full responsibility. Sometimes it's better to share the responsibility among two parts – e.g., you and the grid() geometry controller.
grid() sees the window's area as a… grid. This means that the whole of the window's interior is divided into a number of columns of equal width and a number of rows of equal height.
The grid itself is not visible – the distribution is modeled inside the manager and you are only able to know its effects i.e., the widget's final arrangement.
You're not obliged to declare the number of rows and columns in advance – grid() finds the proper numbers for you. Let's try it.
