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()
from tkinter import *
: Imports all the classes, functions, and constants from thetkinter
module. This allows you to use them directly without having to prefix them with the module name.import calendar
: Imports thecalendar
module, which provides various functions related to calendars.def showCalender():
: Defines a function calledshowCalender()
that will be called when the "Show Calendar" button is clicked.gui = Tk()
: Creates an instance of theTk
class, which represents the main window of the GUI application.gui.config(background='grey')
: Sets the background color of the GUI window to grey.gui.title("Calender GUI")
: Sets the title of the GUI window to "Calendar GUI".gui.geometry("550x600")
: Sets the size of the GUI window to 550 pixels wide and 600 pixels high.year = int(year_field.get())
: Retrieves the value entered in theyear_field
Entry widget and converts it to an integer.gui_content = calendar.calendar(year)
: Calls thecalendar
function from thecalendar
module, passing the selected year as an argument. It returns a multi-line string representing the calendar for that year.calYear = Label(gui, text=gui_content, font="Consolas 10 bold")
: Creates a Label widget (calYear
) inside thegui
window, with the text content set to thegui_content
calendar string. The font is set to "Consolas 10 bold".calYear.grid(row=5, column=1, padx=20)
: Positions thecalYear
Label widget in the grid layout of the GUI window at row 5, column 1, with a horizontal padding of 20 pixels.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.new = Tk()
: Creates another instance of theTk
class, representing a new window for the main application.new.config(background='grey')
: Sets the background color of the new window to grey.new.title("Calendar")
: Sets the title of the new window to "Calendar".new.geometry("250x140")
: Sets the size of the new window to 250 pixels wide and 140 pixels high.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.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.year_field = Entry(new)
: Creates an Entry widget (year_field
) inside the new window, which allows the user to enter a year.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 theshowCalender
function is assigned as the command to be executed when the button is clicked.cal.grid(row=1, column=1)
: Positions thecal
Label widget in the grid layout of the new window at row 1, column 1.year.grid(row=2, column=1)
: Positions theyear
Label widget in the grid layout of the new window at row 2, column 1.year_field.grid(row=3, column=1)
: Positions theyear_field
Entry widget in the grid layout of the new window at row 3, column 1.button.grid(row=4, column=1)
: Positions thebutton
Button widget in the grid layout of the new window at row 4, column 1.new.mainloop()
: Starts the event loop for the new window, keeping it open until it is closed by the user.