在Python中的简单待办事项列表GUI应用程序

在Python中的简单待办事项列表GUI应用程序

每天都有一系列等待我们完成的任务。其中一些可能有趣,而其他一些可能乏味。然而,完成这些任务的重要性是毋庸置疑的,特别是在它们定期构建的情况下。

有时候我们中的许多人都难以记录我们所需的事项。也许我们中的一些人习惯使用手写的待办事项列表来提醒自己有待完成的事情和截止日期。然而,手写的笔记有更高的丢失或遗忘的可能性。既然我们是Python编程者,使用Tkinter构建一个待办事项列表管理GUI应用程序是有道理的。

在下面的教程中,我们将逐步了解使用Python编程语言中的Tkinter库构建一个简单的待办事项列表管理GUI应用程序的步骤。

但在我们开始之前,让我们简单了解一下待办事项列表是什么以及它的好处。

什么是待办事项列表

一个待办事项列表是我们需要完成的任务或我们想要做的事情的列表。

最常见的是,这个列表中的任务按优先级排序。出于传统,它们是写在一张纸上或便利贴上作为记忆辅助工具。由于技术的发展,我们能够使用Excel电子表格、Word文档、电子邮件列表以及像Microsoft待办事项和Google待办事项列表这样的待办事项列表应用程序创建待办事项列表。我们可以在家庭、工作场所或个人生活中使用待办事项列表。

将我们需要做的事情都写在一个地方的列表中意味着我们不应该忘记任何重要的事情。通过对列表中的任务进行优先排序,我们可以确定它们的完成顺序,并迅速观察到哪些任务需要我们立即关注,哪些任务可以推迟一段时间。

理解使用待办事项列表的优势

使用待办事项列表的一个重要原因是它将帮助我们保持计划有序。当我们将所有任务都写在列表中时,它们看起来更方便。当我们清晰了解需要采取行动的任务和已经完成的任务时,它有助于我们保持集中注意力。同时,为其他更有创造性的任务释放我们思维空间。

当我们完成一个任务时,我们可以将其标记或在列表中划掉。这给我们带来了进步和成就感,如果我们总是匆忙从一个任务到另一个任务,我们将会缺乏这种感觉。如果我们有成就感,它会激励我们继续前进。

然而,这并不是待办事项列表的唯一好处。使用待办事项列表的一些优势如下所示:

  1. 它有助于提升记忆力。 待办事项列表充当外部记忆辅助工具,每次只能记住少量信息。保持待办事项列表可以帮助我们跟踪所有事项,而不仅仅是需要执行的一些任务。待办事项列表还会加强这些信息,使得遗忘的可能性降低。
  2. 它还有助于提高生产力。 待办事项列表使用户能够优先处理更重要的任务。该列表将帮助我们集中注意力处理许多重要任务。这意味着我们不必浪费时间在不需要立即注意的任务上。
  3. 它还能提供动力支持。 待办事项列表是一个很好的激励工具,因为它们帮助我们澄清我们的目标。我们可以将长期目标划分为更小、更可实现的短期里程碑,每完成一个里程碑,我们的信心就会提升。

既然我们已经了解了待办事项列表及其好处,现在是时候开始构建一个用于管理日常任务的待办事项列表应用程序了。

使用Tkinter构建待办事项列表应用程序

使用Tkinter构建待办事项列表管理应用程序的过程分为几个步骤。下面是这些步骤:

步骤1: 首先,我们将导入构建该项目所需的所有模块。

步骤2: 然后,我们将定义执行应用程序所需的一些函数。

步骤3: 接下来,我们将创建应用程序的主窗口。

步骤4: 然后,我们将向应用程序添加一个数据库以存储数据。

步骤5: 我们将向应用程序添加必要的小部件并应用事件触发器。

步骤6: 在应用程序的主函数中调用这些函数。

让我们详细讨论这些步骤。

导入所需的模块

在开始构建项目之前,导入应用程序运行所需的模块非常重要。我们将从Tkinter库中导入所有所需的小部件和模块,以为应用程序提供图形用户界面。我们还将导入 sqlite3 模块,以将数据存储在数据库中。

