How to get specific Information from a dictionary

72 Views Asked by At

I'm trying to make a calendar with Python Tkinter where I can make entries and save those with the date I choose. To save them I use a dictionary in that I could make more than 1 entry. (If there is another way please tell me)How can I now define which entry I want to display on the screen Like when one entry is 20.02.2023: Dentist and the other is 12.02.2023: Doctor? How can I display f.e the Doctor? I use the Tkinter calendar preset so I didn't make the calendar by myself so I save my date with the get. date function but in the end, it comes to the same. I am also not sure if I should use two dictionary's or 1 so for the date one and the entry one or for both the same.

Here is a simplified version of my code:

from tkinter import*
from tkcalendar import *
import datetime
from tkinter.ttk import *

root = Tk()
root.title("Calendar")
root.attributes("-fullscreen", True)

dictionary_date = {}
dictionary_entry = {}

#here it doesnt work and I really dont know how to fix it
def output_entry():
    if date == cal.get_date():
        label.config(text="Today is the" + str(cal.get_date()) + " und your entry is: " + str(dictionary_entry))
    else:
        label.config(text="You have no entry today")
def save_entry():
    global date
    entry_information = entry.get()
    date = cal.get_date()
    dictionary_date[""] = date
    dictionary_entry[""] = entry_information


# this is the code for the calendar itself
today = datetime.date.today()
cal = Calendar(root, selectmode="day", year=today.year, month=today.month, day=today.day)
Calendar.date.day
cal.pack(fill="both", expand=True)

#Code for the entry and the label where the entry should stand
entry = Entry(root, width=35)
entry.pack()
label = Label(root, text="")
label.pack(pady=20)

button_save = Button(root, text="Save Entry with date", command=save_entry)
button_save.pack()
button_output = Button(root, text= "Get Entry and Date", command= output_entry)
button_output.pack()

root.mainloop()

Like you see I cant get both dictionary and can't get both informations.

1

There are 1 best solutions below

1
On

If I understand correctly, it seems to me that you can get away with using a single dictionary with the key:value structure of {date:entry}. Note that dictionary keys in Python have to be strings, while their values can be just about any type.

To that end, when you're saving an entry you would just convert the date specified into a string to act as the 'key' and save the entry_information as the 'value'.

calendar_dict {}  # initialize a dictionary for calendar information


def output_entry():
    date = str(cal.get_date())  # get the selected date as a string
    if event := calendar_dict[date]:  # check in the dictionary for events on this date
        label.config(text=f'Today is the {date} und your entry is: {event}')
    else:
        label.config(text="You have no entry today")


def save_entry():
    date = str(cal.get_date())  # get the selected date as a string
    entry_information = entry.get()  # get the event info
    calendar_dict[date] = entry_information  # save event to dict

Note that as written, only one event can be saved on a given date. Calling save_entry on the same date more than once will overwrite any existing values in calendar_dict