Building a Basic GUI
Labels are widgets that are used to display short pieces of text. Here we create a Label that belongs to the root window—its parent widget—and we specify the text to be displayed by assigning it to the Label’s text parameter.
| import tkinter |
| |
| window = tkinter.Tk() |
| label = tkinter.Label(window, text='This is our label.') |
| label.pack() |
| |
| window.mainloop() |
Here is the resulting GUI:
Method call label.pack() is crucial. Each widget has a method called pack that places it in its parent widget and then tells the parent to resize itself as necessary. If we forget to call this method, the child widget (in this case,
Get Practical Programming, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.