< Prev  |  Home  |  Next >

Widgets

Now that we know how to create an empty window, our next job is to start adding ‘things’ to it. In Tkinter, these ‘things’ are called widgets. Let’s add a label to our window:

Run the program above, and you’ll see the following window appear:

Label widget

Notice that I’ve left out the code to change the window title and icon. Feel free to add these in yourself, I just left them out to save space, and to make the code easier to follow.

In the example above, the line:

Creates a Tkinter label, and stores it in a variable called ‘lbl’. When creating a new label, you can specify arguments as to where you want it to be placed, and how you want it to look. In this case, we want the label to be placed inside the main window (which, funnily enough, we’ve called ‘window’), and the text that you want the label to display.

The second new line:

Adds the label we have created to the specified window, in a position that it thinks best.

If we wanted to add a text entry widget and a button widget to our window, we could do something like the following:

Here’s how it looks:

Widets in a GUI

There are loads of other widgets that you can add to your GUI windows. If you’re feeling adventurous, you can Google some and see if you can add them in!

Challenge

Use widgets to create a login page to an application. It could (but doesn’t have to) look something like this:

Login GUI

Don’t worry too much about how your solution looks at this point, we’ll be covering that in the next section.

< Prev  |  Home  |  Next >