下面是一个演示同样功能的代码片段。

文件:todo_application.py

# importing the required modules
import tkinter as tk            # importing the tkinter module as tk
from tkinter import ttk         # importing the ttk module from the tkinter library
from tkinter import messagebox  # importing the messagebox module from the tkinter library
import sqlite3 as sql           # importing the sqlite3 module as sql

解释:

在上面的代码片段中,我们导入了tkinter模块作为tk。然后我们从Tkinter库中导入了ttk模块和messagebox模块。最后,我们将sqlite3模块导入为sql。

为应用程序定义函数

成功导入所需的模块后,我们需要定义各种函数来处理应用程序中的不同事件。这些函数将允许我们将任务添加到列表中,从列表中删除任务,从列表中删除所有任务,更新列表,清空列表,检索数据库和关闭应用程序。

我们将首先定义一个空列表,用于帮助我们存储任务。以下是演示相同功能的代码片段。

文件:todo_application.py

# defining an empty list
tasks = []

说明:

在上述代码片段中,我们定义了一个空列表作为 任务 。此列表将允许我们存储所有输入的任务。

现在让我们定义执行应用程序所需的所有函数。

添加任务到列表中

我们将首先定义应用程序的第一个函数,用于将任务添加到列表中。此函数将从输入字段中检索包含任务的字符串,并将其存储到列表和数据库中。

以下是代码段,示例了这样一个函数的定义。

文件: todo_application.py

# defining the function to add tasks to the list
def add_task():
    # getting the string from the entry field
    task_string = task_field.get()
    # checking whether the string is empty or not
    if len(task_string) == 0:
        # displaying a message box with 'Empty Field' message
        messagebox.showinfo('Error', 'Field is Empty.')
    else:
        # adding the string to the tasks list
        tasks.append(task_string)
        # using the execute() method to execute a SQL statement
        the_cursor.execute('insert into tasks values (?)', (task_string,))
        # calling the function to update the list
        list_update()
        # deleting the entry in the entry field
        task_field.delete(0, 'end')

说明:

在上面的代码片段中,我们定义了一个名为 add_task 的函数。我们使用 get() 方法从输入框中检索字符串,并将其存储在 task_string 变量中。然后我们检查字符串是否为空。如果 task_string 变量的长度为零,则显示一个消息框,显示 ‘Field is Empty’ 的消息。如果字符串不为空,我们使用 append() 方法将该字符串添加到先前创建的列表中。我们还使用 execute() 方法执行SQL语句 ‘insert into tasks values (?)’ 并将存在于 task_string 中的值存储在数据库中。然后我们调用 list_update() 函数来更新列表,并使用 delete() 方法删除输入框中的条目。

更新列表

现在我们将定义另一个函数,允许我们更新列表并将条目插入到列表框中。

让我们考虑以下代码片段,展示了相同的内容。

文件:todo_application.py

# defining the function to update the list
def list_update():
    # calling the function to clear the list
    clear_list()
    # iterating through the strings in the list
    for task in tasks:
        # using the insert() method to insert the tasks in the list box
        task_listbox.insert('end', task)

解释:

在上面的代码片段中,我们定义了一个名为 list_update 的函数。在这个函数中,我们调用了 clear_list() 函数来清除列表。然后我们使用 for 循环遍历列表中的任务,并使用 insert() 方法将任务插入到列表框中。

从列表中删除任务

现在我们将定义一个函数来从列表和数据库中删除所选任务。以下是展示相同内容的代码片段:

文件:todo_application.py

# defining the function to delete a task from the list
def delete_task():
    # using the try-except method
    try:
        # getting the selected entry from the list box
        the_value = task_listbox.get(task_listbox.curselection())
        # checking if the stored value is present in the tasks list
        if the_value in tasks:
            # removing the task from the list
            tasks.remove(the_value)
            # calling the function to update the list
            list_update()
            # using the execute() method to execute a SQL statement
            the_cursor.execute('delete from tasks where title = ?', (the_value,))
    except:
        # displaying the message box with 'No Item Selected' message for an exception
        messagebox.showinfo('Error', 'No Task Selected. Cannot Delete.')

