Home Python Project How to Build a GUI Calendar Using Python

How to Build a GUI Calendar Using Python

by anupmaurya
57 minutes read

In this article, we will learn How to Build a GUI Calendar Using Python with help of tkinter module.

Python offers multiple options for developing a GUI (Graphical User Interface). Out of all the GUI methods, Tkinter is the most commonly used method. Python with Tkinter outputs the fastest and easiest way to create GUI applications. 

To create GUI Calendar Using Python, we will need

  • To import two Python modules one for creating GUI and another to get year data.
  • For GUI we will use "tkinter module" because it’s very easy to learn and use.
  • And to get any year data we will use "calendar module".

To install Tkinter and calendar module, go to terminal and type,

pip install tkinter
pip install calendar module

Code with comments for GUI calendar using Python

from tkinter import *
import tkinter as tk
from PIL import ImageTk, Image
import calendar
root = tk.Tk()
root.geometry('400x300')
root.title('Calender-Techarge')
root.iconbitmap("calender-ico.ico")


def show():

        m = int(month.get())
        y = int(year.get())
        output = calendar.month(y,m)

        cal.insert('end',output)

def clear():
        cal.delete(1.0,'end')

def exit():
        root.destroy()


img = ImageTk.PhotoImage(Image.open('calendar.png'))
label = Label(image=img)
label.place(x=170,y=3)



m_label = Label(root,text="Month",font=('verdana','10','bold'))
m_label.place(x=70,y=80)

month = Spinbox(root, from_= 1, to = 12,width="5") 
month.place(x=140,y=80) 
  
y_label = Label(root,text="Year",font=('verdana','10','bold'))
y_label.place(x=210,y=80)

year = Spinbox(root, from_= 2020, to = 3000,width="8") 
year.place(x=260,y=80) 


cal = Text(root,width=33,height=8,relief=RIDGE,borderwidth=2)
cal.place(x=70,y=110)

show = Button(root,text="Show",font=('verdana',10,'bold'),relief=RIDGE,borderwidth=2,command=show)
show.place(x=140,y=250)

clear = Button(root,text="Clear",font=('verdana',10,'bold'),relief=RIDGE,borderwidth=2,command=clear)
clear.place(x=200,y=250)

exit = Button(root,text="Exit",font=('verdana',10,'bold'),relief=RIDGE,borderwidth=2,command=exit)
exit.place(x=260,y=250)
root.mainloop()

Explanation of Code for How to Build a GUI Calendar Using Python

Let’s a breakdown of the code

1. Imports:

  • from tkinter import *: Imports everything from the tkinter module for GUI development.
  • import tkinter as tk: Imports tkinter and assigns it to the alias tk for shorter usage. (This line is redundant with the previous line)
  • from PIL import ImageTk, Image: Imports functionalities from Pillow (PIL Fork) library to handle images.

2. Window Setup:

  • root = tk.Tk(): Creates the main window of the application.
  • root.geometry('400x300'): Sets the window size to 400×300 pixels.
  • root.title('Calender-Techarge'): Sets the title of the window.
  • root.iconbitmap("calender-ico.ico"): Sets the icon for the window (requires a “calender-ico.ico” file in the same directory).

3. Function Definitions:

  • show(): This function is called when the “Show” button is clicked.
    • It retrieves the selected month from the month Spinbox and converts it to an integer using int(month.get()).
    • It retrieves the selected year from the year Spinbox and converts it to an integer using int(year.get()).
    • It uses the calendar.month(y, m) function to generate a calendar month string for the provided year (y) and month (m).
    • It inserts the generated calendar month string into the cal Text widget using the insert('end', output) method, placing the text at the end.
  • clear(): This function is called when the “Clear” button is clicked.
    • It simply deletes all the content from the cal Text widget using the delete(1.0, 'end') method. This removes everything from the first line (1.0) to the end.
  • exit(): This function is called when the “Exit” button is clicked.
    • It destroys the main window (root) using the destroy() method, effectively closing the application.

4. GUI Elements:

  • img = ImageTk.PhotoImage(Image.open('calendar.png')): Loads a calendar image (calendar.png) and converts it to a format usable by Tkinter using ImageTk.PhotoImage.
  • label = Label(image=img): Creates a label widget and assigns the loaded calendar image to it.
  • label.place(x=170, y=3): Positions the label widget on the window at coordinates (170, 3).
  • m_label = Label(root,text="Month",font=('verdana','10','bold')): Creates a label widget with the text “Month” and sets its font properties.
  • m_label.place(x=70,y=80): Positions the month label on the window at coordinates (70, 80).
  • month = Spinbox(root, from_= 1, to = 12,width="5"): Creates a Spinbox widget that allows users to select a month value between 1 and 12. It sets the width to 5 characters.
  • month.place(x=140,y=80): Positions the month Spinbox on the window at coordinates (140, 80).

Similar logic is used to create the year Spinbox label (y_label) and Spinbox (year).

  • cal = Text(root,width=33,height=8,relief=RIDGE,borderwidth=2): Creates a Text widget for displaying the calendar month. It sets the width to 33 characters, height to 8 lines, and adds a raised border style with a width of 2 pixels.
  • cal.place(x=70,y=110): Positions the Text widget on the window at coordinates (70, 110).
  • show = Button(...): Creates a button with the text “Show” and assigns the show function to be called when clicked.
  • Similar buttons (clear and exit) are created for clear and exit functionalities with their respective functions assigned. These buttons are positioned on the window using the place method.

5. Main Loop:

  • root.mainloop(): Starts the main event loop for the Tkinter application. This listens for user interactions with the window elements.

Output

Python | GUI Calendar using Tkinter

Here, we have made a very simple and short program, but you can make this GUI application more user-friendly like you can add entry widgets to get the year during the run time and show the specific year’s calendar by clicking on a button.

In case, you don’t know how to create your GUI application more friendly or how to add more functionalities to your GUI application read more at https://docs.python.org/3/library/tk.html

ThankYou Pythoner’s!!!

You may also like

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.