01 Introduction To TKinter - Windows, Labels, Entry Boxes, Buttons and Text Boxes
01 Introduction To TKinter - Windows, Labels, Entry Boxes, Buttons and Text Boxes
Introduction to TKinter
Programming Guides
www.computerscienceuk.com
ComputerScienceUK Programming Guide – Python & Tkinter
Activity 1
3 minutes
What is a GUI?
www.computerscienceuk.com
ComputerScienceUK Programming Guide – Python & Tkinter
In order to make use of the tkinter library it first needs to be imported. To import
the library simply write the following at the top of your program code:
www.computerscienceuk.com
ComputerScienceUK Programming Guide – Python & Tkinter
This object doesn’t have to be called ‘window’, but it makes sense to do so. Also, notice that the
object is being created from the class Tk() where the ‘T’ is capital.
www.computerscienceuk.com
ComputerScienceUK Programming Guide – Python & Tkinter
As you can see, this code has added a title to the bar at the top of the window:
www.computerscienceuk.com
ComputerScienceUK Programming Guide – Python & Tkinter
In order for our window to stay on the screen and not immediately close once the code to
create the window has been executed, we run a method of our window object called
“mainloop()” at the end of our program code.
To do this we write the name of our window object (in this case ‘window’) followed by
‘.mainloop()’.
www.computerscienceuk.com
ComputerScienceUK Programming Guide – Python & Tkinter
In programming, we call the different items that you might find in a GUI, “widgets”. The
main widgets that GUIs will contain are ‘labels’, ‘buttons’, ‘text boxes’, ‘drop down
menus’, ‘check boxes’ and ‘radio buttons’.
The next few slides will show how some of these widgets can be created and positioned in
our new GUI window.
www.computerscienceuk.com
ComputerScienceUK Programming Guide – Python & Tkinter
With the tkinter library, the window is arranged into a grid with as many columns and rows as we
require. Each row and column is numbered so that the cells can be referenced by coordinates.
As is common practice in computing, the coordinates start (0,0) from the TOP LEFT.
www.computerscienceuk.com
ComputerScienceUK Programming Guide – Python & Tkinter
We do this by using the ‘sticky’ property. We can get the widgets to stick to either the top,
bottom, left or right of the cell. We do this using the compass directions north (N), south
(S), east (E) and west (W). We use capital N,S,E and W when using the ‘sticky’ command.
You can further improve the control you have in positioning widgets and the general
layout of a window by using a frame widget. We look at frame widgets later.
www.computerscienceuk.com
ComputerScienceUK Programming Guide – Python & Tkinter
To create a label we use the ‘Label’ command (notice the capital ‘L’). We then say where we want it
to be placed (in this case it is inside the window object) and then we write the text that the label is to
contain. Finally we use the ‘grid’ method to position the widget inside our window in a location of
our choice.
Here is an example:
www.computerscienceuk.com
ComputerScienceUK Programming Guide – Python & Tkinter
Activity 2
10 minutes Create your first GUI
www.computerscienceuk.com
ComputerScienceUK Programming Guide – Python & Tkinter
This widget provides a place to enter text. It could be added the same way as the label widget.
However, as entry widgets are usually a place for the user to enter different inputs, we will often want
to refer back to entry boxes later. Because of this, it is usually best to assign an ‘Entry’ widget to a
variable name so that we can call on it elsewhere in our code.
Here is an example of creating an entry widget, assigning it to a variable and then positioning this
variable using the grid() method.
www.computerscienceuk.com
ComputerScienceUK Programming Guide – Python & Tkinter
www.computerscienceuk.com
ComputerScienceUK Programming Guide – Python & Tkinter
What is needed next to “command=” is the name of a function. This is because when a button is
clicked it needs to execute some code and so by adding the name of a function here, will allow the
button to execute a function when it is clicked.
So, let’s create a function which will ‘collect’ the text that a user might write into the ‘Entry’ box:
www.computerscienceuk.com
ComputerScienceUK Programming Guide – Python & Tkinter
www.computerscienceuk.com
ComputerScienceUK Programming Guide – Python & Tkinter
Like we did for the entry widget, it is best to assign text widgets to variables as often we will want to
call on text widgets so that they can be updated, when a button is clicked, to display new text.
Notice also the ability to set height and width, to allow text to wrap onto another line when the
window is resized and the option to set a background colour.
On the grid method, notice the ‘columnspan’ property. This allows widgets to be positioned across
columns, which may be a useful option when trying to arrange widgets more professionally.
www.computerscienceuk.com
ComputerScienceUK Programming Guide – Python & Tkinter
www.computerscienceuk.com
ComputerScienceUK Programming Guide – Python & Tkinter
Previously we created a function to be executed when the button was clicked. The function collected
the text that was entered into the ‘entry’ box. Now that we have a text box, we can update the function
so that it can manipulate and output the inputted text.
www.computerscienceuk.com
ComputerScienceUK Programming Guide – Python & Tkinter
Activity 3
10 minutes Create your first GUI
www.computerscienceuk.com
ComputerScienceUK Programming Guide – Python & Tkinter
Activity 4
45 minutes
www.computerscienceuk.com