How To Build A Calendar In Python Using Tkinter

Introduction

Building Projects in Python is fun, but when added the functionality of Tkinter it adds GUI attribute to the projects as if it is to build a software which gets an essence of interactive software, which input-output or graphical window. Tkinter is lightweights and easy to use, and can operate cross-platform. The functionality of Cross-Platform adds the attribute that it can be used on any operating system, such as Linux, macOS, and even Windows. To install Tkinter, one has to run in the command line as follows,

pip install tkinter

So, Tkinter is a Python Interface to the Graphics User Interface ( GUI ) Toolkit. Using Tkinter in Python enables users to create GUI applications. In order to create applications in Python one needs to,

  • Import the module
  • Create the container ( main window )
  • Add desired widgets to the container
  • Apply event Trigger

Let's build the calculator now,

First, we need to import the Tkinter and import all, so here we denote all by symbol" * " and then we add the functionality of calendar addon through the command import calendar.

from tkinter import *
import calendar

Then we have to add the function to show the calendar of the given year. While the title represents the title of the console and year is for to take the input of the year as integer mentioned by ".get", while geometry represents the window size for the purpose. The gui_content initializes the module integrity for the calendar function.

def showCalender():
    gui = Tk()
    gui.config(background='white')
    gui.title("Calender for the year")
    gui.geometry("525x550")
    year = int(year_field.get())
    gui_content= calendar.calendar(year)
    calYear = Label(gui,text= gui_content,font=("times", 10))
    calYear.grid(row=5, column=1,padx=10)
    gui.mainloop()

Here is the driver code for the purpose. The "cal" denotes the heading for the output and with some added attributes, followed by a "year_field" which will take the input for the year input from the user and then a button to ask the system to show the calendar for the input year. Then the "Exit" button to add the end operation and exit purpose of the console.

if __name__=='__main__':
    new = Tk()
    new.config(background='white')
    new.title("Calender For Any Year")
    new.geometry("410x620")
    cal = Label(new, text="Calender For Any Year",font=("times", 30, "bold",))
    year = Label(new, text="Enter year", bg='orange',font=("times", 30, "bold",))
    year_field=Entry(new,font=("times", 20, "bold",))
    button = Button(new, text='Show Calender',fg='white',bg='Orange',command=showCalender,font=("times", 20, "bold",))
    Exit = Button(new, text="Exit", command=new.destroy,font=("times", 20, "bold",))

Here we add all the widgets in position, namely - " cal.grid "," year.grid "," year_field.grid "," button.grid "," exit.grid ".

cal.grid(row=1, column=1)
    year.grid(row=2, column=1)
    year_field.grid(row=3, column=1)
    button.grid(row=4, column=1)
    Exit.grid(row=5, column=1)
    new.mainloop()

The full code,

from tkinter import *
import calendar
def showCalender():
    gui = Tk()
    gui.config(background='white')
    gui.title("Calender for the year")
    gui.geometry("525x550")
    year = int(year_field.get())
    gui_content= calendar.calendar(year)
    calYear = Label(gui,text= gui_content,font=("times", 10,))
    calYear.grid(row=5, column=1,padx=10)
    gui.mainloop()
if __name__=='__main__':
    new = Tk()
    new.config(background='white')
    new.title("Calender For Any Year")
    new.geometry("410x620")
    cal = Label(new, text="Calender For Any Year",font=("times", 30, "bold",))
    year = Label(new, text="Enter year", bg='orange',font=("times", 30, "bold",))
    year_field=Entry(new,font=("times", 20, "bold",))
    button = Button(new, text='Show Calender',fg='white',bg='Orange',command=showCalender,font=("times", 20, "bold",))
    Exit = Button(new, text="Exit", command=new.destroy,font=("times", 20, "bold",))
    cal.grid(row=1, column=1)
    year.grid(row=2, column=1)
    year_field.grid(row=3, column=1)
    button.grid(row=4, column=1)
    Exit.grid(row=5, column=1)
    new.mainloop()

Output

How To Build A Calendar For Any Year In Python Using Tkinter

How To Build A Calendar For Any Year In Python Using Tkinter

How To Build A Calendar For Any Year In Python Using Tkinter


Similar Articles