解释:

在上面的代码片段中,我们定义了一个名为 delete_task 的函数。在这个函数中,我们使用了 try-except 方法来使用 get() 方法从列表框中获取所选条目,并将其存储在 the_value 变量中。然后,我们检查了任务列表中的选定值,并将其从列表中删除。然后,我们调用 list_update() 函数来更新列表,并使用 execute() 方法执行SQL语句 ‘delete from tasks where title = ?’ ,并从 the_value 变量中删除该值。我们还使用 message box 显示了一个异常的 ‘No Task Selected’ 消息。

删除列表中的所有条目

现在,我们将定义一个函数,用于从列表中删除所有任务。让我们来看一下以下演示相同功能的代码片段。

文件:todo_application.py

# function to delete all tasks from the list
def delete_all_tasks():
    # displaying a message box to ask user for confirmation
    message_box = messagebox.askyesno('Delete All', 'Are you sure?')
    # if the value turns to be True
    if message_box == True:
        # using while loop to iterate through the tasks list until it's empty 
        while(len(tasks) != 0):
            # using the pop() method to pop out the elements from the list
            tasks.pop()
        # using the execute() method to execute a SQL statement
        the_cursor.execute('delete from tasks')
        # calling the function to update the list
        list_update()

解释:

在上面的代码片段中,我们定义了一个名为 delete_all_tasks 的函数。在这个函数内部,我们显示了一个消息框来询问用户是否确认。然后,我们使用 if 条件语句来检查值是否为True。然后,我们使用 while 循环来迭代tasks列表的元素,并使用 pop() 方法将其弹出。然后,我们使用 execute() 方法来执行SQL语句 ‘delete from tasks’ 。最后,我们调用 list_update() 函数来更新列表。

清空列表

现在,我们将定义一个函数来清空列表。以下代码片段展示了相同的操作。

文件:todo_application.py

# function to clear the list
def clear_list():
    # using the delete method to delete all entries from the list box
    task_listbox.delete(0, 'end')

解释:

在上面的代码片段中,我们定义了一个名为 clear_list 的函数。在这个函数中,我们使用 delete() 方法从列表框中删除所有条目。

关闭应用程序

我们现在将定义一个函数来关闭应用程序。我们还将在命令提示符中打印最终的列表。让我们考虑以下代码片段,它演示了相同的功能。

文件:todo_application.py

# function to close the application
def close():
    # printing the elements from the tasks list
    print(tasks)
    # using the destroy() method to close the application
    guiWindow.destroy()

解释:

在上面的代码片段中,我们定义了一个名为 close 的函数。在这个函数中,我们打印了用户的任务列表。然后,我们使用了 destroy() 方法来关闭应用程序。

从数据库中检索数据

现在,我们将定义一个从数据库中检索数据并将值附加到列表中的函数。以下是显示相同内容的代码片段。

文件:todo_application.py

# function to retrieve data from the database
def retrieve_database():
    # using the while loop to iterate through the elements in the tasks list
    while(len(tasks) != 0):
        # using the pop() method to pop out the elements from the list
        tasks.pop()
    # iterating through the rows in the database table
    for row in the_cursor.execute('select title from tasks'):
        # using the append() method to insert the titles from the table to the list
        tasks.append(row[0])

说明:

在上面的代码片段中,我们定义了一个函数 retrieve_database 。在这个函数内部,我们使用了while循环来迭代tasks列表中的元素,并借助pop()方法将它们弹出。然后,我们使用for循环迭代数据库表中的行,并使用append()方法将表中的值插入到列表中。

创建应用程序的主窗口

现在,我们已经为应用程序定义了所有必要的函数,接下来我们需要设计应用程序的主窗口,在这个窗口中,所有的小部件都将被显示出来。可以通过创建Tkinter库的Tk()类的对象来实现。

让我们考虑以下演示相同操作的代码片段。

文件:todo_application.py

