Python Tkinter section

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 How to create a a label in python tkinter with string text - code picture?
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. How to create a label with a text as a variable python tkinger - code picture?

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

Then, after importing the holidayapi library, you can create a v1 object with your given key as a parameter, then call the holidays method, with set of other parameters like the country, the year, the day and the month of the holidays to retrieve. The method will make the request, that will return the json data object, containing the desired holidays, if there was no error. Otherwise, it will contain an indicated of an error. how to use the holiday api in python- code picture?

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.
  • Create a button in python that calls a function - code picture?
  • The function getFHoliday will:
    1. get the values of the day and the month of the corresponding Entry widgets.
    2. 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
    3. from the returned json object, it will get the holidays property, which is an array of dictionaries that will contain the desired holidays. We will display only the first holiday. So, we will get the value of the name key from the first dictionary in that array (if the array is not empty). That value will correspond to the name of the holiday.
    4. set the value of the text property of the holidayLabel with the word Happy and the name of the holiday, by calling the configure method of that label.
get the holiday of a date with holiday api and display it on a label with python - code picture?



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_windowtext=theDate,
                fg="white"bg="purple",
                font=('Tahoma',20,"bold"))
todayLabel.place(relx0.05rely=0.05)    

dayLabel Label(main_window,text="Day",
                 fg="white",bg="purple")
dayLabel.place(relx=0.5rely=0.2)
dayEntry Entry(main_window)
dayEntry.place(relx=0.6rely=0.2)

monthLabel Label(main_window,text="Month",
                fg="white",bg="purple")
monthLabel.place(relx=0.5rely=0.3)
monthEntry Entry(main_window)
monthEntry.place(relx=0.6rely=0.3)

yearLabel Label(main_window,text="Year 2022",
                fg="white",bg="purple",
                font=("Tahoma",14,"normal"))
yearLabel.place(relx=0.5rely=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.5rely=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 🙂.