on
ai 주식투자
- Get link
- X
- Other Apps
import tkinter as tk
from tkinter import messagebox
def add_task():
"""Adds a new task to the to-do list."""
task = task_entry.get()
if task:
task_list.insert(tk.END, task)
task_entry.delete(0, tk.END)
else:
messagebox.showwarning("Warning", "Please enter a task.")
def delete_task():
"""Deletes the selected task from the to-do list."""
try:
selected_index = task_list.curselection()[0]
task_list.delete(selected_index)
except IndexError:
messagebox.showinfo("Info", "Please select a task to delete.")
def mark_complete():
"""Marks the selected task as complete (crosses it out)."""
try:
selected_index = task_list.curselection()[0]
task = task_list.get(selected_index)
task_list.delete(selected_index)
task_list.insert(selected_index, f"✅ {task}") # Add a checkmark
except IndexError:
messagebox.showinfo("Info", "Please select a task to mark as complete.")
# --- Main Window Setup ---
window = tk.Tk()
window.title("To-Do List")
window.geometry("400x400")
# --- Widgets ---
task_label = tk.Label(window, text="Enter Task:")
task_label.grid(row=0, column=0, padx=5, pady=5)
task_entry = tk.Entry(window, width=30)
task_entry.grid(row=0, column=1, padx=5, pady=5)
add_button = tk.Button(window, text="Add Task", command=add_task)
add_button.grid(row=1, column=0, columnspan=2, padx=5, pady=5)
task_list = tk.Listbox(window, width=40, height=10)
task_list.grid(row=2, column=0, columnspan=2, padx=5, pady=5)
delete_button = tk.Button(window, text="Delete Task", command=delete_task)
delete_button.grid(row=3, column=0, padx=5, pady=5)
complete_button = tk.Button(window, text="Mark Complete", command=mark_complete)
complete_button.grid(row=3, column=1, padx=5, pady=5)
# --- Start the GUI ---
window.mainloop()
-
How to Run:
This code creates a window with an entry field for adding tasks, a listbox to display the tasks, and buttons for adding, deleting, and marking tasks as complete. It provides a user-friendly interface for managing your to-do list. I've included a checkmark to visually indicate completed tasks.
This code creates a simple to-do list application with a graphical user interface (GUI) using Python's tkinter library. Here's a breakdown of each part, explained in a way that's easy to understand:
1. Importing Libraries:
import tkinter as tk
from tkinter import messagebox
-
2. Defining Functions (What happens when you click a button):
3. Setting Up the Main Window:
window = tk.Tk()
window.title("To-Do List")
window.geometry("400x400")
-4. Creating Widgets (The visual elements):
5. Arranging Widgets (Using grid):
The grid() method is used to arrange the widgets in a grid layout within the window. row and column specify the position of the widget in the grid. padx and pady add padding around the widget.
6. Starting the GUI:
window.mainloop()
-How it Works Together:
The code creates a window with a text box, buttons, and a listbox. When the user types a task into the text box and clicks "Add Task," the add_task() function adds the task to the listbox. When the user selects a task and clicks "Delete Task," the delete_task() function removes the task from the listbox. When the user selects a task and clicks "Mark Complete," the mark_complete() function adds a checkmark to the beginning of the task in the listbox. The mainloop() function keeps the window open and responsive to user interactions.
Comments
Post a Comment