# main function
if __name__ == "__main__":
    # creating an object of the Tk() class
    guiWindow = tk.Tk()
    # setting the title of the window
    guiWindow.title("To-Do List Manager - JAVATPOINT")
    # setting the geometry of the window
    guiWindow.geometry("500x500+750+250")
    # disabling the resizable option
    guiWindow.resizable(0, 0)
    # setting the background color to #FAEBD7
    guiWindow.configure(bg = "#FAEBD7")

说明:

在上面的代码片段中,我们创建了Tkinter库的Tk()类的对象。然后使用title()方法设置了窗口的标题。接下来使用geometry()方法设置了窗口的大小和位置。通过将resizable()方法的参数设置为零,禁用了可调整大小的选项,以获得更好的用户界面。最后,使用configure()方法将窗口的背景颜色设置为#FAEBD7。

将数据库添加到应用程序中

我们将在应用程序中添加数据库,该数据库将存储用户的数据。我们将开始使用connect()方法连接到数据库。然后创建了一个cursor类的对象,这将允许我们执行SQLite语句并从查询的结果集中获取数据。然后使用execute()方法执行SQL语句。

文件:todo_application.py

    # using the connect() method to connect to the database
    the_connection = sql.connect('listOfTasks.db')
    # creating an object of the cursor class
    the_cursor = the_connection.cursor()
    # using the execute() method to execute a SQL statement
    the_cursor.execute('create table if not exists tasks (title text)')

说明:

在上面的代码片段中,我们使用了 connect() 方法。然后我们创建了一个 cursor 类的对象。然后我们使用 execute() 方法执行SQL语句 ‘create table if not exists tasks (title text)’

添加必要的小部件到应用程序并应用事件触发器

现在我们已经成功地将数据库添加到应用程序中,是时候向主窗口添加必要的小部件并应用必要的事件触发器了。

我们将首先通过将框架添加到主窗口来为其他小部件提供合适的结构。然后,我们将在这些框架中添加一些标签以提供一些信息。然后,我们将添加一个输入字段,以便用户可以输入他们的任务。我们还将添加一些按钮,这些按钮执行特定的功能,例如将任务添加到列表中,从列表中删除一个任务,从列表中删除所有任务,并在点击时关闭应用程序。我们还将添加一个列表框来显示所有输入的任务。

现在让我们详细了解其实现。

框架

首先,我们将使用tkinter模块的 Frame() 小部件将框架添加到应用程序中。让我们来看下面的代码片段,演示了 Frame() 小部件的实现。

文件:todo_application.py

    # defining frames using the tk.Frame() widget
    header_frame = tk.Frame(guiWindow, bg = "#FAEBD7")
    functions_frame = tk.Frame(guiWindow, bg = "#FAEBD7")
    listbox_frame = tk.Frame(guiWindow, bg = "#FAEBD7")

    # using the pack() method to place the frames in the application
    header_frame.pack(fill = "both")
    functions_frame.pack(side = "left", expand = True, fill = "both")
    listbox_frame.pack(side = "right", expand = True, fill = "both")

解释:

在上面的代码片段中,我们使用了 Frame() 小部件为应用程序定义了一些框架。我们将这些小部件的 master 参数设置为 guiWindow ,这是 Tk() 类的对象,并且将 bg 参数指定为背景颜色为” #FAEBD7 “。然后,我们使用 pack() 方法将这些框架放置在应用程序中。

标签

现在,我们将使用 ttk 模块的 Label() 小部件向这些框架添加标签。这些标签将表示应用程序的标题和输入字段的标题。

让我们在下面的代码片段中了解标签的实现。

文件:todo_application.py

    # defining a label using the ttk.Label() widget
    header_label = ttk.Label(
        header_frame,
        text = "The To-Do List",
        font = ("Brush Script MT", "30"),
        background = "#FAEBD7",
        foreground = "#8B4513"
    )
    # using the pack() method to place the label in the application
    header_label.pack(padx = 20, pady = 20)

    # defining another label using the ttk.Label() widget
    task_label = ttk.Label(
        functions_frame,
        text = "Enter the Task:",
        font = ("Consolas", "11", "bold"),
        background = "#FAEBD7",
        foreground = "#000000"
    )
    # using the place() method to place the label in the application
    task_label.place(x = 30, y = 40)

