Question: I have the following code for a python tkinter GUI event handling calendar, Unfortunately my functions are not working togethor properly, I am getting variable

I have the following code for a python tkinter GUI event handling calendar, Unfortunately my functions are not working togethor properly, I am getting variable undefined errors and others, can you please help to fix this code so that it actually ADDS, SAVES, VIEWS, EDITS, and DELETES events when the buttons are pressed.
#MAIN WINDOW ITEMS
# Create a dictionary to store events
events ={}
# Create a listbox to display events
listbox_events = tk.Listbox(root, width=50, height=5)
listbox_events.pack()
# Define a function to check for existing events
# Get the list of events from the listbox
# Check if the event title already exists in the list
def check_for_existing_events(listbox_events, event_title, cal):
date_selected = cal.get_date()
# Get the list of events from the listbox
events = listbox_events.get(0, tk.END)
# Check if the event title already exists in the list
for event in events:
if event.startswith(event_title):
return True
return False
# Factory function to manage the state of the variables
def event_manager():
# Encapsulated variables
listbox_events = None
event_title = None
event_time = None
event_descript = None
date_selected = None
def set_variables(lb_events, e_title, e_time, e_descript, date_sel):
nonlocal listbox_events, event_title, event_time, event_descript, date_selected
listbox_events = lb_events
event_title = e_title
event_time = e_time
event_descript = e_descript
date_selected = date_sel
def get_variables():
return listbox_events, event_title, event_time, event_descript, date_selected
# Define a function to ADD an event
def add_event(cal):
nonlocal date_selected, event_title, event_time, event_descript
date_selected = cal.get_date()
event_title = entry_event_title.get()
event_time = entry_event_time.get()
event_descript = entry_event_descript.get()
if not event_title or not event_time:
messagebox.showwarning("Warning", "Please enter an event title and event time.")
return
elif check_for_existing_events(listbox_events, event_title, cal):
messagebox.showinfo("Event exists", "Event exists, please select another action.")
return
else:
response = messagebox.askyesno("Add Event", "Do you want to add an event?")
if response:
open_event_management_window()
return set_variables, get_variables, add_event
# Define a function to SAVE an event
def save_event(cal):
nonlocal date_selected, event_title, event_time, event_descript
date_selected = cal.get_date()
event_title = entry_event_title.get()
event_time = entry_event_time.get()
event_descript = entry_event_descript.get()
if event_title and event_time:
event_date = cal.selection_get()
events[event_title]={"Time": event_time, "Date": event_date, "Description": event_descript}
listbox_events.insert(tk.END, event_title)
highlight_event(event_title, listbox_events)
else:
messagebox.showwarning("Warning", "Event title and time cannot be empty.")
# Define a function to HIGHLIGHT an event
def highlight_event(event_title, listbox_events):
listbox_events.itemconfig(listbox_events.size()-1,{'bg':'cyan'})
# Define a function to SAVE events to a file
def save_events_to_file(filename, listbox_events):
with open(filename,"w") as file:
for event in listbox_events.get(0, tk.END):
file.write(event +"
")
# Define a function to VIEW events
def view_event(listbox_events, events):
selected_event = event_listbox.get(tk.ANCHOR)
if selected_event:
event_info = events[selected_event]
messagebox.showinfo("Event Details", f"Title: {selected_event}
Time: {event_info["Time"]}
Date: {event_info["Date"]}
Description: {event_info["Description"]}")
# Create a function to EDIT an event
def edit_event(listbox_events, cal, event_time, event_title):
date_selected = cal.get_date()
selected_event = event_listbox.get(tk.ANCHOR)
if selected_event:
event_time_old = selected_event.split("-")[0]
# Find the event and update its time and title
for event in events.get(date_selected, []):
if event["event time"]== event_time_old:
event["event time"]= event_time
event["event title"]= event_title
messagebox.showinfo("Event Updated", f"Event updated for {date_selected}: {event_title} at {event_time}")
return
messagebox.showinfo("Event Not Found", f"No event found for {date_selected}")
else:
messagebox.showwarning("Warning", "Please select an event to edit.")
# Create a function to DELETE an event
# Find and delete the event from the events dictionary
# Update the listbox
def deletuntitled *- Sublime Text (UNREGISTERED)
untitled
#MAIN WINDOW ITEMS # Create a dic
I have the following code for a python tkinter

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!