
Say Happy Day with A Python Label
Post by Amina Delali, January 16th, 2023
Description
The Label TK widget in Tkinter is used to display a text or an image on the parent window or widget.
To define the text that is going to be displayed, we just set the text property to that text.
To define the image to display, we just set the image property to a photoimage object for example, that will encapsulate image
In the code below, we are going to display on a window the date of today, and we will also display the message formed with the word Happy and the name of the holiday of a certain day in a certain month, of the year 2022. I will explain later, why the year 2022.The values of the day, and the month are given by the user, by entering the values in the corresponding Entry widget. If nothing is displayed, it will mean that the corresponding holiday doesn't exist, or that the entered values are not valid.
You can set the text property of the label right while creating the label. The value of the text can be known in advance, like the text of the labels that we are going to use next to the Entry widgets used to enter the value of the day and the month . Here in the code, they are dayLabel and monthLabel. There is also yearLabel, that is used to indicate the year 2022
Or it can be unknown, but defined in a variable, that will have a value once the code is run. In our case, it is todayLabel, that is used to display the date of the day.
The value of the text may also change during the execution of the code. Here, we have holidayLabel, that will display the message Happy, with the name of the holiday. At the beginning of the execution, the text will be an empty string.
To get the date of today, we will simply call the today method, of the date class, from the datetime module. It will return a date object containing the date of today. Then, we will call the method strftime from that returned object, to get a formatted string, from the date of today, and put it in the theDate variable. We will assign this variable to the text property of todayLabel.
Before displaying the holiday of the selected day, we will use an api called Holiday API to retrieve the list of international holidays, for the year 2022.
To be able to use the API in python, you have first to install the python-holidayapi package.
pip install python-holidayapi

We are using the free version of the API, so our key will allow us to access only the archive of the year 2022. For example, today it is supposed to be Martin Luther King Jr day. But, since we are using the 2022 dates, it is going to be the 17th of January. This is why, we enter the value 17 in the day entry to display that holiday.
To retrieve and display the holiday corresponding to the entered values, we will:
- Create the showFHButton that has the getFHoliday function, as the command property.
-
The function getFHoliday will:
- get the values of the day and the month of the corresponding Entry widgets.
-
call the holidays method, from the API object created previously, with the following parameter:
- the retrieved day and month
- the year 2022
- and the country as US


The Code
from datetime import date
from tkinter import *
import holidayapi
main_window=Tk()
main_window.title("Say Happy Day with A Python Label")
main_window.geometry('500x450+400+100')
main_window.configure(bg="purple")
key = 'YOUR_HOLIDAYS_API_CODE'
hapi = holidayapi.v1(key)
today = date.today()
theDate = today.strftime("%A, %B %d, %Y")
todayLabel = Label(main_window, text=theDate,
fg="white", bg="purple",
font=('Tahoma',20,"bold"))
todayLabel.place(relx= 0.05, rely=0.05)
dayLabel = Label(main_window,text="Day",
fg="white",bg="purple")
dayLabel.place(relx=0.5, rely=0.2)
dayEntry = Entry(main_window)
dayEntry.place(relx=0.6, rely=0.2)
monthLabel = Label(main_window,text="Month",
fg="white",bg="purple")
monthLabel.place(relx=0.5, rely=0.3)
monthEntry = Entry(main_window)
monthEntry.place(relx=0.6, rely=0.3)
yearLabel = Label(main_window,text="Year 2022",
fg="white",bg="purple",
font=("Tahoma",14,"normal"))
yearLabel.place(relx=0.5, rely=0.4)
holidayLabel = Label(main_window,text="",
bg="purple",fg="gold",
wraplength=400,justify="left",
font=("Tahoma",30,"bold"))
holidayLabel.place(relx=0.05,rely=0.6)
def getFHoliday():
day = dayEntry.get()
month = monthEntry.get()
holidays = hapi.holidays({
'country': 'US',
'year': '2022',
'month': month,
'day': day
})
if holidays['holidays']:
firstH = holidays['holidays'][0]['name']
holidayLabel.configure(text="Happy\n"+ firstH)
else:
holidayLabel.configure(text="")
showFHButton = Button(main_window,text="Show First Holiday",
bg="gold",
command=getFHoliday)
showFHButton.place(relx=0.5, rely=0.5)
main_window.mainloop()
The Output
Something to say?
If you want to add something about this post, please feel free to do it by commenting below 🙂.