解释:

在上面的代码片段中,我们使用了 ttk 模块的 Label() 小部件来定义一个标签,将其 master 参数设置为之前定义的框架 header_frame 。我们还设置了文本、字体样式和大小、背景色和前景色。然后我们使用了 pack() 方法,并设置了此标签在窗口上的位置。然后,我们使用了 ttk 模块的 Label() 小部件定义了另一个标签,并将其 master 参数设置为之前定义的另一个框架 functions_frame 。我们再次设置了文本、字体样式和大小、背景色和前景色。然后,我们使用了 place() 方法来设置此标签在主窗口上的位置。

输入框

我们现在将使用 Tkinter 库的 ttk 模块的 Entry() 小部件在应用程序中添加一个输入框。此输入框将允许我们在列表中插入任务标题。

让我们来考虑下面的代码片段,演示了 Entry() 小部件的实现。

文件: todo_application.py

    # defining an entry field using the ttk.Entry() widget
    task_field = ttk.Entry(
        functions_frame,
        font = ("Consolas", "12"),
        width = 18,
        background = "#FFF8DC",
        foreground = "#A52A2A"
    )
    # using the place() method to place the entry field in the application
    task_field.place(x = 30, y = 80)

说明:

在上面的代码片段中,我们使用ttk模块的Entry()小部件定义了一个输入字段,并将该小部件的master参数设置为我们之前定义的一个名为functions_frame的帧。我们还设置了小部件的字体样式和大小以及宽度。我们还设置了输入字段的背景色和前景色。然后,我们使用place()方法将这个输入字段放置在应用程序中。

按钮

现在是时候为应用程序添加一些按钮来执行之前定义的某些功能了。我们可以使用Tkinter库的ttk模块的Button()小部件来创建这些按钮。

以下是代码片段,展示了按钮的创建。

文件:todo_application.py

    # adding buttons to the application using the ttk.Button() widget
    add_button = ttk.Button(
        functions_frame,
        text = "Add Task",
        width = 24,
        command = add_task
    )
    del_button = ttk.Button(
        functions_frame,
        text = "Delete Task",
        width = 24,
        command = delete_task
    )
    del_all_button = ttk.Button(
        functions_frame,
        text = "Delete All Tasks",
        width = 24,
        command = delete_all_tasks
    )
    exit_button = ttk.Button(
        functions_frame,
        text = "Exit",
        width = 24,
        command = close
    )
    # using the place() method to set the position of the buttons in the application
    add_button.place(x = 30, y = 120)
    del_button.place(x = 30, y = 160)
    del_all_button.place(x = 30, y = 200)
    exit_button.place(x = 30, y = 240)

说明:

在上面的代码片段中,我们使用ttk模块的Button()小部件定义了一些按钮。我们将它们的master参数设置为之前定义的functions_frame,一个框架。我们还设置了这些按钮将显示的文本以及每个按钮的宽度。然后,我们将command参数的值设置为它们将调用的函数。然后,我们使用place()方法并设置它们在主窗口上的位置。

列表框

我们现在将使用tkinter库的Listbox()小部件向应用程序添加一个列表框。这个列表框将显示任务列表。让我们考虑以下代码片段,说明了列表框的实现。

文件:todo_application.py

    # defining a list box using the tk.Listbox() widget
    task_listbox = tk.Listbox(
        listbox_frame,
        width = 26,
        height = 13,
        selectmode = 'SINGLE',
        background = "#FFFFFF",
        foreground = "#000000",
        selectbackground = "#CD853F",
        selectforeground = "#FFFFFF"
    )
    # using the place() method to place the list box in the application
    task_listbox.place(x = 10, y = 20)

解释:

在上面的代码片段中,我们使用了tkinter模块的Listbox()小部件。我们将此小部件的master参数设置为之前定义的一个名为listbox_frame的框架。我们设置了列表框的宽度和高度。我们还将selectmode参数设置为SINGLE,以便每次只选择一个条目。然后,我们设置了选定实体的背景和前景色。然后,我们使用place()方法将列表框放置在应用程序中。

