Beginner's Course on Tkinter

Jul 14, 2024

Introduction to Tkinter


Overview

  • Tkinter is a library for building Graphical User Interfaces (GUI) in Python.
  • Tkinter is part of the core Python stack; no need to install it separately.
  • The lecture covers basics: setting up a window, layouts, widgets, and functionalities.

Importing Tkinter

  • Import the library: import tkinter as tk
  • Best practice: avoid from tkinter import * to prevent ambiguity.

Creating a Basic Window

  1. Initialize Window: tk.Tk()
    root = tk.Tk()
    
  2. Main Loop: root.mainloop()
    root.mainloop()
    

Window Properties

  • Set Title: root.title('title')
  • Set Geometry: root.geometry('widthxheight')
    root.title('My First GUI')
    root.geometry('500x500')
    

Basic Widgets

  1. Label
    label = tk.Label(root, text="Hello World", font=('Arial', 18))
    label.pack(padx=20, pady=20)
    
  2. Text Box
    text_box = tk.Text(root, font=('Arial', 16), height=3)
    text_box.pack(padx=20)
    
  3. Entry
    entry = tk.Entry(root)
    entry.pack(padx=10)
    
  4. Button
    button = tk.Button(root, text="Click Me", font=('Arial', 18))
    button.pack(padx=10, pady=10)
    

Layouts

  1. Pack Layout: Simplest layout manager.
  2. Grid Layout: Align widgets in a grid.
    button_frame = tk.Frame(root)
    button_frame.columnconfigure(0, weight=1)
    button1 = tk.Button(button_frame, text="1", font=('Arial', 18))
    button1.grid(row=0, column=0, sticky='we')
    button_frame.pack(fill='x')
    
  3. Place Layout: Manually place widgets.
    another_button = tk.Button(root, text="Test")
    another_button.place(x=200, y=200, height=100, width=100)
    

Adding Functionality

  1. Define a Class for GUI
    class MyGUI:
        def __init__(self):
            self.root = tk.Tk()
            self.root.mainloop()
        # Add more initiation here...
    
  2. Event Handling
    def show_message(self):
        message = self.text_box.get("1.0", tk.END)
        if self.check_state.get() == 0:
            print(message)
        else:
            messagebox.showinfo("Message", message)
    
    button.config(command=self.show_message)
    

Additional Features

  1. Menu Bar

    menu_bar = tk.Menu(root)
    file_menu = tk.Menu(menu_bar, tearoff=0)
    file_menu.add_command(label="Close", command=root.quit)
    menu_bar.add_cascade(label="File", menu=file_menu)
    root.config(menu=menu_bar)
    
  2. Closing Event

    def on_closing(self):
        if messagebox.askyesno("Quit", "Do you want to quit?"):
            self.root.destroy()
        
    self.root.protocol("WM_DELETE_WINDOW", self.on_closing)
    
  3. Keyboard Shortcuts

    def shortcut(self, event):
        if event.state == 12 and event.keysym == 'Return':
            self.show_message()
    
    self.text_box.bind('<KeyPress>', self.shortcut)
    

Summary

  • Tkinter provides basic tools for building interactive GUIs in Python.
  • Understand and use different layouts and widgets.
  • Utilize classes for structuring the GUI and adding functionalities.
  • Menus and event handling improve user interaction.