Introduction
Basic Widgets
Toplevel Widgets
Geometry Management
Binding Functions
Working with Images in Tkinter
Tkinter Advance
Applications and Projects
Let's create a basic To-Do application using Tkinter. This application will allow users to:
import tkinter as tk from tkinter import messagebox # Initialize the main window root = tk.Tk() root.title("To-Do App") root.geometry("300x400")
# Function to add a task def add_task(): task = task_entry.get() if task != "": tasks_listbox.insert(tk.END, task) task_entry.delete(0, tk.END) else: messagebox.showwarning("Warning", "Please enter a task!") # Function to delete a task def delete_task(): try: selected_task_index = tasks_listbox.curselection()[0] tasks_listbox.delete(selected_task_index) except IndexError: messagebox.showwarning("Warning", "Please select a task to delete!")
Entry and Buttons:
frame = tk.Frame(root) frame.pack(pady=20) task_entry = tk.Entry(frame, width=25) task_entry.pack(side=tk.LEFT, padx=20) add_btn = tk.Button(frame, text="Add Task", command=add_task) add_btn.pack(fill=tk.BOTH) delete_btn = tk.Button(root, text="Delete Task", command=delete_task) delete_btn.pack(pady=20)
Listbox to display tasks:
tasks_listbox = tk.Listbox(root, width=50, height=10) tasks_listbox.pack(pady=20)
root.mainloop()
When you put all these together and run the script, you'll have a simple To-Do GUI application using Tkinter. You can enter tasks, add them to the list, and delete them once completed.
This is a basic implementation. You can expand upon it by:
Remember, this is a foundational step. Once you understand these basics, you can explore more advanced features and functionalities to build a more robust To-Do application.
import tkinter as tk def add_task(): task = entry.get() if task: listbox.insert(tk.END, task) entry.delete(0, tk.END) def remove_task(): selected_task_index = listbox.curselection() if selected_task_index: listbox.delete(selected_task_index) # Create main window root = tk.Tk() root.title("ToDo List") # Entry widget for task input entry = tk.Entry(root, width=30) entry.grid(row=0, column=0, padx=10, pady=10) # Add and Remove buttons add_button = tk.Button(root, text="Add Task", command=add_task) add_button.grid(row=0, column=1, padx=5, pady=10) remove_button = tk.Button(root, text="Remove Task", command=remove_task) remove_button.grid(row=0, column=2, padx=5, pady=10) # Listbox to display tasks listbox = tk.Listbox(root, selectmode=tk.SINGLE, width=40, height=10) listbox.grid(row=1, column=0, columnspan=3, padx=10, pady=10) # Start the Tkinter event loop root.mainloop()
This example is similar to the previous one.
import tkinter as tk def add_task(): task = entry.get() if task: listbox.insert(tk.END, task) entry.delete(0, tk.END) def remove_task(): selected_task_index = listbox.curselection() if selected_task_index: listbox.delete(selected_task_index) # Create main window root = tk.Tk() root.title("ToDo List") # Entry widget for task input entry = tk.Entry(root, width=30) entry.grid(row=0, column=0, padx=10, pady=10) # Add and Remove buttons add_button = tk.Button(root, text="Add Task", command=add_task) add_button.grid(row=0, column=1, padx=5, pady=10) remove_button = tk.Button(root, text="Remove Task", command=remove_task) remove_button.grid(row=0, column=2, padx=5, pady=10) # Listbox to display tasks with checkboxes listbox = tk.Listbox(root, selectmode=tk.SINGLE, width=40, height=10) listbox.grid(row=1, column=0, columnspan=3, padx=10, pady=10) # Add checkboxes to each task for i in range(5): listbox.insert(tk.END, f"Task {i+1}") # Start the Tkinter event loop root.mainloop()
The first example covers this.
The first example covers this.
This involves adding update and delete functionalities to tasks, which can be achieved by extending the code. You would need to add an "Update" button and modify the code accordingly.
The first example covers this.
The first example is a simple project example.