调用函数

我们现在将调用函数来检索数据库并更新列表。我们还将使用mainloop()方法运行应用程序并与数据库建立连接。

让我们考虑以下代码片段来演示相同的情况。

文件:todo_application.py

    # calling some functions
    retrieve_database()
    list_update()
    # using the mainloop() method to run the application
    guiWindow.mainloop()
    # establishing the connection with database
    the_connection.commit()
    the_cursor.close()

解释:

在上面的代码片段中,我们调用了 retrieve_database() 函数和 list_update() 函数。然后,我们使用guiWindow来调用 mainloop() 方法,guiWindow是Tk()类的一个对象,用于运行应用程序。然后,我们使用 commit() 方法和 close() 方法来建立与数据库的连接。

因此,项目代码现在已经完成。我们可以保存文件并在命令提示符或终端中运行以下命令来查看输出。

语法:

$ python todo_application.py

在我们看到输出之前,让我们来考虑一下Python中“待办事项列表GUI应用程序”项目的全部代码。

完整的项目代码

以下是Python编程语言中“待办事项列表GUI应用程序”项目的代码片段。

文件:todo_application.py

# importing the required modules
import tkinter as tk                    # importing the tkinter module as tk
from tkinter import ttk                 # importing the ttk module from the tkinter library
from tkinter import messagebox          # importing the messagebox module from the tkinter library
import sqlite3 as sql                   # importing the sqlite3 module as sql

# defining the function to add tasks to the list
def add_task():
    # getting the string from the entry field
    task_string = task_field.get()
    # checking whether the string is empty or not
    if len(task_string) == 0:
        # displaying a message box with 'Empty Field' message
        messagebox.showinfo('Error', 'Field is Empty.')
    else:
        # adding the string to the tasks list
        tasks.append(task_string)
        # using the execute() method to execute a SQL statement
        the_cursor.execute('insert into tasks values (?)', (task_string ,))
        # calling the function to update the list
        list_update()
        # deleting the entry in the entry field
        task_field.delete(0, 'end')

# defining the function to update the list
def list_update():
    # calling the function to clear the list
    clear_list()
    # iterating through the strings in the list
    for task in tasks:
        # using the insert() method to insert the tasks in the list box
        task_listbox.insert('end', task)

# defining the function to delete a task from the list
def delete_task():
    # using the try-except method
    try:
        # getting the selected entry from the list box
        the_value = task_listbox.get(task_listbox.curselection())
        # checking if the stored value is present in the tasks list
        if the_value in tasks:
            # removing the task from the list
            tasks.remove(the_value)
            # calling the function to update the list
            list_update()
            # using the execute() method to execute a SQL statement
            the_cursor.execute('delete from tasks where title = ?', (the_value,))
    except:
        # displaying the message box with 'No Item Selected' message for an exception
        messagebox.showinfo('Error', 'No Task Selected. Cannot Delete.')      

# function to delete all tasks from the list
def delete_all_tasks():
    # displaying a message box to ask user for confirmation
    message_box = messagebox.askyesno('Delete All', 'Are you sure?')
    # if the value turns to be True
    if message_box == True:
        # using while loop to iterate through the tasks list until it's empty 
        while(len(tasks) != 0):
            # using the pop() method to pop out the elements from the list
            tasks.pop()
        # using the execute() method to execute a SQL statement
        the_cursor.execute('delete from tasks')
        # calling the function to update the list
        list_update()

# function to clear the list
def clear_list():
    # using the delete method to delete all entries from the list box
    task_listbox.delete(0, 'end')

# function to close the application
def close():
    # printing the elements from the tasks list
    print(tasks)
    # using the destroy() method to close the application
    guiWindow.destroy()

# function to retrieve data from the database
def retrieve_database():
    # using the while loop to iterate through the elements in the tasks list
    while(len(tasks) != 0):
        # using the pop() method to pop out the elements from the list
        tasks.pop()
    # iterating through the rows in the database table
    for row in the_cursor.execute('select title from tasks'):
        # using the append() method to insert the titles from the table in the list
        tasks.append(row[0])

