python - 我应该添加什么代码来保存我所做的更改?
问题描述
伙计们,我是编程的新手。几天前我开始学习编码(Python)。我观看了一个 youtube 视频并制作了一个待办事项列表。但是有一件事在视频中没有解释,我想问一些比我更了解 Python 的人。
在下面的代码中,我不知道如何保存我的更改。
假设我创建了我想做的任务,但是一旦我关闭程序,我的所有细节都会丢失。再次打开 .py 文件后是否可以保存数据?
感谢您的帮助!
import tkinter
from tkinter import messagebox
from tkinter import font
#Create Root Window
root = tkinter.Tk()
#Change root window background color
root.configure(bg='white')
# define font
my_btn_font = font.Font(size=11,family='great vibes')
my_listbox_font = font.Font(size=10,family='great vibes')
my_textbox_font = font.Font(size=15,family='great vibes')
#Change the titel
root.title('My Daily To Do List')
#Change window size
root.geometry('335x350')
root.resizable(False, False)
#Create an empty list
tasks = []
#For testing purposes use a default list
#tasks = ['Work', 'Chill','Relax']
#********************************************************************
#Defining all functions here
def update_listbox():
#Clear current list
clear_listbox()
#Populate the listbox
for task in tasks:
listbox_tasks.insert('end', task)
def clear_listbox():
listbox_tasks.delete(0, 'end')
def add_task():
#Get the task to add
task = text_input.get()
#Make sure the task is not empty
if task !="":
#Append to the list
tasks.append(task)
#Update the listbox
update_listbox()
else:
messagebox.showinfo('Info', 'Please enter a task')
text_input.delete(0, 'end')
def delete_all():
confirmed = messagebox.askyesno('Please confirm:', 'Do you really want to Delete all?')
if confirmed == True:
#Making the delete all list global
global tasks
#Clear the tasks list
tasks = []
#Update the listbox
update_listbox()
def del_one():
#Get the text of the currently selected item
task = listbox_tasks.get('active')
#Confirm it is in the list
if task in tasks:
tasks.remove(task)
#Update the listbox
update_listbox()
def sort_asc():
#Sort the list
tasks.sort()
#Update the listbox
update_listbox()
def sort_des():
#Sort the list
tasks.sort()
#Reverse the list
tasks.reverse()
#Update the listbox
update_listbox()
def show_number_of_tasks():
#Get the number of tasks
number_of_tasks = len(tasks)
#Create and format the message
msg = 'Number of tasks: {}'.format(number_of_tasks)
#Display the message
lbl_display['text']=msg
#***********************************************************************
#Adding Listbox, Input box & Title
# Creating Title
lbl_title = tkinter.Label(root, text='To-Do-List', bg='white')
lbl_title.grid(row=0,column=0)
lbl_title['font'] = my_btn_font
lbl_display = tkinter.Label(root, text='', bg='white')
lbl_display.grid(row=0,column=1)
lbl_title['font'] = my_btn_font
#Creating Text input box
text_input = tkinter.Entry(root, bd=4, font=my_textbox_font)
text_input.grid(row=1,column=1)
#******************************************************************
#Adding buttons and starting main loop
#Creating all the buttons
btn_add_task = tkinter.Button(root, text='Add Task', fg='gray4', bg='white', command=add_task, width=9)
btn_add_task.grid(row=1,column=0)
btn_add_task['font'] = my_btn_font
btn_delete_all = tkinter.Button(root, text='Delete All', fg='gray4', bg='white', command=delete_all, width=9)
btn_delete_all.grid(row=2,column=0)
btn_delete_all['font'] = my_btn_font
btn_del_one = tkinter.Button(root, text='Delete', fg='gray4', bg='white', command=del_one, width=9)
btn_del_one.grid(row=3,column=0)
btn_del_one['font'] = my_btn_font
btn_sort_asc = tkinter.Button(root, text='Sort(ASC)', fg='gray4', bg='white', command=sort_asc, width=9)
btn_sort_asc.grid(row=4,column=0)
btn_sort_asc['font'] = my_btn_font
btn_sort_des = tkinter.Button(root, text='Sort(DSC)', fg='gray4', bg='white', command=sort_des, width=9)
btn_sort_des.grid(row=5,column=0)
btn_sort_des['font'] = my_btn_font
btn_number_of_tasks = tkinter.Button(root, text='No. of Tasks', fg='gray4', bg='white', command=show_number_of_tasks, width=9)
btn_number_of_tasks.grid(row=6,column=0)
btn_number_of_tasks['font'] = my_btn_font
btn_exit = tkinter.Button(root, text='Exit', fg='gray4', bg='white', command=exit, width=9)
btn_exit.grid(row=7,column=0)
btn_exit['font'] = my_btn_font
listbox_tasks = tkinter.Listbox(root, bd=4, height=10, width=20, font='my_listbox_font')
listbox_tasks.grid(row=2,column=1, rowspan=7)
#Start the main events loop
root.mainloop()
解决方案
推荐阅读
- paypal - 如何从 Paypal 授权页面中删除稍后付款
- jackrabbit - Moqui 是否支持在 Apache Jackrabbit 中保存内容/查看内容历史记录/结帐/签入
- javascript - 在 Angular 中填充下拉列表时,正确的选项是什么?
- bash - 如何从另一个 bash 命令的输出中解析命令参数?
- python - 小部件之间的 Tkinter 空间不断增长
- c++ - 如何在 Sublime Text 上为 C++20 构建系统?
- python - 如何在不使用 eval() 和 exec() 在 python 中将列表格式的字符串转换为实际的 python 列表
- python - “NoneType”对象没有属性“display_name”
- git - 如何在正确的分支中转移提交?我将它推入了主分支而不是正确的功能分支
- php - PHP selectfield 从数据库中获取值,更改和 POST