📊 Unit-IV: Python GUI Programming with Tkinter

Lecture 1: Introduction to Tkinter and Basic Widgets

1. Introduction to Tkinter

Tkinter is Python's standard GUI (Graphical User Interface) package. It is the most commonly used method for creating graphical applications with Python.

Note: Tkinter is included with standard Python installations on Windows, macOS, and Linux.

Why Use Tkinter?

  • Easy to learn and use
  • Cross-platform compatibility
  • Comes pre-installed with Python
  • Large community support
  • Great for small to medium-sized applications

Basic Tkinter Program Structure

import tkinter as tk # Create the main window root = tk.Tk() root.title("My First Tkinter App") root.geometry("400x300") # Add widgets here # Start the main event loop root.mainloop()
2. The Label Widget

The Label widget is used to display text or images on the screen.

# Creating a label label = tk.Label( root, text="Hello, Tkinter!", font=("Arial", 14), fg="blue", bg="lightgray", padx=10, pady=5 ) label.pack()

Label Example:

Hello, Tkinter!
3. The Button Widget

The Button widget is used to add buttons to your application.

def button_click(): print("Button was clicked!") button = tk.Button( root, text="Click Me!", command=button_click, bg="#4CAF50", fg="white", padx=10, pady=5, font=("Arial", 12) ) button.pack(pady=10)

Button Example:

4. The Entry Widget

The Entry widget is used to accept single-line text input from the user.

# Creating an Entry widget entry = tk.Entry( root, width=30, font=("Arial", 12), show="*" # For password fields ) entry.pack(pady=10) # Getting the entry value user_input = entry.get()

Entry Example:

5. The Text Widget

The Text widget is used to display and edit multi-line text.

# Creating a Text widget text_widget = tk.Text( root, height=10, width=40, wrap=tk.WORD, font=("Arial", 12) ) text_widget.pack(pady=10) # Inserting text text_widget.insert(tk.END, "This is a Text widget.\nYou can type multiple lines here.") # Getting text content = text_widget.get("1.0", tk.END)

Text Widget Example:

6. The Canvas Widget

The Canvas widget is used to draw shapes, images, and complex layouts.

canvas = tk.Canvas( root, width=300, height=200, bg="white" ) canvas.pack(pady=10) # Drawing shapes canvas.create_rectangle(10, 10, 100, 100, fill="blue") canvas.create_oval(150, 50, 250, 150, fill="red") canvas.create_line(0, 0, 300, 200, width=3) canvas.create_text(150, 30, text="Canvas Example", font=("Arial", 12, "bold"))

Canvas Example:

Canvas Example
Summary

In this lecture, we've covered the basics of Tkinter and several important widgets:

  • Tkinter: Python's standard GUI package
  • Label: For displaying text or images
  • Button: For creating clickable buttons
  • Entry: For single-line text input
  • Text: For multi-line text display and editing
  • Canvas: For drawing shapes and custom graphics

Practice Exercise:

Create a simple Tkinter application that includes:

  1. A Label with a welcome message
  2. An Entry field for the user's name
  3. A Button that, when clicked, displays a greeting with the user's name in a new Label
  4. A Canvas with at least two different shapes