# main function
if __name__ == "__main__":
    # creating an object of the Tk() class
    guiWindow = tk.Tk()
    # setting the title of the window
    guiWindow.title("To-Do List Manager - JAVATPOINT")
    # setting the geometry of the window
    guiWindow.geometry("500x450+750+250")
    # disabling the resizable option
    guiWindow.resizable(0, 0)
    # setting the background color to #FAEBD7
    guiWindow.configure(bg = "#FAEBD7")

    # using the connect() method to connect to the database
    the_connection = sql.connect('listOfTasks.db')
    # creating the cursor object of the cursor class
    the_cursor = the_connection.cursor()
    # using the execute() method to execute a SQL statement
    the_cursor.execute('create table if not exists tasks (title text)')

    # defining an empty list
    tasks = []

    # defining frames using the tk.Frame() widget
    header_frame = tk.Frame(guiWindow, bg = "#FAEBD7")
    functions_frame = tk.Frame(guiWindow, bg = "#FAEBD7")
    listbox_frame = tk.Frame(guiWindow, bg = "#FAEBD7")

    # using the pack() method to place the frames in the application
    header_frame.pack(fill = "both")
    functions_frame.pack(side = "left", expand = True, fill = "both")
    listbox_frame.pack(side = "right", expand = True, fill = "both")

    # defining a label using the ttk.Label() widget
    header_label = ttk.Label(
        header_frame,
        text = "The To-Do List",
        font = ("Brush Script MT", "30"),
        background = "#FAEBD7",
        foreground = "#8B4513"
    )
    # using the pack() method to place the label in the application
    header_label.pack(padx = 20, pady = 20)

    # defining another label using the ttk.Label() widget
    task_label = ttk.Label(
        functions_frame,
        text = "Enter the Task:",
        font = ("Consolas", "11", "bold"),
        background = "#FAEBD7",
        foreground = "#000000"
    )
    # using the place() method to place the label in the application
    task_label.place(x = 30, y = 40)

    # defining an entry field using the ttk.Entry() widget
    task_field = ttk.Entry(
        functions_frame,
        font = ("Consolas", "12"),
        width = 18,
        background = "#FFF8DC",
        foreground = "#A52A2A"
    )
    # using the place() method to place the entry field in the application
    task_field.place(x = 30, y = 80)

    # adding buttons to the application using the ttk.Button() widget
    add_button = ttk.Button(
        functions_frame,
        text = "Add Task",
        width = 24,
        command = add_task
    )
    del_button = ttk.Button(
        functions_frame,
        text = "Delete Task",
        width = 24,
        command = delete_task
    )
    del_all_button = ttk.Button(
        functions_frame,
        text = "Delete All Tasks",
        width = 24,
        command = delete_all_tasks
    )
    exit_button = ttk.Button(
        functions_frame,
        text = "Exit",
        width = 24,
        command = close
    )
    # using the place() method to set the position of the buttons in the application
    add_button.place(x = 30, y = 120)
    del_button.place(x = 30, y = 160)
    del_all_button.place(x = 30, y = 200)
    exit_button.place(x = 30, y = 240)

    # defining a list box using the tk.Listbox() widget
    task_listbox = tk.Listbox(
        listbox_frame,
        width = 26,
        height = 13,
        selectmode = 'SINGLE',
        background = "#FFFFFF",
        foreground = "#000000",
        selectbackground = "#CD853F",
        selectforeground = "#FFFFFF"
    )
    # using the place() method to place the list box in the application
    task_listbox.place(x = 10, y = 20)

    # calling some functions
    retrieve_database()
    list_update()
    # using the mainloop() method to run the application
    guiWindow.mainloop()
    # establishing the connection with database
    the_connection.commit()
    the_cursor.close()

输出:

['Exercise', 'Practice Coding', 'Complete Assignment', 'Shopping']

在Python中的简单待办事项列表GUI应用程序

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程