TKinter · Part 2 of 2
Tkinter GUIs: windows, labels, entry boxes and buttons
Build your first Python GUI with Tkinter, from an empty window to a login form and a working register.
Build your first Python GUI with Tkinter, from an empty window to a login form and a working register. This lesson joins videos 48 to 51 of my WJEC GCSE Computer Science Unit 2 Python series into one walkthrough.
You will create a window with tk.Tk(), title() and geometry() and keep it open with mainloop(). Then you add labels and style them with font, fg and bg, read, clear and fill Entry boxes with get(), delete() and insert(), hide a password with show="*", and make buttons run a function with command=.
Chapters
0:00 Introduction
0:35 Your first window
2:30 Labels
4:42 Entry boxes
7:23 Buttons
9:37 Summary
Each part ends with a "Your turn" task, so pause and try it before the next chapter.
From section G, Graphical user interfaces with Tkinter (specification references 2.2.1 and 2.3.1).
The narration uses an AI clone of my own voice.
Transcript
- Tkinter GUIs.
- In this video, you will build your first Python programs with windows, labels, boxes
- and buttons, using Tkinter.
- There are 4 parts. First, you will create a window and give it a title and a size.
- Next, you will add labels and change their font and colour.
- Then you will read, clear and fill entry boxes, and hide a password.
- Finally, you will make buttons run a function when they are clicked.
- Each part ends with a task for you to try.
- Pause the video when you get there, and try it before you move on.
- Part 1. Your first window. You will create a Tkinter window, give it a title and a
- size, and keep it open with mainloop.
- Click File, then New File.
- Type import tkinter as tk. This loads Tkinter, and tk
- is the short name you use for it from now on.
- Next, type window equals tk.Tk, with a capital T, followed by empty
- brackets. This creates the main window.
- Add window.title, and in brackets and speech marks, Pentre Bach Pre-School.
- This sets the text in the title bar.
- Add window.geometry, and in brackets and speech marks, 400, small x,
- 300. This makes the window 400 pixels wide and 300 pixels high.
- Use a small letter x, not a times sign, and no spaces.
- Add window.mainloop, with empty brackets, as the last line.
- mainloop keeps the window open until the user closes it.
- Without it, the window opens and closes straight away.
- Click File, then Save As, and save the file as
- first_window.py.
- Press F5 to run it. An empty grey window opens, 400 pixels wide and
- 300 pixels high, with Pentre Bach Pre-School in its title bar.
- Nothing appears in the Shell.
- Change the numbers in geometry to 600, small x, 200.
- Save and press F5 again. This time you see a wider, shorter window.
- Close the window with the X in its corner.
- The program ends, and the Shell shows the 3 greater than signs of the prompt again.
- Your turn. Change the title to Staff System and the size to 500 by 400,
- then save and run it to check the new window.
- Part 2. Labels. You will add labels to a window and change their font, size and
- colour.
- Click File, then New File, and type the 4 lines that create the window.
- Import tkinter as tk, set window equals tk.Tk,
- set window.title to Labels, and window.geometry to 400,
- small x, 200.
- Now create welcome_label. Set it equal to tk.Label, and in the
- brackets give it the window, a comma, then text equals Welcome to
- Pentre Bach Pre-School in speech marks.
- The first thing in the brackets is always the window the label goes in.
- Call welcome_label.pack, with empty brackets.
- A label does not appear until you place it in the window, and pack is what places
- it.
- Next, create heading_label with the text Staff System, and add font equals,
- then in brackets, Arial in speech marks, 20, and bold in speech marks.
- The 3 parts are the font name, the size and the style.
- Then create hours_label with the text Open 8am to 6pm, font
- Arial 12, f g equals white, and b g equals green.
- f g sets the text colour, and b g sets the background colour.
- Pack both new labels, and add window.mainloop at the end.
- Save the file as labels.py and press F5 to run it.
- A 400 by 200 window titled Labels shows 3 labels stacked in the centre.
- Welcome to Pentre Bach Pre-School is in the normal font.
- Staff System is in large bold Arial 20.
- Open 8am to 6pm is in Arial 12, with white text on a green background.
- Try other colour names, such as blue or orange, and other sizes, then run it again.
- Your turn. Add a fourth label called phone_label with the text Call
- 01558 123456, in Arial 14, with black text on
- a yellow background, then pack it and run the program.
- Part 3. Entry boxes. You will read text from an entry box with get, clear it with
- delete, fill it with insert, and hide a password with show equals the star symbol.
- Click File, then New File. Import tkinter as tk, and create a
- window titled Login, 300 by 200.
- Add a label with the text Username, then an Entry box called username_entry,
- set to tk.Entry with the window in brackets.
- Pack both.
- Add username_entry.insert, and in brackets, 0, a comma,
- and staff in speech marks. This puts starting text in the box.
- 0 means the first position.
- Add a label with the text Password, and an Entry box called password_entry
- with show equals the star symbol in speech marks.
- Each character you type shows as a star.
- Pack both.
- Now write the show_details function, near the top of the program.
- Type def show details, empty brackets and a colon.
- Inside, set username equals username_entry.get, and password
- equals password_entry.get. get reads what is in each box.
- Then print Username, colon, and username, and print Password, colon, and
- password, so the text shows in the Shell.
- Write the clear_boxes function.
- Inside it, type username_entry.delete, with 0, a comma, and
- tk.END in capitals. Do the same for password_entry.delete 0 to
- END removes everything from the first character to the end.
- Add a button with the text Show and command equals show details, and a button with
- the text Clear and command equals clear boxes.
- Pack them, then add window.mainloop.
- Save the file as entry_boxes.py and press F5.
- The Login window shows a Username box that starts with staff, a Password box, and
- the Show and Clear buttons.
- Delete staff. Type jjones as the username and Parkwood27 as the
- password. The password box shows only stars.
- Then click "Show".
- Look at the Shell to see what get read: Username: jjones, and Password:
- Parkwood27. The real password is printed, even though the box hid it.
- Then click "Clear" and watch both boxes empty.
- Your turn. Change the insert line so the Username box starts with the text admin,
- then run it, click "Show" without changing anything, and check what the Shell
- prints.
- Part 4. Buttons. You will make a button run a function when it
- is clicked, using command equals.
- Click File, then New File, and type import tkinter
- as tk.
- Create the variable children_in, and set it to 0.
- Write the child_arrives function.
- Type def child arrives, empty brackets and a colon.
- Inside, type global children in, so the function can change the variable outside
- it. Then set children in equals children in plus 1, and print Children
- in, colon, and children in. It adds 1 to the count and prints the new count.
- Write the child_leaves function in the same way.
- It also starts with global children in, then takes 1 away with children in equals
- children in minus 1, and prints the new count.
- Create a window with window equals tk.Tk, title it Register,
- and set the geometry to 300, small x, 150.
- Create arrive_button. Set it equal to tk.Button, with the
- window, text equals Child arrives, and command equals child arrives.
- Do not put brackets after the function name.
- If you do, it runs once when the program starts, instead of on each click.
- command equals just names the function to run.
- Create leave_button with the text Child leaves and command equals
- child leaves. Pack both buttons and add window.mainloop.
- Save the file as buttons.py and press F5 to run it.
- A window titled Register shows 2 buttons, one above the other: Child arrives and
- Child leaves.
- Click "Child arrives" 3 times, then "Child leaves" once, and watch the count in the
- Shell. It shows Children in: 1, then 2, then 3, and then back down to
- 2.
- Your turn. Add a third button called Reset with a function that sets children in
- back to 0 and prints the count, then run it and check that clicking "Reset" prints
- Children in: 0.
- That's Tkinter GUIs done. You made a window, added labels,
- read and filled entry boxes, and made buttons run a function when they're clicked.
- If you skipped any of the 4 tasks, go back to the end of that part and have a go.
- Next, watch the next video on screen, or pick another topic from the playlist.