How to display with a random word List the same random word on a specific date

51 views Asked by At

Im making a Calendar App with Python Tkinter and I have already found out how to save a user input to a date and how to display it. So I'm trying to make a daily Quote Button. But for that it should on one date always display the same Quoete because its a Quotes of the day. For that I used the same method I used for the user input. But now if I click a second time on a day it doesnt display the same Quote it simple displays: .!Toplevel. And I really dont found a answer.

This is a simplified version from my code:

from tkinter import *
from tkcalendar import *
import datetime
import secrets

root = Tk()
Quotes_dict = {}

today = datetime.date.today()
cal = Calendar(root, selectmode="day", year=today.year, month=today.month, day=today.day)
Calendar.date.day
cal.place(x=0, y=0, height=600, width=1500)

list= ['Hi', 'HI2']


def random(quotes):
    return secrets.choice(quotes)


def Quotes():
    Quotes_fenster = Toplevel(root)
    app_width = 1000
    app_height = 100
    Quotes_fenster.geometry(f'{app_width}x{app_height}+{125}+{10}')
    Quotes_fenster.resizable(False, False)
    Quotes_fenster.title("Zitate")

    Quotes_zitat = Label(Quotes_fenster, text="", font=18)
    Quotes_zitat.pack()

    datum = str(cal.get_date())
    try:
        if event := Quotes_dict[datum]:
            Quotes_zitat.config(text=f'{event}')
    except Exception as e:

        Quotes_oftheday = random(list)
        Quotes_dict[datum] = Quotes_fenster
        Quotes_zitat.config(text=f'{ Quotes_oftheday}')

button = Button(root,text=" Quotes", command=Quotes)
button.pack()
root.mainloop()

I hope you understand what I'm saying

1

There are 1 answers

0
Jeffrey Bonde On
Quotes_dict[datum] = Quotes_fenster

What you are doing here is saving the Quotes_fenster object, which is a Tk.TopLevel instance, into your Quotes_dict.

When you click the button a second time on the same day

if event := Quotes_dict[datum]:
   Quotes_zitat.config(text=f'{event}')

This retrieves the Quotes_fenster object from the last button press, converts to string and then puts it into the Tk.Label's text. In Tkinter, the string representation of objects is the period-delimited list of inherited types of the object; in this case .!TopLevel for the Quotes_fenster object.

What you probably intended was

Quotes_dict[datum] = Quotes_oftheday