Tkinter Basics
Tkinter Basics
After building terminal programs, the next step is creating real windows and interactive apps.
Python includes a built-in GUI library called Tkinter.
It is the best place to start learning desktop GUI development.
What Is Tkinter?
Tkinter is Python’s standard library for building graphical user interfaces (GUIs).
It lets you create:
- windows
- buttons
- labels
- text inputs
- menus
- layouts
- dialogs
No extra installation is needed in most Python setups.
Why Learn Tkinter?
Tkinter is useful because it is:
- beginner-friendly
- included with Python
- great for learning GUI concepts
- good for small desktop apps
- widely taught
Your First GUI Window
import tkinter as tk
window = tk.Tk()
window.mainloop()
What This Code Does
import tkinter as tk
Imports Tkinter and gives it the short name tk.
tk.Tk()
Creates the main application window.
mainloop()
Starts the GUI event loop.
Without it, the window closes immediately.
Add a Window Title
import tkinter as tk
window = tk.Tk()
window.title("My App")
window.mainloop()
Set Window Size
window.geometry("400x300")
Width = 400 pixels
Height = 300 pixels
Full example:
import tkinter as tk
window = tk.Tk()
window.title("Demo")
window.geometry("400x300")
window.mainloop()
Change Background Color
window.configure(bg="lightblue")
Example:
import tkinter as tk
window = tk.Tk()
window.title("Colors")
window.geometry("300x200")
window.configure(bg="lightblue")
window.mainloop()
Common Beginner Terms
Widget
A GUI element like a button or label.
Root Window
The main application window.
Event Loop
The system that waits for clicks and actions.
Complete Starter Example
import tkinter as tk
window = tk.Tk()
window.title("Welcome")
window.geometry("500x300")
window.configure(bg="white")
window.mainloop()
Common Beginner Errors
Forgot mainloop()
Window may appear and close instantly.
Misspelled geometry()
Check spelling carefully.
Wrong Size Format
Use:
"400x300"
Not:
400,300
No Tkinter Installed
Some Linux systems may need Tkinter package installed separately.
Code Along
Create a window:
- title =
Student App - size =
500x400
Mini Challenge
Build a window with:
- title =
Notes - size =
350x250 - background =
yellow
Expected result:
A yellow desktop window titled Notes.
Real World Use Case
Tkinter is used for calculators, internal tools, data entry apps, and simple desktop utilities.
Quiz
- What does GUI stand for?
- What does
tk.Tk()create? - Why is
mainloop()needed? - What is a widget?
Assignment
Create your own custom window with title, size, and background color.
Summary
You learned how to create your first Tkinter window and understand the basic structure of GUI apps.