CodingNic

GUI Development

Tkinter Basics

GUI Development 34 min read

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

python
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

python
import tkinter as tk

window = tk.Tk()
window.title("My App")

window.mainloop()

Set Window Size

python
window.geometry("400x300")

Width = 400 pixels
Height = 300 pixels

Full example:

python
import tkinter as tk

window = tk.Tk()
window.title("Demo")
window.geometry("400x300")

window.mainloop()

Change Background Color

python
window.configure(bg="lightblue")

Example:

python
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

python
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:

python
"400x300"

Not:

python
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

  1. What does GUI stand for?
  2. What does tk.Tk() create?
  3. Why is mainloop() needed?
  4. 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.