This Is A Python Based Jupyter Notebook Project To Create A Graphical User Interface (GUI) To Show The Calendar , As Per The User's Input .




#Importing tkinter module
# %pip install tkinter
from tkinter import *
#importing calendar module
import calendar

#function to show calendar of the given year
def showCalender():
    gui = Tk()
    gui.config(background='grey')
    gui.title("Calender GUI")
    gui.geometry("550x600")
    year = int(year_field.get())
    gui_content= calendar.calendar(year)
    calYear = Label(gui, text= gui_content, font= "Consolas 10 bold")
    calYear.grid(row=5, column=1,padx=20)
    gui.mainloop()

#Driver code
if __name__=='__main__':
    new = Tk()
    new.config(background='grey')
    new.title("Calender")
    new.geometry("250x140")
    cal = Label(new, text="Calender",bg='grey',font=("times", 28, "bold"))
    year = Label(new, text="Enter year", bg='dark grey')
    year_field=Entry(new)
    button = Button(new, text='Show Calender',
fg='Black',bg='Blue',command=showCalender)

    #putting widgets in position
    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)
    new.mainloop()

  1. from tkinter import *: Imports all the classes, functions, and constants from the tkinter module. This allows you to use them directly without having to prefix them with the module name.

  2. import calendar: Imports the calendar module, which provides various functions related to calendars.

  3. def showCalender():: Defines a function called showCalender() that will be called when the "Show Calendar" button is clicked.

  4. gui = Tk(): Creates an instance of the Tk class, which represents the main window of the GUI application.

  5. gui.config(background='grey'): Sets the background color of the GUI window to grey.

  6. gui.title("Calender GUI"): Sets the title of the GUI window to "Calendar GUI".

  7. gui.geometry("550x600"): Sets the size of the GUI window to 550 pixels wide and 600 pixels high.

  8. year = int(year_field.get()): Retrieves the value entered in the year_field Entry widget and converts it to an integer.

  9. gui_content = calendar.calendar(year): Calls the calendar function from the calendar module, passing the selected year as an argument. It returns a multi-line string representing the calendar for that year.

  10. calYear = Label(gui, text=gui_content, font="Consolas 10 bold"): Creates a Label widget (calYear) inside the gui window, with the text content set to the gui_content calendar string. The font is set to "Consolas 10 bold".

  11. calYear.grid(row=5, column=1, padx=20): Positions the calYear Label widget in the grid layout of the GUI window at row 5, column 1, with a horizontal padding of 20 pixels.

  12. gui.mainloop(): Starts the event loop of the GUI, which continuously listens for events and updates the GUI accordingly. This line keeps the GUI window open until it is closed by the user.

  13. new = Tk(): Creates another instance of the Tk class, representing a new window for the main application.

  14. new.config(background='grey'): Sets the background color of the new window to grey.

  15. new.title("Calendar"): Sets the title of the new window to "Calendar".

  16. new.geometry("250x140"): Sets the size of the new window to 250 pixels wide and 140 pixels high.

  17. cal = Label(new, text="Calendar", bg='grey', font=("times", 28, "bold")): Creates a Label widget (cal) inside the new window, with the text content set to "Calendar". The background color is set to grey, and the font is set to "times" with a size of 28 and bold style.

  18. year = Label(new, text="Enter year", bg='dark grey'): Creates another Label widget (year) inside the new window, with the text content set to "Enter year". The background color is set to dark grey.

  19. year_field = Entry(new): Creates an Entry widget (year_field) inside the new window, which allows the user to enter a year.

  20. button = Button(new, text='Show Calendar', fg='Black', bg='Blue', command=showCalender): Creates a Button widget (button) inside the new window, with the text content set to "Show Calendar". The foreground color is set to black, the background color is set to blue, and the showCalender function is assigned as the command to be executed when the button is clicked.

  21. cal.grid(row=1, column=1): Positions the cal Label widget in the grid layout of the new window at row 1, column 1.

  22. year.grid(row=2, column=1): Positions the year Label widget in the grid layout of the new window at row 2, column 1.

  23. year_field.grid(row=3, column=1): Positions the year_field Entry widget in the grid layout of the new window at row 3, column 1.

  24. button.grid(row=4, column=1): Positions the button Button widget in the grid layout of the new window at row 4, column 1.

  25. new.mainloop(): Starts the event loop for the new window, keeping it open until it is closed by the user.

Hope This Code And The Line By Line Explanation Of The Code Will Help You For Better Understanding . If This Really Helps You , Please Like , Share , Comment & Subscribe Our CODE EXPLORER You-Tube Channel.