![]() Grid is in many cases the best choice for general use. Though grid is easier to learn and to use and produces nicer layouts, lots of developers keep using pack. Grid was introduced in 1996 as an alternative to pack. The algorithmic behaviour of pack is not easy to understand and it can be difficult to change an existing design. The first geometry manager of Tk had been pack. If the brightness is less than 120, we set the foreground colour (fg) of the label to White otherwise to black, so that the text can be easier read. We calculate the brightness (grey value) of each colour. we assign to every label a different colour, which we randomly create using the randrange method of the random module. We are playing around with colours in this example, i.e. We use the place geometry manager in the following example. ![]() It can be applied to all standard widgets. The place manager can be accessed through the place method. The Place geometry manager allows you explicitly set the position and size of a window, either in absolute terms, or relative to another window. If we change LEFT to RIGHT in the previous example, we get the colours in reverse order: W = tk.Label(root, text="blue", bg="blue", fg="white") W = tk.Label(root, text="green", bg="green", fg="black") W = tk.Label(root, text="red", bg="red", fg="white") The corresponding code looks like this: import tkinter as tk We want to place the three label side by side now and shorten the text slightly: We will change the last label of our previous example to ipady=10. In the following example, we change only the label with the text "Green Grass", so that the result can be easier recognized. The code for the window above: import tkinter as tk internal and external padding and padding in x and y direction: padx The pack() manager knows four padding options, i.e. If you want to make the widgets as wide as the parent widget, you have to use the fill=X option: import tkinter as tk Furthermore, we can see that each label has been given the size of the text. As you can see, it has chosen to place the label widgets on top of each other and centre them. So pack had to decide which way to arrange the labels. In our example, we have packed three labels into the parent widget "root". W = tk.Label(root, text="Blue Sky", bg="blue", fg="white") W = tk.Label(root, text="Green Grass", bg="green", fg="black") ![]() W = tk.Label(root, text="Red Sun", bg="red", fg="white") For example simple applications like placing a number of widgets side by side, or on top of each other. For simple applications it is definitely the manager of choice. ![]() Though the pack command is easier to use, this layout managers is limited in its possibilities compared to the grid and place mangers. The pack command takes care of the details. Instead of having to declare precisely where a widget should appear on the display screen, we can declare the positions of widgets with the pack command relative to each other. Pack is the easiest to use of the three geometry managers of Tk and Tkinter. Enjoying this page? We offer live Python training courses covering the content of this site.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |