Professional Documents
Culture Documents
Entry box: Users will type the task in the entry box which
further will be displayed in the Listbox.
Messagebox: To display an error message when the user
clicks on the add button with an empty entry box.
Source code for the ToDo list in Python Tkinter. The entire
code is explained in a sequence of creation.
ws = Tk()
ws.geometry('500x450+500+200')
ws.title('PythonGuides')
ws.config(bg='#223441')
ws.resizable(width=False, height=False)
....
....
ws.mainloop()
lb.config(yscrollcommand=sb.set)
sb.config(command=lb.yview)
my_entry.pack(pady=20)
delTask_btn = Button(
button_frame,
text='Delete Task',
font=('times 14'),
bg='#ff8b61',
padx=20,
pady=10,
command=deleteTask
)
delTask_btn.pack(fill=BOTH, expand=True, side=LEFT)
We have stored the value of the entry box in the task variable
get() method is used to pull the value provided by the user in
the entry box.
If-else condition is applied to avoid black space entry in the
Listbox.
if the task does not have blank space then only it will allow it
to store the information in the Listbox otherwise it will
display a warning message box informing the user that the
entry box can’t be empty.
def newTask():
task = my_entry.get()
if task != "":
lb.insert(END, task)
my_entry.delete(0, "end")
else:
messagebox.showwarning("warning", "Please enter some
task.")
def deleteTask():
lb.delete(ANCHOR)
Complete Source Code:
def newTask():
task = my_entry.get()
if task != "":
lb.insert(END, task)
my_entry.delete(0, "end")
else:
messagebox.showwarning("warning", "Please enter some
task.")
def deleteTask():
lb.delete(ANCHOR)
ws = Tk()
ws.geometry('500x450+500+200')
ws.title('PythonGuides')
ws.config(bg='#223441')
ws.resizable(width=False, height=False)
frame = Frame(ws)
frame.pack(pady=10)
lb = Listbox(
frame,
width=25,
height=8,
font=('Times', 18),
bd=0,
fg='#464646',
highlightthickness=0,
selectbackground='#a6a6a6',
activestyle="none",
)
lb.pack(side=LEFT, fill=BOTH)
task_list = [
'Eat apple',
'drink water',
'go gym',
'write software',
'write documentation',
'take a nap',
'Learn something',
'paint canvas'
]
sb = Scrollbar(frame)
sb.pack(side=RIGHT, fill=BOTH)
lb.config(yscrollcommand=sb.set)
sb.config(command=lb.yview)
my_entry = Entry(
ws,
font=('times', 24)
)
my_entry.pack(pady=20)
button_frame = Frame(ws)
button_frame.pack(pady=20)
addTask_btn = Button(
button_frame,
text='Add Task',
font=('times 14'),
bg='#c5f776',
padx=20,
pady=10,
command=newTask
)
addTask_btn.pack(fill=BOTH, expand=True, side=LEFT)
delTask_btn = Button(
button_frame,
text='Delete Task',
font=('times 14'),
bg='#ff8b61',
padx=20,
pady=10,
command=deleteTask
)
delTask_btn.pack(fill=BOTH, expand=True, side=LEFT)
ws.mainloop()
Output:
############# DRIVER CODE#############
#import module
from tkinter import *
from tkinter import messagebox
#5Create functions
def newTask():
task = my_entry.get()
if task != "" :
lb.insert(END, task)
my_entry.delete(0, "end")
else:
messagebox.showwarning("Warning!, Please enter a
valid task")
def deleteTask():
lb.delete(ANCHOR)
#2 configure and create main window
root = Tk()
root.title('To Do List')
root.geometry('500x450+500+200')
root.config(bg='#223344')
root.resizable(width=False, height=False)
frame = Frame(root)
frame.pack(pady=10)
lb = Listbox(
frame,
width=25,
height=8,
font=('Times', 18),
bd=0,
fg='#464646',
highlightthickness = 0,
selectbackground = '#a6a6a6',
activestyle="none",
)
lb.pack(side=LEFT, fill=BOTH)
task_list = [
'Eat Apple',
'Drink Water',
'Go Gym',
'Write Code',
'Write Documentation',
'Tak a nap',
'Learn something new',
'Paint canvas'
]
sb = Scrollbar(frame)
sb.pack(side=RIGHT, fill=BOTH)
lb.config(yscrollcommand=sb.set)
sb.config(command=lb.yview)
button_frame = Frame(root)
button_frame.pack(pady=20)
addTask_btn = Button(
button_frame,
text = 'Add Task',
font=('Times', 14),
bg='#c5f776',
padx = 20,
pady = 10,
command = newTask
)
addTask_btn.pack(fill=BOTH, expand=True, side=LEFT)
delTask_btn = Button(
button_frame,
text='Delete Task',
font=('times', 24),
bg='#ff8b61',
padx=20,
pady=10,
command=deleteTask
)
delTask_btn.pack(fill=BOTH, expand=True, side=LEFT)