Diferències
Ací es mostren les diferències entre la revisió seleccionada i la versió actual de la pàgina.
| info:cursos:pue:python-pcpp1:m3:1.3 [22/12/2023 12:59] – creat mate | info:cursos:pue:python-pcpp1:m3:1.3 [23/12/2023 19:49] (actual) – mate | ||
|---|---|---|---|
| Línia 74: | Línia 74: | ||
| You're not obliged to declare the number of rows and columns in advance – '' | You're not obliged to declare the number of rows and columns in advance – '' | ||
| + | The most commonly used '' | ||
| + | |||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | |||
| + | Let's see them all in action. | ||
| + | |||
| + | <code python> | ||
| + | import tkinter as tk | ||
| + | |||
| + | window = tk.Tk() | ||
| + | button_1 = tk.Button(window, | ||
| + | button_2 = tk.Button(window, | ||
| + | button_3 = tk.Button(window, | ||
| + | button_1.grid(row=0, | ||
| + | button_2.grid(row=1, | ||
| + | button_3.grid(row=2, | ||
| + | window.mainloop() | ||
| + | |||
| + | </ | ||
| + | |||
| + | Analyze the snippet in the editor and determine the resulting number of columns and rows. That's essential if you want to imagine the resulting window' | ||
| + | |||
| + | Are you ready to solve the puzzle? Did you imagine the window that way? | ||
| + | |||
| + | {{ : | ||
| + | |||
| + | We're sure you did. Look. The window is divided into **nine cells**: three rows and three columns. The buttons are settled on the **grid' | ||
| + | |||
| + | Now we’re going to affect the buttons’ relation to the cells’ boundaries. | ||
| + | |||
| + | Can you see what we changed in the code? | ||
| + | |||
| + | <code python> | ||
| + | import tkinter as tk | ||
| + | |||
| + | window = tk.Tk() | ||
| + | button_1 = tk.Button(window, | ||
| + | button_2 = tk.Button(window, | ||
| + | button_3 = tk.Button(window, | ||
| + | button_1.grid(row=0, | ||
| + | button_2.grid(row=1, | ||
| + | button_3.grid(row=2, | ||
| + | window.mainloop() | ||
| + | |||
| + | </ | ||
| + | |||
| + | Yes, we modified the third '' | ||
| + | |||
| + | We admit that this puzzle is somewhat harder than the previous ones. Don't rush through this – think it over carefully. | ||
| + | |||
| + | Here's the solution. | ||
| + | |||
| + | {{ : | ||
| + | |||
| + | Note: the manager noticed that the total number of columns is actually two, not three as in the previous code. This is why the window looks different. | ||
| + | |||
| + | The third, fully automatic geometry manager is named '' | ||
| + | |||
| + | Let's take a look at it. | ||
| + | |||
| + | The default pack's operation tends to deploy all subsequent widgets in one column, one below the other. You can change this behavior to a limited extent by using the following parameters: | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | |||
| + | We want to warn you that the results produced by '' | ||
| + | |||
| + | We suggest you use it only as a temporary solution to help you get a working application quickly, but if you want your application to look nice and to be legible and clear (of course, you would want that!) you'd better forget about '' | ||
| + | |||
| + | Let '' | ||
| + | |||
| + | <code python> | ||
| + | import tkinter as tk | ||
| + | |||
| + | |||
| + | window = tk.Tk() | ||
| + | button_1 = tk.Button(window, | ||
| + | button_2 = tk.Button(window, | ||
| + | button_3 = tk.Button(window, | ||
| + | button_1.pack() | ||
| + | button_2.pack() | ||
| + | button_3.pack() | ||
| + | window.mainloop() | ||
| + | |||
| + | </ | ||
| + | |||
| + | As you can see, using '' | ||
| + | |||
| + | Let's look at the window we get. The window looks different. | ||
| + | |||
| + | {{ : | ||
| + | |||
| + | Very different. For example, the window fits its size to the area occupied by the widgets. The buttons are located one after the other, from top to bottom. | ||
| + | |||
| + | Let's play a little game with '' | ||
| + | |||
| + | We've ordered '' | ||
| + | |||
| + | <code python> | ||
| + | import tkinter as tk | ||
| + | |||
| + | window = tk.Tk() | ||
| + | button_1 = tk.Button(window, | ||
| + | button_2 = tk.Button(window, | ||
| + | button_3 = tk.Button(window, | ||
| + | button_1.pack(side=tk.RIGHT) | ||
| + | button_2.pack() | ||
| + | button_3.pack() | ||
| + | window.mainloop() | ||
| + | |||
| + | </ | ||
| + | |||
| + | Can you predict the window' | ||
| + | |||
| + | Is this what you expected? | ||
| + | |||
| + | {{ : | ||
| + | |||
| + | No? Are you surprised? You have the right to be. Pack is the **least intuitiv**e geometry manager for sure, and you really need to spend some time testing its whims. | ||
| + | |||
| + | We have one more experiment left to carry out. | ||
| + | |||
| + | Note: we want the '' | ||
| + | |||
| + | This puzzle is a bit easier than the previous one. Think for a moment. | ||
| + | |||
| + | <code python> | ||
| + | import tkinter as tk | ||
| + | |||
| + | window = tk.Tk() | ||
| + | button_1 = tk.Button(window, | ||
| + | button_2 = tk.Button(window, | ||
| + | button_3 = tk.Button(window, | ||
| + | button_1.pack(side=tk.RIGHT, | ||
| + | button_2.pack() | ||
| + | button_3.pack() | ||
| + | window.mainloop() | ||
| + | |||
| + | </ | ||
| + | |||
| + | Yes, you're right – this is the expected answer. | ||
| + | |||
| + | {{ : | ||
| + | |||
| + | We think that there is one intriguing question that can be asked here and now: do these buttons have to be **gray**? It's boring. Very boring. | ||
| + | |||
| + | We're going to clear up this issue soon | ||