In Python, we can use Tkinter to make guIs. If you are very imaginative and creative, you can make a lot of interesting things with Tkinter. Here, we’ll make a Python calendar GUI application using Tkinter. In this application, the user must enter the year you want to view the calendar before it appears.
Calendar GUI application using Tkinter
This article is a series of three introductory Python projects. Beginners can try implementing these projects and get their hands dirty in a Python compilation environment.
Step 1: Install Tkinter
First install Tkinter using the following command:
PIP Install TK copies the codeCopy the code
We also need a calendar package, but we don’t have to install it; it’s the default package that comes with Python.
Step 2: Import the module
First import the Calendar module and the Tkinter module
Import Calendar from tkinter import * Copies the codeCopy the code
Step 3: Display the calendar for the given year
The following function displays the calendar for the given year
def showCalender(): gui = Tk() gui.config(background='grey') gui.title("Calender for the year") 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() copies the codeCopy the code
instructions
The ShowCalender function is displaying a calendar. This is where you manage the display of the calendar when you Enter a year in the search box and press Enter. You can set the gray background color here, and you can change it in the code as needed. You can also set the size of the calendar to 550 x 600 here. You then ask for the year to be entered as an integer. Once the user enters the year calendar content, it is retrieved from Python’s calendar module by taking the year as an argument.
Step 4: Set up the driver code
Here is the 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_field =Entry(new) button = button (new, Text ='Show Calender',fg='Black',bg='Blue',command=showCalender) copies the codeCopy the code
Adjust the position of the widgets
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) export.grid (row=6, column=1) new.mainloop() copies the codeCopy the code
instructions
In the driver code, first we provide the background color for the left side of the screen (as shown below). Because the window giving the input year is small, we set the size to 250×140. In the button line below year_field, we call the showCalendar function created above. This function shows us the complete calendar for the entered year.
Now we also need to adjust the widget in the calendar, for which we define the location of everything in the grid. You can explore more by changing grid row and column parameters.
Step 5: Output the calendar GUI
Although the style is ugly, but as a novice to make such a small program or a little sense of achievement.
Original link: juejin.cn/post/701180…