用户提问: ai编写小程序
Ai回答: Here's a simple example of a To-Do List mini program written in Python using the Tkinter library for the GUI:
python
import tkinter as tk
from tkinter import messagebox
class ToDoList:
def __init__(self, root):
self.root = root
self.root.title(To-Do List)
self.tasks = []
Create task list
self.task_list = tk.Listbox(self.root, width=40, height=10)
self.task_list.pack(padx=10, pady=10)
Create new task entry
self.new_task_label = tk.Label(self.root, text=New Task:)
self.new_task_label.pack()
self.new_task_entry = tk.Entry(self.root, width=40)
self.new_task_entry.pack()
Create buttons
self.add_task_button = tk.Button(self.root, text=Add Task, command=self.add_task)
self.add_task_button.pack()
self.delete_task_button = tk.Button(self.root, text=Delete Task, command=self.delete_task)
self.delete_task_button.pack()
self.save_tasks_button = tk.Button(self.root, text=Save Tasks, command=self.save_tasks)
self.save_tasks_button.pack()
self.load_tasks_button = tk.Button(self.root, text=Load Tasks, command=self.load_tasks)
self.load_tasks_button.pack()
def add_task(self):
task = self.new_task_entry.get()
if task != :
self.tasks.append(task)
self.task_list.insert(tk.END, task)
self.new_task_entry.delete(0, tk.END)
def delete_task(self):
try:
task_index = self.task_list.curselection()[0]
self.task_list.delete(task_index)
self.tasks.pop(task_index)
except:
messagebox.showwarning(Error, Select a task to delete)
def save_tasks(self):
with open(tasks.txt, w) as file:
for task in self.tasks:
file.write(task + \n)
messagebox.showinfo(Tasks Saved, Tasks have been saved to tasks.txt)
def load_tasks(self):
try:
with open(tasks.txt, r) as file:
self.tasks = file.readlines()
self.tasks = [task.strip() for task in self.tasks]
self.task_list.delete(0, tk.END)
for task in self.tasks:
self.task_list.insert(tk.END, task)
except FileNotFoundError:
messagebox.showwarning(Error, No saved tasks found)
if __name__ == __main__:
root = tk.Tk()
todo_list = ToDoList(root)
root.mainloop()
This program creates a simple to-do list with the following features:
* Add tasks to the list
* Delete tasks from the list
* Save tasks to a file
* Load tasks from a file
Here's how it works:
1、 The program creates a GUI window with a list box to display tasks, an entry field to input new tasks, and buttons to add, delete, save, and load tasks.
2、 When you enter a new task and click Add Task, it gets added to the list.
3、 You can delete a task by selecting it in the list and clicking Delete Task.
4、 Clicking Save Tasks saves all tasks in the list to a file named `tasks.txt`.
5、 Clicking Load Tasks loads tasks from `tasks.txt` and displays them in the list.
This is a basic example、You can enhance it by adding more features like editing tasks, due dates, priorities, etc.
0
IP地址: 39.218.213.155
搜索次数: 0
提问时间: 2025-04-19 22:35:26
热门提问:
ai写作能写小说吗
ico图标的自定义尺寸
ai翻译合同
写ai写作
宝盈睿丰创新混合A/B