Advertisement
CodeCrusader

To-Do List App GUI

Jun 10th, 2023
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.35 KB | Software | 0 0
  1. import tkinter as tk
  2. from tkinter import messagebox
  3.  
  4. # Initialize the window
  5. window = tk.Tk()
  6. window.title("To-Do List")
  7.  
  8. # Create a list to store tasks
  9. tasks = []
  10.  
  11. # Function to add a task
  12. def add_task():
  13.     task = entry.get()
  14.     if task:
  15.         tasks.append(task)
  16.         update_task_list()
  17.         entry.delete(0, tk.END)
  18.     else:
  19.         messagebox.showwarning("Empty Task", "Please enter a task.")
  20.  
  21. # Function to remove a task
  22. def remove_task():
  23.     selected_task = task_listbox.curselection()
  24.     if selected_task:
  25.         index = selected_task[0]
  26.         tasks.pop(index)
  27.         update_task_list()
  28.  
  29. # Function to update the task list
  30. def update_task_list():
  31.     task_listbox.delete(0, tk.END)
  32.     for task in tasks:
  33.         task_listbox.insert(tk.END, task)
  34.  
  35. # Entry widget to input tasks
  36. entry = tk.Entry(window, width=30)
  37. entry.grid(row=0, column=0, padx=10, pady=10)
  38.  
  39. # Button to add a task
  40. add_button = tk.Button(window, text="Add Task", command=add_task)
  41. add_button.grid(row=0, column=1, padx=5)
  42.  
  43. # Task listbox
  44. task_listbox = tk.Listbox(window, width=40)
  45. task_listbox.grid(row=1, column=0, columnspan=2, padx=10, pady=10)
  46.  
  47. # Button to remove a task
  48. remove_button = tk.Button(window, text="Remove Task", command=remove_task)
  49. remove_button.grid(row=2, column=0, columnspan=2, padx=10, pady=5)
  50.  
  51. window.mainloop()
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement